Пример #1
0
function ewww_image_optimizer_webp_create($file, $orig_size, $type, $tool)
{
    global $ewww_debug;
    $ewww_debug .= '<b>ewww_image_optimizer_webp_create()</b><br>';
    // change the file extension
    $webpfile = $file . '.webp';
    if (file_exists($webpfile) || !ewww_image_optimizer_get_option('ewww_image_optimizer_webp')) {
        return;
    }
    if (empty($tool)) {
        ewww_image_optimizer_cloud_optimizer($file, $type, false, $webpfile, 'image/webp');
    } else {
        // check to see if 'nice' exists
        $nice = ewww_image_optimizer_find_binary('nice', 'n');
        switch ($type) {
            case 'image/jpeg':
                exec("{$nice} " . $tool . " -q  85 -quiet " . ewww_image_optimizer_escapeshellarg($file) . " -o " . ewww_image_optimizer_escapeshellarg($webpfile) . ' 2>&1', $cli_output);
                break;
            case 'image/png':
                exec("{$nice} " . $tool . " -lossless -quiet " . ewww_image_optimizer_escapeshellarg($file) . " -o " . ewww_image_optimizer_escapeshellarg($webpfile) . ' 2>&1', $cli_output);
                break;
        }
    }
    if (file_exists($webpfile) && $orig_size < filesize($webpfile)) {
        $ewww_debug .= 'webp file was too big, deleting<br>';
        unlink($webpfile);
    }
    ewwwio_memory(__FUNCTION__);
}
/**
 * Process an image.
 *
 * Returns an array of the $file, $results, $converted to tell us if an image changes formats, and the $original file if it did.
 *
 * @param   string $file		Full absolute path to the image file
 * @param   int $gallery_type		1=wordpress, 2=nextgen, 3=flagallery, 4=aux_images, 5=image editor, 6=imagestore, 7=retina
 * @param   boolean $converted		tells us if this is a resize and the full image was converted to a new format
 * @returns array
 */
function ewww_image_optimizer($file, $gallery_type, $converted, $new, $fullsize = false)
{
    //	global $wpdb;
    global $ewww_debug;
    $ewww_debug .= "<b>ewww_image_optimizer()</b><br>";
    // if the plugin gets here without initializing, we need to run through some things first
    if (!defined('EWWW_IMAGE_OPTIMIZER_CLOUD')) {
        ewww_image_optimizer_init();
    }
    // initialize the original filename
    $original = $file;
    $result = '';
    // check that the file exists
    if (FALSE === file_exists($file)) {
        // tell the user we couldn't find the file
        $msg = sprintf(__("Could not find <span class='code'>%s</span>", EWWW_IMAGE_OPTIMIZER_DOMAIN), $file);
        $ewww_debug .= "file doesn't appear to exist: {$file} <br>";
        // send back the above message
        return array(false, $msg, $converted, $original);
    }
    // check that the file is writable
    if (FALSE === is_writable($file)) {
        // tell the user we can't write to the file
        $msg = sprintf(__("<span class='code'>%s</span> is not writable", EWWW_IMAGE_OPTIMIZER_DOMAIN), $file);
        $ewww_debug .= "couldn't write to the file<br>";
        // send back the above message
        return array(false, $msg, $converted, $original);
    }
    if (function_exists('fileperms')) {
        $file_perms = substr(sprintf('%o', fileperms($file)), -4);
    }
    $file_owner = 'unknown';
    $file_group = 'unknown';
    if (function_exists('posix_getpwuid')) {
        $file_owner = posix_getpwuid(fileowner($file));
        $file_owner = $file_owner['name'];
    }
    if (function_exists('posix_getgrgid')) {
        $file_group = posix_getgrgid(filegroup($file));
        $file_group = $file_group['name'];
    }
    $ewww_debug .= "permissions: {$file_perms}, owner: {$file_owner}, group: {$file_group} <br>";
    $type = ewww_image_optimizer_mimetype($file, 'i');
    if (!$type) {
        //otherwise we store an error message since we couldn't get the mime-type
        $msg = __('Missing finfo_file(), getimagesize() and mime_content_type() PHP functions', EWWW_IMAGE_OPTIMIZER_DOMAIN);
        $ewww_debug .= "couldn't find any functions for mimetype detection<br>";
        return array(false, $msg, $converted, $original);
    }
    if (!EWWW_IMAGE_OPTIMIZER_CLOUD) {
        // check to see if 'nice' exists
        $nice = ewww_image_optimizer_find_binary('nice', 'n');
    }
    // if the user has disabled the utility checks
    if (ewww_image_optimizer_get_option('ewww_image_optimizer_skip_check') == TRUE || EWWW_IMAGE_OPTIMIZER_CLOUD) {
        $skip_jpegtran_check = true;
        $skip_optipng_check = true;
        $skip_gifsicle_check = true;
        $skip_pngout_check = true;
    } else {
        // otherwise we set the variables to false
        $skip_jpegtran_check = false;
        $skip_optipng_check = false;
        $skip_gifsicle_check = false;
        $skip_pngout_check = false;
    }
    if (ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) {
        $skip_jpegtran_check = true;
    }
    if (ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_png')) {
        $skip_optipng_check = true;
        $skip_pngout_check = true;
    }
    if (ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_gif')) {
        $skip_gifsicle_check = true;
    }
    // if the full-size image was converted
    if ($converted) {
        $ewww_debug .= "full-size image was converted, need to rebuild filename for meta<br>";
        $filenum = $converted;
        // grab the file extension
        preg_match('/\\.\\w+$/', $file, $fileext);
        // strip the file extension
        $filename = str_replace($fileext[0], '', $file);
        // grab the dimensions
        preg_match('/-\\d+x\\d+(-\\d+)*$/', $filename, $fileresize);
        // strip the dimensions
        $filename = str_replace($fileresize[0], '', $filename);
        // reconstruct the filename with the same increment (stored in $converted) as the full version
        $refile = $filename . '-' . $filenum . $fileresize[0] . $fileext[0];
        // rename the file
        rename($file, $refile);
        $ewww_debug .= "moved {$file} to {$refile}<br>";
        // and set $file to the new filename
        $file = $refile;
        $original = $file;
    }
    // get the original image size
    $orig_size = filesize($file);
    $ewww_debug .= "original filesize: {$orig_size}<br>";
    // initialize $new_size with the original size
    $new_size = $orig_size;
    // set the optimization process to OFF
    $optimize = false;
    // toggle the convert process to ON
    $convert = true;
    // run the appropriate optimization/conversion for the mime-type
    switch ($type) {
        case 'image/jpeg':
            $png_size = 0;
            // if jpg2png conversion is enabled, and this image is in the wordpress media library
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpg_to_png') && $gallery_type == 1 || !empty($_GET['convert'])) {
                // generate the filename for a PNG
                // if this is a resize version
                if ($converted) {
                    // just change the file extension
                    $pngfile = preg_replace('/\\.\\w+$/', '.png', $file);
                    // if this is a full size image
                } else {
                    // get a unique filename for the png image
                    list($pngfile, $filenum) = ewww_image_optimizer_unique_filename($file, '.png');
                }
            } else {
                // otherwise, set it to OFF
                $convert = false;
                $pngfile = '';
            }
            // check for previous optimization, so long as the force flag is on and this isn't a new image that needs converting
            if (empty($_REQUEST['force']) && !($new && $convert)) {
                if ($results_msg = ewww_image_optimizer_check_table($file, $orig_size)) {
                    return array($file, $results_msg, $converted, $original);
                }
            }
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) {
                list($file, $converted, $result, $new_size) = ewww_image_optimizer_cloud_optimizer($file, $type, $convert, $pngfile, 'image/png', $fullsize);
                if ($converted) {
                    $converted = $filenum;
                }
                break;
            }
            if ($convert) {
                $tools = ewww_image_optimizer_path_check(true, true, false, true);
            } else {
                $tools = ewww_image_optimizer_path_check(true, false, false, false);
            }
            // if jpegtran optimization is disabled
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_disable_jpegtran')) {
                // store an appropriate message in $result
                $result = sprintf(__('%s is disabled', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'jpegtran');
                // otherwise, if we aren't skipping the utility verification and jpegtran doesn't exist
            } elseif (!$skip_jpegtran_check && !$tools['JPEGTRAN']) {
                // store an appropriate message in $result
                $result = sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>');
                // otherwise, things should be good, so...
            } else {
                // set the optimization process to ON
                $optimize = true;
            }
            // if optimization is turned ON
            if ($optimize && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) {
                $ewww_debug .= "attempting to optimize JPG...<br>";
                // generate temporary file-names:
                $tempfile = $file . ".tmp";
                //non-progressive jpeg
                $progfile = $file . ".prog";
                // progressive jpeg
                // check to see if we are supposed to strip metadata (badly named)
                if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpegtran_copy') == TRUE) {
                    // don't copy metadata
                    $copy_opt = 'none';
                } else {
                    // copy all the metadata
                    $copy_opt = 'all';
                }
                // run jpegtran - non-progressive
                exec("{$nice} " . $tools['JPEGTRAN'] . " -copy {$copy_opt} -optimize -outfile " . ewww_image_optimizer_escapeshellarg($tempfile) . " " . ewww_image_optimizer_escapeshellarg($file));
                // run jpegtran - progressive
                exec("{$nice} " . $tools['JPEGTRAN'] . " -copy {$copy_opt} -optimize -progressive -outfile " . ewww_image_optimizer_escapeshellarg($progfile) . " " . ewww_image_optimizer_escapeshellarg($file));
                if (is_file($tempfile)) {
                    // check the filesize of the non-progressive JPG
                    $non_size = filesize($tempfile);
                } else {
                    $non_size = 0;
                }
                if (is_file($progfile)) {
                    // check the filesize of the progressive JPG
                    $prog_size = filesize($progfile);
                } else {
                    $prog_size = 0;
                }
                $ewww_debug .= "optimized JPG (non-progresive) size: {$non_size}<br>";
                $ewww_debug .= "optimized JPG (progresive) size: {$prog_size}<br>";
                if ($non_size === false || $prog_size === false) {
                    $result = __('Unable to write file', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $new_size = 0;
                } elseif (!$non_size || !$prog_size) {
                    $result = __('Optimization failed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $new_size = 0;
                } else {
                    // if the progressive file is bigger
                    if ($prog_size > $non_size) {
                        // store the size of the non-progessive JPG
                        $new_size = $non_size;
                        if (is_file($progfile)) {
                            // delete the progressive file
                            unlink($progfile);
                        }
                        // if the progressive file is smaller or the same
                    } else {
                        // store the size of the progressive JPG
                        $new_size = $prog_size;
                        // replace the non-progressive with the progressive file
                        rename($progfile, $tempfile);
                    }
                }
                $ewww_debug .= "optimized JPG size: {$new_size}<br>";
                // if the best-optimized is smaller than the original JPG, and we didn't create an empty JPG
                if ($orig_size > $new_size && $new_size != 0) {
                    // replace the original with the optimized file
                    rename($tempfile, $file);
                    // store the results of the optimization
                    $result = "{$orig_size} vs. {$new_size}";
                    // if the optimization didn't produce a smaller JPG
                } else {
                    if (is_file($tempfile)) {
                        // delete the optimized file
                        unlink($tempfile);
                    }
                    // store the results
                    $result = 'unchanged';
                    $new_size = $orig_size;
                }
                // if conversion and optimization are both turned OFF, finish the JPG processing
            } elseif (!$convert) {
                break;
            }
            // if the conversion process is turned ON, or if this is a resize and the full-size was converted
            if ($convert && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) {
                $ewww_debug .= "attempting to convert JPG to PNG: {$pngfile} <br>";
                // retrieve version info for ImageMagick
                $convert_path = ewww_image_optimizer_find_binary('convert', 'i');
                // convert the JPG to PNG
                if (!empty($convert_path)) {
                    $ewww_debug .= "converting with ImageMagick<br>";
                    exec($convert_path . " " . ewww_image_optimizer_escapeshellarg($file) . " -strip " . ewww_image_optimizer_escapeshellarg($pngfile));
                } elseif (ewww_image_optimizer_gd_support()) {
                    $ewww_debug .= "converting with GD<br>";
                    imagepng(imagecreatefromjpeg($file), $pngfile);
                }
                // if lossy optimization is ON and full-size exclusion is not active
                if (ewww_image_optimizer_get_option('ewww_image_optimizer_png_lossy') && $tools['PNGQUANT'] && !$fullsize) {
                    $ewww_debug .= "attempting lossy reduction<br>";
                    exec("{$nice} " . $tools['PNGQUANT'] . " " . ewww_image_optimizer_escapeshellarg($pngfile));
                    $quantfile = preg_replace('/\\.\\w+$/', '-fs8.png', $pngfile);
                    if (file_exists($quantfile) && filesize($pngfile) > filesize($quantfile)) {
                        $ewww_debug .= "lossy reduction is better: original - " . filesize($pngfile) . " vs. lossy - " . filesize($quantfile) . "<br>";
                        rename($quantfile, $pngfile);
                    } elseif (file_exists($quantfile)) {
                        $ewww_debug .= "lossy reduction is worse: original - " . filesize($pngfile) . " vs. lossy - " . filesize($quantfile) . "<br>";
                        unlink($quantfile);
                    } else {
                        $ewww_debug .= "pngquant did not produce any output<br>";
                    }
                }
                // if optipng isn't disabled
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng')) {
                    // retrieve the optipng optimization level
                    $optipng_level = ewww_image_optimizer_get_option('ewww_image_optimizer_optipng_level');
                    if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpegtran_copy') && preg_match('/0.7/', ewww_image_optimizer_tool_found($tools['OPTIPNG'], 'o'))) {
                        $strip = '-strip all ';
                    } else {
                        $strip = '';
                    }
                    // if the PNG file was created
                    if (file_exists($pngfile)) {
                        $ewww_debug .= "optimizing converted PNG with optipng<br>";
                        // run optipng on the new PNG
                        exec("{$nice} " . $tools['OPTIPNG'] . " -o{$optipng_level} -quiet {$strip} " . ewww_image_optimizer_escapeshellarg($pngfile));
                    }
                }
                // if pngout isn't disabled
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_pngout')) {
                    // retrieve the pngout optimization level
                    $pngout_level = ewww_image_optimizer_get_option('ewww_image_optimizer_pngout_level');
                    // if the PNG file was created
                    if (file_exists($pngfile)) {
                        $ewww_debug .= "optimizing converted PNG with pngout<br>";
                        // run pngout on the new PNG
                        exec("{$nice} " . $tools['PNGOUT'] . " -s{$pngout_level} -q " . ewww_image_optimizer_escapeshellarg($pngfile));
                    }
                }
                if (is_file($pngfile)) {
                    // find out the size of the new PNG file
                    $png_size = filesize($pngfile);
                } else {
                    $png_size = 0;
                }
                $ewww_debug .= "converted PNG size: {$png_size}<br>";
                // if the PNG is smaller than the original JPG, and we didn't end up with an empty file
                if ($new_size > $png_size && $png_size != 0) {
                    $ewww_debug .= "converted PNG is better: {$png_size} vs. {$new_size}<br>";
                    // store the size of the converted PNG
                    $new_size = $png_size;
                    // check to see if the user wants the originals deleted
                    if (ewww_image_optimizer_get_option('ewww_image_optimizer_delete_originals') == TRUE) {
                        // delete the original JPG
                        unlink($file);
                    }
                    // store the location of the PNG file
                    $file = $pngfile;
                    // successful conversion and we store the increment
                    $converted = $filenum;
                } else {
                    $ewww_debug .= "converted PNG is no good<br>";
                    // otherwise delete the PNG
                    $converted = FALSE;
                    if (is_file($pngfile)) {
                        unlink($pngfile);
                    }
                }
            }
            break;
        case 'image/png':
            // png2jpg conversion is turned on, and the image is in the wordpress media library
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_png_to_jpg') && $gallery_type == 1 || !empty($_GET['convert'])) {
                $ewww_debug .= "PNG to JPG conversion turned on<br>";
                // if the user set a fill background for transparency
                $background = '';
                if ($background = ewww_image_optimizer_jpg_background()) {
                    // set background color for GD
                    $r = hexdec('0x' . strtoupper(substr($background, 0, 2)));
                    $g = hexdec('0x' . strtoupper(substr($background, 2, 2)));
                    $b = hexdec('0x' . strtoupper(substr($background, 4, 2)));
                    // set the background flag for 'convert'
                    $background = "-background " . '"' . "#{$background}" . '"';
                } else {
                    $r = '';
                    $g = '';
                    $b = '';
                }
                // if the user manually set the JPG quality
                if ($quality = ewww_image_optimizer_jpg_quality()) {
                    // set the quality for GD
                    $gquality = $quality;
                    // set the quality flag for 'convert'
                    $cquality = "-quality {$quality}";
                } else {
                    $cquality = '';
                    $gquality = '92';
                }
                // if this is a resize version
                if ($converted) {
                    // just replace the file extension with a .jpg
                    $jpgfile = preg_replace('/\\.\\w+$/', '.jpg', $file);
                    // if this is a full version
                } else {
                    // construct the filename for the new JPG
                    list($jpgfile, $filenum) = ewww_image_optimizer_unique_filename($file, '.jpg');
                }
            } else {
                $ewww_debug .= "PNG to JPG conversion turned off<br>";
                // turn the conversion process OFF
                $convert = false;
                $jpgfile = '';
                $r = null;
                $g = null;
                $b = null;
                $gquality = null;
            }
            // check for previous optimization, so long as the force flag is on and this isn't a new image that needs converting
            if (empty($_REQUEST['force']) && !($new && $convert)) {
                if ($results_msg = ewww_image_optimizer_check_table($file, $orig_size)) {
                    return array($file, $results_msg, $converted, $original);
                }
            }
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_png')) {
                list($file, $converted, $result, $new_size) = ewww_image_optimizer_cloud_optimizer($file, $type, $convert, $jpgfile, 'image/jpeg', $fullsize, array('r' => $r, 'g' => $g, 'b' => $b, 'quality' => $gquality));
                if ($converted) {
                    $converted = $filenum;
                }
                break;
            }
            if ($convert) {
                $tools = ewww_image_optimizer_path_check(true, true, false, true);
            } else {
                $tools = ewww_image_optimizer_path_check(false, true, false, true);
            }
            // if pngout and optipng are disabled
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng') && ewww_image_optimizer_get_option('ewww_image_optimizer_disable_pngout')) {
                // tell the user all PNG tools are disabled
                $result = __('png tools are disabled', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                // if the utility checking is on, optipng is enabled, but optipng cannot be found
            } elseif (!$skip_optipng_check && !$tools['OPTIPNG'] && !ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng')) {
                // tell the user optipng is missing
                $result = sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng</em>');
                // if the utility checking is on, pngout is enabled, but pngout cannot be found
            } elseif (!$skip_pngout_check && !$tools['PNGOUT'] && !ewww_image_optimizer_get_option('ewww_image_optimizer_disable_pngout')) {
                // tell the user pngout is missing
                $result = sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>pngout</em>');
            } else {
                // turn optimization on if we made it through all the checks
                $optimize = true;
            }
            // if optimization is turned on
            if ($optimize) {
                // if lossy optimization is ON and full-size exclusion is not active
                if (ewww_image_optimizer_get_option('ewww_image_optimizer_png_lossy') && $tools['PNGQUANT'] && !$fullsize) {
                    $ewww_debug .= "attempting lossy reduction<br>";
                    exec("{$nice} " . $tools['PNGQUANT'] . " " . ewww_image_optimizer_escapeshellarg($file));
                    $quantfile = preg_replace('/\\.\\w+$/', '-fs8.png', $file);
                    if (file_exists($quantfile) && filesize($file) > filesize($quantfile)) {
                        $ewww_debug .= "lossy reduction is better: original - " . filesize($file) . " vs. lossy - " . filesize($quantfile) . "<br>";
                        rename($quantfile, $file);
                    } elseif (file_exists($quantfile)) {
                        $ewww_debug .= "lossy reduction is worse: original - " . filesize($file) . " vs. lossy - " . filesize($quantfile) . "<br>";
                        unlink($quantfile);
                    } else {
                        $ewww_debug .= "pngquant did not produce any output<br>";
                    }
                }
                // if optipng is enabled
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng')) {
                    // retrieve the optimization level for optipng
                    $optipng_level = ewww_image_optimizer_get_option('ewww_image_optimizer_optipng_level');
                    if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpegtran_copy') && preg_match('/0.7/', ewww_image_optimizer_tool_found($tools['OPTIPNG'], 'o'))) {
                        $strip = '-strip all ';
                    } else {
                        $strip = '';
                    }
                    // run optipng on the PNG file
                    exec("{$nice} " . $tools['OPTIPNG'] . " -o{$optipng_level} -quiet {$strip} " . ewww_image_optimizer_escapeshellarg($file));
                }
                // if pngout is enabled
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_pngout')) {
                    // retrieve the optimization level for pngout
                    $pngout_level = ewww_image_optimizer_get_option('ewww_image_optimizer_pngout_level');
                    // run pngout on the PNG file
                    exec("{$nice} " . $tools['PNGOUT'] . " -s{$pngout_level} -q " . ewww_image_optimizer_escapeshellarg($file));
                }
                // if conversion and optimization are both disabled we are done here
            } elseif (!$convert) {
                $ewww_debug .= "not going to process as we can neither convert or optimize<br>";
                break;
            }
            // flush the cache for filesize
            clearstatcache();
            // retrieve the new filesize of the PNG
            $new_size = filesize($file);
            // if conversion is on and the PNG doesn't have transparency or the user set a background color to replace transparency
            if ($convert && (!ewww_image_optimizer_png_alpha($file) || ewww_image_optimizer_jpg_background())) {
                $ewww_debug .= "attempting to convert PNG to JPG: {$jpgfile} <br>";
                // retrieve version info for ImageMagick
                $convert_path = ewww_image_optimizer_find_binary('convert', 'i');
                // convert the PNG to a JPG with all the proper options
                if (!empty($convert_path)) {
                    $ewww_debug .= "converting with ImageMagick<br>";
                    $ewww_debug .= "using command: {$convert_path} {$background} -flatten {$cquality} {$file} {$jpgfile}";
                    exec("{$convert_path} {$background} -flatten {$cquality} " . ewww_image_optimizer_escapeshellarg($file) . " " . ewww_image_optimizer_escapeshellarg($jpgfile));
                } elseif (ewww_image_optimizer_gd_support()) {
                    $ewww_debug .= "converting with GD<br>";
                    // retrieve the data from the PNG
                    $input = imagecreatefrompng($file);
                    // retrieve the dimensions of the PNG
                    list($width, $height) = getimagesize($file);
                    // create a new image with those dimensions
                    $output = imagecreatetruecolor($width, $height);
                    if ($r === '') {
                        $r = 255;
                        $g = 255;
                        $b = 255;
                    }
                    // allocate the background color
                    $rgb = imagecolorallocate($output, $r, $g, $b);
                    // fill the new image with the background color
                    imagefilledrectangle($output, 0, 0, $width, $height, $rgb);
                    // copy the original image to the new image
                    imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
                    // output the JPG with the quality setting
                    imagejpeg($output, $jpgfile, $gquality);
                }
                if (is_file($jpgfile)) {
                    // retrieve the filesize of the new JPG
                    $jpg_size = filesize($jpgfile);
                    $ewww_debug .= "converted JPG filesize: {$jpg_size}<br>";
                } else {
                    $jpg_size = 0;
                    $ewww_debug .= "unable to convert to JPG<br>";
                }
                // next we need to optimize that JPG if jpegtran is enabled
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_jpegtran') && file_exists($jpgfile)) {
                    // generate temporary file-names:
                    $tempfile = $jpgfile . ".tmp";
                    //non-progressive jpeg
                    $progfile = $jpgfile . ".prog";
                    // progressive jpeg
                    // check to see if we are supposed to strip metadata (badly named)
                    if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpegtran_copy') == TRUE) {
                        // don't copy metadata
                        $copy_opt = 'none';
                    } else {
                        // copy all the metadata
                        $copy_opt = 'all';
                    }
                    // run jpegtran - non-progressive
                    exec("{$nice} " . $tools['JPEGTRAN'] . " -copy {$copy_opt} -optimize -outfile " . ewww_image_optimizer_escapeshellarg($tempfile) . " " . ewww_image_optimizer_escapeshellarg($jpgfile));
                    // run jpegtran - progressive
                    exec("{$nice} " . $tools['JPEGTRAN'] . " -copy {$copy_opt} -optimize -progressive -outfile " . ewww_image_optimizer_escapeshellarg($progfile) . " " . ewww_image_optimizer_escapeshellarg($jpgfile));
                    if (is_file($tempfile)) {
                        // check the filesize of the non-progressive JPG
                        $non_size = filesize($tempfile);
                        $ewww_debug .= "non-progressive JPG filesize: {$non_size}<br>";
                    } else {
                        $non_size = 0;
                    }
                    if (is_file($progfile)) {
                        // check the filesize of the progressive JPG
                        $prog_size = filesize($progfile);
                        $ewww_debug .= "progressive JPG filesize: {$prog_size}<br>";
                    } else {
                        $prog_size = 0;
                    }
                    // if the progressive file is bigger
                    if ($prog_size > $non_size) {
                        // store the size of the non-progessive JPG
                        $opt_jpg_size = $non_size;
                        if (is_file($progfile)) {
                            // delete the progressive file
                            unlink($progfile);
                        }
                        $ewww_debug .= "keeping non-progressive JPG<br>";
                        // if the progressive file is smaller or the same
                    } else {
                        // store the size of the progressive JPG
                        $opt_jpg_size = $prog_size;
                        // replace the non-progressive with the progressive file
                        rename($progfile, $tempfile);
                        $ewww_debug .= "keeping progressive JPG<br>";
                    }
                    // if the best-optimized is smaller than the original JPG, and we didn't create an empty JPG
                    if ($jpg_size > $opt_jpg_size && $opt_jpg_size != 0) {
                        // replace the original with the optimized file
                        rename($tempfile, $jpgfile);
                        // store the size of the optimized JPG
                        $jpg_size = $opt_jpg_size;
                        $ewww_debug .= "optimized JPG was smaller than un-optimized version<br>";
                        // if the optimization didn't produce a smaller JPG
                    } elseif (is_file($tempfile)) {
                        // delete the optimized file
                        unlink($tempfile);
                    }
                }
                $ewww_debug .= "converted JPG size: {$jpg_size}<br>";
                // if the new JPG is smaller than the original PNG
                if ($new_size > $jpg_size && $jpg_size != 0) {
                    // store the size of the JPG as the new filesize
                    $new_size = $jpg_size;
                    // if the user wants originals delted after a conversion
                    if (ewww_image_optimizer_get_option('ewww_image_optimizer_delete_originals') == TRUE) {
                        // delete the original PNG
                        unlink($file);
                    }
                    // update the $file location to the new JPG
                    $file = $jpgfile;
                    // successful conversion, so we store the increment
                    $converted = $filenum;
                } else {
                    $converted = FALSE;
                    if (is_file($jpgfile)) {
                        // otherwise delete the new JPG
                        unlink($jpgfile);
                    }
                }
            }
            break;
        case 'image/gif':
            // if gif2png is turned on, and the image is in the wordpress media library
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_gif_to_png') && $gallery_type == 1 || !empty($_GET['convert'])) {
                // generate the filename for a PNG
                // if this is a resize version
                if ($converted) {
                    // just change the file extension
                    $pngfile = preg_replace('/\\.\\w+$/', '.png', $file);
                    // if this is the full version
                } else {
                    // construct the filename for the new PNG
                    list($pngfile, $filenum) = ewww_image_optimizer_unique_filename($file, '.png');
                }
            } else {
                // turn conversion OFF
                $convert = false;
                $pngfile = '';
            }
            // check for previous optimization, so long as the force flag is on and this isn't a new image that needs converting
            if (empty($_REQUEST['force']) && !($new && $convert)) {
                if ($results_msg = ewww_image_optimizer_check_table($file, $orig_size)) {
                    return array($file, $results_msg, $converted, $original);
                }
            }
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_gif')) {
                list($file, $converted, $result, $new_size) = ewww_image_optimizer_cloud_optimizer($file, $type, $convert, $pngfile, 'image/png', $fullsize);
                if ($converted) {
                    $converted = $filenum;
                }
                break;
            }
            if ($convert) {
                $tools = ewww_image_optimizer_path_check(false, true, true, true);
            } else {
                $tools = ewww_image_optimizer_path_check(false, false, true, false);
            }
            // if gifsicle is disabled
            if (ewww_image_optimizer_get_option('ewww_image_optimizer_disable_gifsicle')) {
                // return an appropriate message
                $result = sprintf(__('%s is disabled', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'gifsicle');
                // if utility checking is on, and gifsicle is not installed
            } elseif (!$skip_gifsicle_check && !$tools['GIFSICLE']) {
                // return an appropriate message
                $result = sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>');
            } else {
                // otherwise, turn optimization ON
                $optimize = true;
            }
            // if optimization is turned ON
            if ($optimize) {
                $tempfile = $file . ".tmp";
                //temporary GIF output
                // run gifsicle on the GIF
                exec("{$nice} " . $tools['GIFSICLE'] . " -b -O3 --careful -o {$tempfile}" . ewww_image_optimizer_escapeshellarg($file));
                if (file_exists($tempfile)) {
                    // retrieve the filesize of the temporary GIF
                    $new_size = filesize($tempfile);
                    // if the new GIF is smaller
                    if ($orig_size > $new_size && $new_size != 0) {
                        // replace the original with the optimized file
                        rename($tempfile, $file);
                        // store the results of the optimization
                        $result = "{$orig_size} vs. {$new_size}";
                        // if the optimization didn't produce a smaller GIF
                    } else {
                        if (is_file($tempfile)) {
                            // delete the optimized file
                            unlink($tempfile);
                        }
                        // store the results
                        $result = 'unchanged';
                        $new_size = $orig_size;
                    }
                }
                // if conversion and optimization are both turned OFF, we are done here
            } elseif (!$convert) {
                break;
            }
            // flush the cache for filesize
            clearstatcache();
            // get the new filesize for the GIF
            $new_size = filesize($file);
            // if conversion is ON and the GIF isn't animated
            if ($convert && !ewww_image_optimizer_is_animated($file)) {
                // if optipng is enabled
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng') && $tools['OPTIPNG']) {
                    // retrieve the optipng optimization level
                    $optipng_level = ewww_image_optimizer_get_option('ewww_image_optimizer_optipng_level');
                    if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpegtran_copy') && preg_match('/0.7/', ewww_image_optimizer_tool_found($tools['OPTIPNG'], 'o'))) {
                        $strip = '-strip all ';
                    } else {
                        $strip = '';
                    }
                    // run optipng on the GIF file
                    exec("{$nice} " . $tools['OPTIPNG'] . " -out " . ewww_image_optimizer_escapeshellarg($pngfile) . " -o{$optipng_level} -quiet {$strip} " . ewww_image_optimizer_escapeshellarg($file));
                }
                // if pngout is enabled
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_pngout') && $tools['PNGOUT']) {
                    // retrieve the pngout optimization level
                    $pngout_level = ewww_image_optimizer_get_option('ewww_image_optimizer_pngout_level');
                    // if $pngfile exists (which means optipng was run already)
                    if (file_exists($pngfile)) {
                        // run pngout on the PNG file
                        exec("{$nice} " . $tools['PNGOUT'] . " -s{$pngout_level} -q " . ewww_image_optimizer_escapeshellarg($pngfile));
                    } else {
                        // run pngout on the GIF file
                        exec("{$nice} " . $tools['PNGOUT'] . " -s{$pngout_level} -q " . ewww_image_optimizer_escapeshellarg($file) . " " . ewww_image_optimizer_escapeshellarg($pngfile));
                    }
                }
                // if a PNG file was created
                if (file_exists($pngfile)) {
                    // retrieve the filesize of the PNG
                    $png_size = filesize($pngfile);
                    // if the new PNG is smaller than the original GIF
                    if ($new_size > $png_size && $png_size != 0) {
                        // store the PNG size as the new filesize
                        $new_size = $png_size;
                        // if the user wants original GIFs deleted after successful conversion
                        if (ewww_image_optimizer_get_option('ewww_image_optimizer_delete_originals') == TRUE) {
                            // delete the original GIF
                            unlink($file);
                        }
                        // update the $file location with the new PNG
                        $file = $pngfile;
                        // successful conversion (for now), so we store the increment
                        $converted = $filenum;
                    } else {
                        $converted = FALSE;
                        if (is_file($pngfile)) {
                            unlink($pngfile);
                        }
                    }
                }
            }
            break;
        default:
            // if not a JPG, PNG, or GIF, tell the user we don't work with strangers
            return array($file, __('Unknown type: ' . $type, EWWW_IMAGE_OPTIMIZER_DOMAIN), $converted, $original);
    }
    // if their cloud api license limit has been exceeded
    if ($result == 'exceeded') {
        return array($file, __('License exceeded', EWWW_IMAGE_OPTIMIZER_DOMAIN), $converted, $original);
    }
    if (!empty($new_size)) {
        $results_msg = ewww_image_optimizer_update_table($file, $new_size, $orig_size, $new);
        return array($file, $results_msg, $converted, $original);
    }
    // otherwise, send back the filename, the results (some sort of error message), the $converted flag, and the name of the original image
    return array($file, $result, $converted, $original);
}
Пример #3
0
function ewww_image_optimizer_webp_create($file, $orig_size, $type, $tool, $recreate = false)
{
    ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
    // change the file extension
    $webpfile = $file . '.webp';
    if (!ewww_image_optimizer_get_option('ewww_image_optimizer_webp')) {
        return;
    } elseif (is_file($webpfile) && empty($_REQUEST['ewww_force']) && !$recreate) {
        ewwwio_debug_message('webp file exists, not forcing or recreating');
        return;
    }
    if (empty($tool)) {
        ewww_image_optimizer_cloud_optimizer($file, $type, false, $webpfile, 'image/webp');
    } else {
        // check to see if 'nice' exists
        $nice = ewww_image_optimizer_find_nix_binary('nice', 'n');
        switch ($type) {
            case 'image/jpeg':
                exec("{$nice} " . $tool . " -q  85 -quiet " . ewww_image_optimizer_escapeshellarg($file) . " -o " . ewww_image_optimizer_escapeshellarg($webpfile) . ' 2>&1', $cli_output);
                break;
            case 'image/png':
                exec("{$nice} " . $tool . " -lossless -quiet " . ewww_image_optimizer_escapeshellarg($file) . " -o " . ewww_image_optimizer_escapeshellarg($webpfile) . ' 2>&1', $cli_output);
                break;
        }
    }
    $webp_size = ewww_image_optimizer_filesize($webpfile);
    ewwwio_debug_message("webp is {$webp_size} vs. {$type} is {$orig_size}");
    if (is_file($webpfile) && $orig_size < $webp_size) {
        ewwwio_debug_message('webp file was too big, deleting');
        unlink($webpfile);
    } elseif (is_file($webpfile)) {
        // Set correct file permissions
        $stat = stat(dirname($webpfile));
        $perms = $stat['mode'] & 0666;
        //same permissions as parent folder, strip off the executable bits
        @chmod($webpfile, $perms);
    }
    ewwwio_memory(__FUNCTION__);
}