Skip to content

Commit

Permalink
fixed conversion bugs, did more testing, ready to rock
Browse files Browse the repository at this point in the history
  • Loading branch information
nosilver4u committed May 11, 2018
1 parent 5788b9e commit 25fe02b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

License: GPLv3

IMPORTANT: This is beta-quality, has received some testing, but needs more.

This is a PHP library that you can use to integrate with the [EWWW Image Optimizer API](https://ewww.io/). The API can be used to reduce image filesize using lossless and lossy methods as well as image format conversion.

By default, EWWW Image Optimizer uses lossy JPG and lossless PNG optimization techniques, The lossy optimization for JPG and PNG files uses sophisticated algorithms to minimize perceptual quality loss, which is vastly different than setting a static quality/compression level.
Expand All @@ -18,7 +16,10 @@ Include the library, and start rolling:
```php
include_once( 'ewwwio-php/ewwwio.php' );
$ewwwio = new EWWWIO( 'abc123' ); // API key is required at instantiation.
$ewwwio->optimize( '/var/www/images/sample.jpg' );
$result = $ewwwio->optimize( '/var/www/images/sample.jpg' );
if ( ! $result ) { // find out what the problem was...
echo $ewwwio->get_error() . "\n";
}
```

You can also verify your key like so:
Expand Down Expand Up @@ -47,5 +48,8 @@ Not yet, but maybe in the future. [The WordPress plugin can though](https://ewww

## Changelog

### 1.0
* fixed conversion bugs, fully tested and marking stable

### 0.90
* initial release, may eat your cat
55 changes: 41 additions & 14 deletions ewwwio.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function boolval( $value ) {
define( 'EWWWIO_PATH', dirname( __file__ ) . '/' );
}

define( 'EWWW_IMAGE_OPTIMIZER_VERSION', '0.9' );
define( 'EWWW_IMAGE_OPTIMIZER_VERSION', '1.0' );

require_once( EWWWIO_PATH . 'vendor/Requests/library/Requests.php' );
Requests::register_autoloader();
Expand Down Expand Up @@ -303,6 +303,39 @@ protected function filesize( $file ) {
}
}

/**
* Generate a unique filename for a converted image.
*
* @param string $file The filename to test for uniqueness.
* @param string $fileext An iterator to append to the base filename, starts empty usually.
* @return array {
* Filename information.
*
* @type string A unique filename for converting an image.
* @type int|string The iterator used for uniqueness.
* }
*/
function unique_filename( $file, $fileext ) {
// Strip the file extension.
$filename = preg_replace( '/\.\w+$/', '', $file );
if ( ! is_file( $filename . $fileext ) ) {
return array( $filename . $fileext, '' );
}
// Set the increment to 1 for starters.
$filenum = 1;
// But it must be only letters, numbers, or underscores.
$filenum = ( preg_match( '/^[\w\d]*$/', $filenum ) ? $filenum : 1 );
$suffix = ( ! empty( $filenum ) ? '-' . $filenum : '' );
// While a file exists with the current increment.
while ( file_exists( $filename . $suffix . $fileext ) ) {
// Increment the increment...
$filenum++;
$suffix = '-' . $filenum;
}
// All done, let's reconstruct the filename.
return array( $filename . $suffix . $fileext, $filenum );
}

/**
* Process an image.
*
Expand Down Expand Up @@ -388,13 +421,12 @@ function optimize( $file ) {
$pngfile = '';
}
if ( $this->jpg_level ) {
list( $file, $converted, $result, $new_size ) = $this->cloud_optimizer( $file, $type, $convert, $pngfile, 'image/png' );
if ( $converted ) {
list( $file, $result, $new_size ) = $this->cloud_optimizer( $file, $type, $convert, $pngfile, 'image/png' );
if ( $this->converted ) {
if ( $this->delete_originals ) {
// delete the original JPG
unlink( $original );
}
$this->converted = true;
$this->webp_create( $file, $new_size, 'image/png' );
} else {
$this->webp_create( $file, $new_size, $type );
Expand All @@ -415,13 +447,12 @@ function optimize( $file ) {
$jpgfile = '';
}
if ( $this->png_level ) {
list( $file, $converted, $result, $new_size ) = $this->cloud_optimizer( $file, $type, $convert, $jpgfile, 'image/jpeg', $this->jpg_background, $this->$jpg_quality );
if ( $converted ) {
list( $file, $result, $new_size ) = $this->cloud_optimizer( $file, $type, $convert, $jpgfile, 'image/jpeg', $this->jpg_background, $this->jpg_quality );
if ( $this->converted ) {
if ( $this->delete_originals ) {
// delete the original JPG
unlink( $original );
}
$this->converted = true;
$this->webp_create( $file, $new_size, 'image/jpeg');
} else {
$this->webp_create( $file, $new_size, $type );
Expand All @@ -441,17 +472,14 @@ function optimize( $file ) {
$pngfile = '';
}
if ( $this->gif_level ) {
list( $file, $converted, $result, $new_size ) = $this->cloud_optimizer( $file, $type, $convert, $pngfile, 'image/png' );
if ( $converted ) {
list( $file, $result, $new_size ) = $this->cloud_optimizer( $file, $type, $convert, $pngfile, 'image/png' );
if ( $this->converted ) {
if ( $this->delete_originals ) {
// delete the original JPG
unlink( $original );
}
$this->converted = true;
$this->webp_create( $file, $new_size, 'image/png' );
}
} else {
$this->webp_create( $file, $new_size, $type );
}
break;
case 'application/pdf':
Expand Down Expand Up @@ -734,7 +762,6 @@ function cloud_optimizer( $file, $type, $convert = false, $newfile = null, $newt
file_put_contents( $tempfile, $response->body );
$orig_size = filesize( $file );
$newsize = $orig_size;
$converted = false;
$msg = '';
if ( 100 > strlen( $response->body ) && strpos( $response->body, 'invalid' ) ) {
$this->debug_message( 'License invalid' );
Expand All @@ -761,7 +788,7 @@ function cloud_optimizer( $file, $type, $convert = false, $newfile = null, $newt
} else {
unlink( $tempfile );
}
return array( $file, $converted, $msg, $newsize );
return array( $file, $msg, $newsize );
}
}
}

0 comments on commit 25fe02b

Please sign in to comment.