function ewww_manage_image_custom_column($column_name, $id) { // once we've found our custom column if ($column_name == 'ewww_image_optimizer') { // get the metadata for the image $meta = new nggMeta($id); // get the optimization status for the image $status = $meta->get_META('ewww_image_optimizer'); $msg = ''; // get the file path of the image $file_path = $meta->image->imagePath; // get the mimetype of the image $type = ewww_image_optimizer_mimetype($file_path, 'i'); // retrieve the human-readable filesize of the image $file_size = size_format(ewww_image_optimizer_filesize($file_path), 2); $file_size = str_replace('B ', 'B', $file_size); //$file_size = ewww_image_optimizer_format_bytes(filesize($file_path)); $valid = true; // check to see if we have a tool to handle the mimetype detected switch ($type) { case 'image/jpeg': // if jpegtran is missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'); } break; case 'image/png': // if the PNG tools are missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_png')) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'); } break; case 'image/gif': // if gifsicle is missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_gif')) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'); } break; default: $valid = false; } // file isn't in a format we can work with, we don't work with strangers if ($valid == false) { print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg; return; } // if we have a valid status, display it, the image size, and give a re-optimize link if ($status && !empty($status)) { echo $status; echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); printf("<br><a href=\"admin.php?action=ewww_ngg_manual&ewww_force=1&ewww_attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); // otherwise, give the image size, and a link to optimize right now } else { _e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); printf("<br><a href=\"admin.php?action=ewww_ngg_manual&ewww_attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } }
function ewww_manage_image_custom_column($column_name, $id) { // once we've found our custom column if ($column_name == 'ewww_image_optimizer' || $column_name == '') { $output = ''; // creating the 'registry' object for working with nextgen $registry = C_Component_Registry::get_instance(); // creating a database storage object from the 'registry' object $storage = $registry->get_utility('I_Gallery_Storage'); if (is_object($id)) { $image = $id; } else { // get an image object $image = $storage->object->_image_mapper->find($id); } if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) { $print_meta = print_r($image->meta_data, TRUE); $print_meta = preg_replace(array('/ /', '/\\n+/'), array(' ', '<br />'), esc_html($print_meta)); $output .= '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>'; } $msg = ''; // get the absolute path $file_path = $storage->get_image_abspath($image, 'full'); // get the mimetype of the image $type = ewww_image_optimizer_mimetype($file_path, 'i'); // retrieve the human-readable filesize of the image $file_size = size_format(ewww_image_optimizer_filesize($file_path), 2); $file_size = str_replace('B ', 'B', $file_size); $valid = true; // check to see if we have a tool to handle the mimetype detected switch ($type) { case 'image/jpeg': // if jpegtran is missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'), array('em' => array())); } break; case 'image/png': // if the PNG tools are missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_png')) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'), array('em' => array())); } break; case 'image/gif': // if gifsicle is missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_gif')) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'), array('em' => array())); } break; default: $valid = false; } // file isn't in a format we can work with, we don't work with strangers if ($valid == false) { echo esc_html__('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg; return; } // if we have a valid status, display it if (!empty($image->meta_data['ewww_image_optimizer'])) { $output .= esc_html($image->meta_data['ewww_image_optimizer']); // otherwise, give the image size, and a link to optimize right now } else { $output .= esc_html__('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); } // display the image size $output .= "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size) . "<br>"; // display the optimization link with the appropriate text $output .= $this->ewww_render_optimize_action_link($id, $image); if (is_object($id)) { return $output; } else { echo $output; } } }
/** * Print column data for optimizer results in the media library using * the `manage_media_custom_column` hook. */ function ewww_image_optimizer_custom_column($column_name, $id) { global $ewww_debug; $ewww_debug .= "<b>ewww_image_optimizer_custom_column()</b><br>"; // once we get to the EWWW IO custom column if ($column_name == 'ewww-image-optimizer') { // retrieve the metadata $meta = wp_get_attachment_metadata($id); if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) { $print_meta = print_r($meta, TRUE); $print_meta = preg_replace(array('/ /', '/\\n+/'), array(' ', '<br />'), $print_meta); echo '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>'; } if (!empty($meta['cloudinary'])) { _e('Cloudinary image', EWWW_IMAGE_OPTIMIZER_DOMAIN); return; } // if the filepath isn't set in the metadata if (empty($meta['file'])) { if (isset($meta['file'])) { unset($meta['file']); if (strpos($meta['ewww_image_optimizer'], 'Could not find') === 0) { unset($meta['ewww_image_optimizer']); } wp_update_attachment_metadata($id, $meta); } } list($file_path, $upload_path) = ewww_image_optimizer_attachment_path($meta, $id); // if the file does not exist if (empty($file_path)) { _e('Could not retrieve file path.', EWWW_IMAGE_OPTIMIZER_DOMAIN); return; } $msg = ''; $convert_desc = ''; $convert_link = ''; // retrieve the mimetype of the attachment $type = ewww_image_optimizer_mimetype($file_path, 'i'); // get a human readable filesize $file_size = size_format(filesize($file_path), 2); $file_size = preg_replace('/\\.00 B /', ' B', $file_size); // run the appropriate code based on the mimetype switch ($type) { case 'image/jpeg': // if jpegtran is missing, tell them that if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'); } else { $convert_link = __('JPG to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN); $class_type = 'jpg'; $convert_desc = __('WARNING: Removes metadata. Requires GD or ImageMagick. PNG is generally much better than JPG for logos and other images with a limited range of colors.', EWWW_IMAGE_OPTIMIZER_DOMAIN); } break; case 'image/png': // if pngout and optipng are missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_png')) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'); } else { $convert_link = __('PNG to JPG', EWWW_IMAGE_OPTIMIZER_DOMAIN); $class_type = 'png'; $convert_desc = __('WARNING: This is not a lossless conversion and requires GD or ImageMagick. JPG is much better than PNG for photographic use because it compresses the image and discards data. Transparent images will only be converted if a background color has been set.', EWWW_IMAGE_OPTIMIZER_DOMAIN); } break; case 'image/gif': // if gifsicle is missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_gif')) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'); } else { $convert_link = __('GIF to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN); $class_type = 'gif'; $convert_desc = __('PNG is generally better than GIF, but does not support animation. Animated images will not be converted.', EWWW_IMAGE_OPTIMIZER_DOMAIN); } break; default: // not a supported mimetype _e('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN); return; } // if the optimizer metadata exists if (!empty($meta['ewww_image_optimizer'])) { // output the optimizer results echo $meta['ewww_image_optimizer']; // output the filesize echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { // output a link to re-optimize manually printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&ewww_force=1&ewww_attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id)) { echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&ewww_attachment_ID={$id}&ewww_convert=1&ewww_force=1'>{$convert_link}</a>"; } } else { echo $msg; } $restorable = false; if (!empty($meta['converted'])) { if (!empty($meta['orig_file']) && file_exists($meta['orig_file'])) { $restorable = true; } } if (isset($meta['sizes'])) { // meta sizes don't contain a path, so we calculate one $base_dir = dirname($file_path) . '/'; foreach ($meta['sizes'] as $size => $data) { if (!empty($data['converted'])) { if (!empty($data['orig_file']) && file_exists($base_dir . $data['orig_file'])) { $restorable = true; } } } } if ($restorable && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_restore&ewww_attachment_ID=%d\">%s</a>", $id, __('Restore original', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } // link to webp upgrade script $oldwebpfile = preg_replace('/\\.\\w+$/', '.webp', $file_path); if (file_exists($oldwebpfile) && current_user_can(apply_filters('ewww_image_optimizer_admin_permissions', ''))) { echo "<br><a href='options.php?page=ewww-image-optimizer-webp-migrate'>Run WebP upgrade</a>"; } // determine filepath for webp $webpfile = $file_path . '.webp'; if (file_exists($webpfile)) { $webpurl = wp_get_attachment_url($id) . '.webp'; // get a human readable filesize $webp_size = size_format(filesize($webpfile), 2); $webp_size = preg_replace('/\\.00 B /', ' B', $webp_size); echo "<br>WebP: <a href='{$webpurl}'>{$webp_size}</a>"; } } else { // otherwise, this must be an image we haven't processed _e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); // tell them the filesize echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { // and give the user the option to optimize the image right now printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&ewww_attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id)) { echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&ewww_attachment_ID={$id}&ewww_convert=1&ewww_force=1'>{$convert_link}</a>"; } } else { echo $msg; } } } ewwwio_memory(__FUNCTION__); }
function ewww_manage_image_custom_column($column_name, $id) { // check to make sure we're outputing our custom column if ($column_name == 'ewww_image_optimizer') { // get the metadata $meta = new flagMeta($id); if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) { $print_meta = print_r($meta->image->meta_data, TRUE); $print_meta = preg_replace(array('/ /', '/\\n+/'), array(' ', '<br />'), $print_meta); echo '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>'; } // grab the image status from the meta $status = $meta->image->meta_data['ewww_image_optimizer']; $msg = ''; // get the image path from the meta $file_path = $meta->image->imagePath; // get the mimetype $type = ewww_image_optimizer_mimetype($file_path, 'i'); // get the file size $file_size = size_format(filesize($file_path), 2); $file_size = str_replace('B ', 'B', $file_size); $valid = true; // if we don't have a valid tool for the image type, output the appropriate message switch ($type) { case 'image/jpeg': if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !EWWW_IMAGE_OPTIMIZER_CLOUD) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'); } break; case 'image/png': if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !EWWW_IMAGE_OPTIMIZER_CLOUD) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'); } break; case 'image/gif': if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !EWWW_IMAGE_OPTIMIZER_CLOUD) { $valid = false; $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'); } break; default: $valid = false; } // let user know if the file type is unsupported if ($valid == false) { _e('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN); return; } // output the image status if we know it if (!empty($status)) { echo $status; echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); printf("<br><a href=\"admin.php?action=ewww_flag_manual&force=1&attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); // otherwise, tell the user that they can optimize the image now } else { _e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); printf("<br><a href=\"admin.php?action=ewww_flag_manual&attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } }
function ewww_image_optimizer_scan_other() { global $wpdb; // initialize the $attachments variable for auxiliary images $attachments = null; // check if there is a previous bulk operation to resume if (get_option('ewww_image_optimizer_aux_resume')) { // retrieve the attachment IDs that have not been finished from the 'bulk attachments' option $attachments = get_option('ewww_image_optimizer_aux_attachments'); } else { // collect a list of images from the current theme $child_path = get_stylesheet_directory(); $parent_path = get_template_directory(); $attachments = ewww_image_optimizer_image_scan($child_path); if ($child_path !== $parent_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($parent_path)); } // collect a list of images for buddypress if (!function_exists('is_plugin_active')) { // need to include the plugin library for the is_plugin_active function require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if (is_plugin_active('buddypress/bp-loader.php') || is_plugin_active_for_network('buddypress/bp-loader.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/avatars'), ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/group-avatars')); } if (is_plugin_active('buddypress-activity-plus/bpfb.php') || is_plugin_active_for_network('buddypress-activity-plus/bpfb.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/bpfb')); } if (is_plugin_active('grand-media/grand-media.php') || is_plugin_active_for_network('grand-media/grand-media.php')) { // scan the grand media folder for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(WP_CONTENT_DIR . '/grand-media')); } if (is_plugin_active('wp-symposium/wp-symposium.php') || is_plugin_active_for_network('wp-symposium/wp-symposium.php')) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(get_option('symposium_img_path'))); } if (is_plugin_active('ml-slider/ml-slider.php') || is_plugin_active_for_network('ml-slider/ml-slider.php')) { $slide_paths = array(); $sliders = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'ml-slider'"); $slides = $wpdb->get_col("\n\t\t\t\tSELECT wpposts.ID \n\t\t\t\tFROM {$wpdb->posts} wpposts \n\t\t\t\tINNER JOIN {$wpdb->term_relationships} term_relationships\n\t\t\t\t\t\tON wpposts.ID = term_relationships.object_id\n\t\t\t\tINNER JOIN {$wpdb->terms} wpterms \n\t\t\t\t\t\tON term_relationships.term_taxonomy_id = wpterms.term_id\n\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} term_taxonomy\n\t\t\t\t\t\tON wpterms.term_id = term_taxonomy.term_id\n\t\t\t\tWHERE \tterm_taxonomy.taxonomy = 'ml-slider'\n\t\t\t\t\tAND wpposts.post_type = 'attachment'\n\t\t\t\t"); foreach ($slides as $slide) { $type = get_post_meta($slide, 'ml-slider_type', true); $type = $type ? $type : 'image'; // backwards compatibility, fall back to 'image' if ($type != 'image') { continue; } $backup_sizes = get_post_meta($slide, '_wp_attachment_backup_sizes', true); if (ewww_image_optimizer_iterable($backup_sizes)) { foreach ($backup_sizes as $backup_size => $meta) { if (preg_match('/resized-/', $backup_size)) { $path = $meta['path']; $image_size = ewww_image_optimizer_filesize($path); if (!$image_size) { continue; } $already_optimized = ewww_image_optimizer_find_already_optimized($path); $mimetype = ewww_image_optimizer_mimetype($path, 'i'); if (preg_match('/^image\\/(jpeg|png|gif)/', $mimetype) && empty($already_optimized)) { $slide_paths[] = $path; } } } } } $attachments = array_merge($attachments, $slide_paths); } // collect a list of images in auxiliary folders provided by user if ($aux_paths = ewww_image_optimizer_get_option('ewww_image_optimizer_aux_paths')) { if (ewww_image_optimizer_iterable($aux_paths)) { foreach ($aux_paths as $aux_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($aux_path)); } } } // store the filenames we retrieved in the 'bulk_attachments' option so we can keep track of our progress in the database update_option('ewww_image_optimizer_aux_attachments', $attachments, false); } return $attachments; }
/** * 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); }
function ewww_manage_image_custom_column($column_name, $id) { // check to make sure we're outputing our custom column if ($column_name == 'ewww_image_optimizer') { // get the metadata $meta = new flagMeta($id); if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) { $print_meta = print_r($meta->image->meta_data, TRUE); $print_meta = preg_replace(array('/ /', '/\\n+/'), array(' ', '<br />'), esc_html($print_meta)); echo '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>'; } // grab the image status from the meta if (empty($meta->image->meta_data['ewww_image_optimizer'])) { $status = ''; } else { $status = $meta->image->meta_data['ewww_image_optimizer']; } $msg = ''; // get the image path from the meta $file_path = $meta->image->imagePath; // get the mimetype $type = ewww_image_optimizer_mimetype($file_path, 'i'); // get the file size $file_size = size_format(ewww_image_optimizer_filesize($file_path), 2); $file_size = str_replace('B ', 'B', $file_size); $valid = true; // if we don't have a valid tool for the image type, output the appropriate message $skip = ewww_image_optimizer_skip_tools(); switch ($type) { case 'image/jpeg': if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !$skip['jpegtran']) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'), array('em' => array())); } break; case 'image/png': if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !$skip['optipng'] && !$skip['pngout']) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'), array('em' => array())); } break; case 'image/gif': if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !$skip['gifsicle']) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'), array('em' => array())); } break; default: $valid = false; } // let user know if the file type is unsupported if ($valid == false) { esc_html_e('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN); return; } $ewww_manual_nonce = wp_create_nonce("ewww-manual-" . $id); // output the image status if we know it if (!empty($status)) { echo esc_html($status); echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { printf("<br><a href=\"admin.php?action=ewww_flag_manual&ewww_manual_nonce={$ewww_manual_nonce}&ewww_force=1&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } elseif (get_transient('ewwwio-background-in-progress-flag-' . $id)) { esc_html_e('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN); // otherwise, tell the user that they can optimize the image now } else { esc_html_e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { printf("<br><a href=\"admin.php?action=ewww_flag_manual&ewww_manual_nonce={$ewww_manual_nonce}&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } } }
function ewww_image_optimizer_scan_other() { global $wpdb; // $aux_resume = get_option('ewww_image_optimizer_aux_resume'); // initialize the $attachments variable for auxiliary images $attachments = null; // check the 'bulk resume' option // $resume = get_option('ewww_image_optimizer_aux_resume'); // check if there is a previous bulk operation to resume if (get_option('ewww_image_optimizer_aux_resume')) { // retrieve the attachment IDs that have not been finished from the 'bulk attachments' option $attachments = get_option('ewww_image_optimizer_aux_attachments'); } else { // collect a list of images from the current theme $child_path = get_stylesheet_directory(); $parent_path = get_template_directory(); $attachments = ewww_image_optimizer_image_scan($child_path); if ($child_path !== $parent_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($parent_path)); } // collect a list of images for buddypress if (!function_exists('is_plugin_active')) { // need to include the plugin library for the is_plugin_active function require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if (is_plugin_active('buddypress/bp-loader.php') || is_plugin_active_for_network('buddypress/bp-loader.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/avatars'), ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/group-avatars')); } if (is_plugin_active('buddypress-activity-plus/bpfb.php') || is_plugin_active_for_network('buddypress-activity-plus/bpfb.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/bpfb')); } if (is_plugin_active('grand-media/grand-media.php') || is_plugin_active_for_network('grand-media/grand-media.php')) { // scan the grand media folder for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(WP_CONTENT_DIR . '/grand-media')); } if (is_plugin_active('wp-symposium/wp-symposium.php') || is_plugin_active_for_network('wp-symposium/wp-symposium.php')) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(get_option('symposium_img_path'))); } if (is_plugin_active('ml-slider/ml-slider.php') || is_plugin_active_for_network('ml-slider/ml-slider.php')) { $slide_paths = array(); $sliders = get_posts(array('numberposts' => -1, 'post_type' => 'ml-slider', 'post_status' => 'any', 'fields' => 'ids')); foreach ($sliders as $slider) { $slides = get_posts(array('numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_type' => 'attachment', 'post_status' => 'inherit', 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'ml-slider', 'field' => 'slug', 'terms' => $slider)))); foreach ($slides as $slide) { $backup_sizes = get_post_meta($slide, '_wp_attachment_backup_sizes', true); $type = get_post_meta($slide, 'ml-slider_type', true); $type = $type ? $type : 'image'; // backwards compatibility, fall back to 'image' if ($type === 'image') { foreach ($backup_sizes as $backup_size => $meta) { if (preg_match('/resized-/', $backup_size)) { $path = $meta['path']; $image_size = filesize($path); $query = $wpdb->prepare("SELECT id FROM {$wpdb->ewwwio_images} WHERE path LIKE %s AND image_size LIKE '{$image_size}'", $path); $optimized_query = $wpdb->get_results($query, ARRAY_A); if (!empty($optimized_query)) { foreach ($optimized_query as $image) { if ($image['path'] == $path) { // $ewww_debug .= "{$image['path']} does not match $path, continuing our search<br>"; $already_optimized = $image; } } } $mimetype = ewww_image_optimizer_mimetype($path, 'i'); if (preg_match('/^image\\/(jpeg|png|gif)/', $mimetype) && empty($already_optimized)) { $slide_paths[] = $path; } } } } } } $attachments = array_merge($attachments, $slide_paths); } // collect a list of images in auxiliary folders provided by user if ($aux_paths = ewww_image_optimizer_get_option('ewww_image_optimizer_aux_paths')) { foreach ($aux_paths as $aux_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($aux_path)); } } // store the filenames we retrieved in the 'bulk_attachments' option so we can keep track of our progress in the database update_option('ewww_image_optimizer_aux_attachments', $attachments); } return $attachments; }
/** * 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 * @param boolean $converted tells us if this is a resize and the full image was converted to a new format * @param boolean $new tells the optimizer that this is a new image, so it should attempt conversion regardless of previous results * @param boolean $fullsize tells the optimizer this is a full size image * @returns array */ function ewww_image_optimizer($file, $gallery_type = 4, $converted = false, $new = false, $fullsize = false) { ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); // if the plugin gets here without initializing, we need to run through some things first if (!defined('EWWW_IMAGE_OPTIMIZER_CLOUD')) { ewww_image_optimizer_cloud_init(); } session_write_close(); $bypass_optimization = apply_filters('ewww_image_optimizer_bypass', false, $file); if (true === $bypass_optimization) { // tell the user optimization was skipped $msg = __("Optimization skipped", EWWW_IMAGE_OPTIMIZER_DOMAIN); ewwwio_debug_message("optimization bypassed: {$file}"); // send back the above message return array(false, $msg, $converted, $file); } // 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 %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file); ewwwio_debug_message("file doesn't appear to exist: {$file}"); // 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(__('%s is not writable', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file); ewwwio_debug_message("couldn't write to the file {$file}"); // 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']; } ewwwio_debug_message("permissions: {$file_perms}, owner: {$file_owner}, group: {$file_group}"); $type = ewww_image_optimizer_mimetype($file, 'i'); if (strpos($type, 'image') === FALSE && strpos($type, 'pdf') === FALSE) { ewwwio_debug_message('could not find any functions for mimetype detection'); //otherwise we store an error message since we couldn't get the mime-type return array(false, __('Unknown type: ' . $type, EWWW_IMAGE_OPTIMIZER_DOMAIN), $converted, $original); $msg = __('Missing finfo_file(), getimagesize() and mime_content_type() PHP functions', EWWW_IMAGE_OPTIMIZER_DOMAIN); return array(false, $msg, $converted, $original); } if (!EWWW_IMAGE_OPTIMIZER_CLOUD) { // check to see if 'nice' exists $nice = ewww_image_optimizer_find_nix_binary('nice', 'n'); if (!defined('EWWW_IMAGE_OPTIMIZER_NOEXEC')) { // Check if exec is disabled if (ewww_image_optimizer_exec_check()) { define('EWWW_IMAGE_OPTIMIZER_NOEXEC', true); ewwwio_debug_message('exec seems to be disabled'); ewww_image_optimizer_disable_tools(); // otherwise, query the php settings for safe mode } elseif (ewww_image_optimizer_safemode_check()) { define('EWWW_IMAGE_OPTIMIZER_NOEXEC', true); ewwwio_debug_message('safe mode appears to be enabled'); ewww_image_optimizer_disable_tools(); } else { define('EWWW_IMAGE_OPTIMIZER_NOEXEC', false); } } } $skip = ewww_image_optimizer_skip_tools(); // if the user has disabled the utility checks if (EWWW_IMAGE_OPTIMIZER_CLOUD) { $skip['jpegtran'] = true; $skip['optipng'] = true; $skip['gifsicle'] = true; $skip['pngout'] = true; $skip['pngquant'] = true; $skip['webp'] = true; } if (ewww_image_optimizer_get_option('ewww_image_optimizer_metadata_skip_full') && $fullsize) { $keep_metadata = true; } else { $keep_metadata = false; } if (ewww_image_optimizer_get_option('ewww_image_optimizer_lossy_skip_full') && $fullsize) { $skip_lossy = true; } else { $skip_lossy = false; } if (ini_get('max_execution_time') < 90 && ewww_image_optimizer_stl_check()) { set_time_limit(0); } // if the full-size image was converted if ($converted) { ewwwio_debug_message('full-size image was converted, need to rebuild filename for meta'); $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); ewwwio_debug_message("moved {$file} to {$refile}"); // and set $file to the new filename $file = $refile; $original = $file; } // get the original image size $orig_size = filesize($file); ewwwio_debug_message("original filesize: {$orig_size}"); if ($orig_size < ewww_image_optimizer_get_option('ewww_image_optimizer_skip_size')) { // tell the user optimization was skipped $msg = __("Optimization skipped", EWWW_IMAGE_OPTIMIZER_DOMAIN); ewwwio_debug_message("optimization bypassed due to filesize: {$file}"); // send back the above message return array(false, $msg, $converted, $file); } if ($type == 'image/png' && ewww_image_optimizer_get_option('ewww_image_optimizer_skip_png_size') && $orig_size > ewww_image_optimizer_get_option('ewww_image_optimizer_skip_png_size')) { // tell the user optimization was skipped $msg = __("Optimization skipped", EWWW_IMAGE_OPTIMIZER_DOMAIN); ewwwio_debug_message("optimization bypassed due to filesize: {$file}"); // send back the above message return array($file, $msg, $converted, $file); } // initialize $new_size with the original size, HOW ABOUT A ZERO... //$new_size = $orig_size; $new_size = 0; // set the optimization process to OFF $optimize = false; // toggle the convert process to ON $convert = true; // allow other plugins to mangle the image however they like prior to optimization do_action('ewww_image_optimizer_pre_optimization', $file, $type); // 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['ewww_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['ewww_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_jpg_level') > 10) { list($file, $converted, $result, $new_size) = ewww_image_optimizer_cloud_optimizer($file, $type, $convert, $pngfile, 'image/png', $skip_lossy); if ($converted) { // 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($original); } $converted = $filenum; ewww_image_optimizer_webp_create($file, $new_size, 'image/png', null, $orig_size != $new_size); } else { ewww_image_optimizer_webp_create($file, $new_size, $type, null, $orig_size != $new_size); } break; } if ($convert) { $tools = ewww_image_optimizer_path_check(!$skip['jpegtran'], !$skip['optipng'], false, !$skip['pngout'], !$skip['pngquant'], !$skip['webp']); } else { $tools = ewww_image_optimizer_path_check(!$skip['jpegtran'], false, false, false, false, !$skip['webp']); } // if jpegtran optimization is disabled if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpg_level') == 0) { // store an appropriate message in $result $result = __('JPG optimization is disabled', EWWW_IMAGE_OPTIMIZER_DOMAIN); // otherwise, if we aren't skipping the utility verification and jpegtran doesn't exist } elseif (!$skip['jpegtran'] && !$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) { ewwwio_debug_message('attempting to optimize JPG...'); // 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') && !$keep_metadata) { // 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)); // check the filesize of the non-progressive JPG $non_size = ewww_image_optimizer_filesize($tempfile); // check the filesize of the progressive JPG $prog_size = ewww_image_optimizer_filesize($progfile); ewwwio_debug_message("optimized JPG (non-progresive) size: {$non_size}"); ewwwio_debug_message("optimized JPG (progresive) size: {$prog_size}"); 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); } } ewwwio_debug_message("optimized JPG size: {$new_size}"); // 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 && ewww_image_optimizer_mimetype($tempfile, 'i') == $type) { // 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) { ewww_image_optimizer_webp_create($file, $orig_size, $type, $tools['WEBP']); break; } // if the conversion process is turned ON, or if this is a resize and the full-size was converted if ($convert) { ewwwio_debug_message("attempting to convert JPG to PNG: {$pngfile}"); if (empty($new_size)) { $new_size = $orig_size; } // convert the JPG to PNG if (ewww_image_optimizer_gmagick_support()) { try { $gmagick = new Gmagick($file); $gmagick->stripimage(); $gmagick->setimageformat('PNG'); $gmagick->writeimage($pngfile); } catch (Exception $gmagick_error) { ewwwio_debug_message($gmagick_error->getMessage()); } $png_size = ewww_image_optimizer_filesize($pngfile); } if (!$png_size && ewww_image_optimizer_imagick_support()) { try { $imagick = new Imagick($file); $imagick->stripImage(); $imagick->setImageFormat('PNG'); $imagick->writeImage($pngfile); } catch (Exception $imagick_error) { ewwwio_debug_message($imagick_error->getMessage()); } $png_size = ewww_image_optimizer_filesize($pngfile); } if (!$png_size) { $convert_path = ''; // retrieve version info for ImageMagick if (PHP_OS != 'WINNT') { $convert_path = ewww_image_optimizer_find_nix_binary('convert', 'i'); } elseif (PHP_OS == 'WINNT') { $convert_path = ewww_image_optimizer_find_win_binary('convert', 'i'); } if (!empty($convert_path)) { ewwwio_debug_message('converting with ImageMagick'); exec($convert_path . " " . ewww_image_optimizer_escapeshellarg($file) . " -strip " . ewww_image_optimizer_escapeshellarg($pngfile)); $png_size = ewww_image_optimizer_filesize($pngfile); } } if (!$png_size && ewww_image_optimizer_gd_support()) { ewwwio_debug_message('converting with GD'); imagepng(imagecreatefromjpeg($file), $pngfile); $png_size = ewww_image_optimizer_filesize($pngfile); } // if lossy optimization is ON and full-size exclusion is not active if (ewww_image_optimizer_get_option('ewww_image_optimizer_png_level') == 40 && $tools['PNGQUANT'] && !$skip_lossy) { ewwwio_debug_message('attempting lossy reduction'); exec("{$nice} " . $tools['PNGQUANT'] . " " . ewww_image_optimizer_escapeshellarg($pngfile)); $quantfile = preg_replace('/\\.\\w+$/', '-fs8.png', $pngfile); if (is_file($quantfile) && filesize($pngfile) > filesize($quantfile)) { ewwwio_debug_message("lossy reduction is better: original - " . filesize($pngfile) . " vs. lossy - " . filesize($quantfile)); rename($quantfile, $pngfile); } elseif (is_file($quantfile)) { ewwwio_debug_message("lossy reduction is worse: original - " . filesize($pngfile) . " vs. lossy - " . filesize($quantfile)); unlink($quantfile); } else { ewwwio_debug_message('pngquant did not produce any output'); } } // if optipng isn't disabled if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng')) { // retrieve the optipng optimization level $optipng_level = (int) 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')) && !$keep_metadata) { $strip = '-strip all '; } else { $strip = ''; } // if the PNG file was created if (file_exists($pngfile)) { ewwwio_debug_message('optimizing converted PNG with optipng'); // 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 = (int) ewww_image_optimizer_get_option('ewww_image_optimizer_pngout_level'); // if the PNG file was created if (file_exists($pngfile)) { ewwwio_debug_message('optimizing converted PNG with pngout'); // run pngout on the new PNG exec("{$nice} " . $tools['PNGOUT'] . " -s{$pngout_level} -q " . ewww_image_optimizer_escapeshellarg($pngfile)); } } $png_size = ewww_image_optimizer_filesize($pngfile); ewwwio_debug_message("converted PNG size: {$png_size}"); // 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_image_optimizer_mimetype($pngfile, 'i') == 'image/png') { ewwwio_debug_message("converted PNG is better: {$png_size} vs. {$new_size}"); // 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; // let webp know what we're dealing with now $type = 'image/png'; // successful conversion and we store the increment $converted = $filenum; } else { ewwwio_debug_message('converted PNG is no good'); // otherwise delete the PNG $converted = FALSE; if (is_file($pngfile)) { unlink($pngfile); } } } ewww_image_optimizer_webp_create($file, $new_size, $type, $tools['WEBP'], $orig_size != $new_size); break; case 'image/png': $jpg_size = 0; // 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') || !empty($_GET['ewww_convert'])) && $gallery_type == 1 && !$skip_lossy && (!ewww_image_optimizer_png_alpha($file) || ewww_image_optimizer_jpg_background())) { ewwwio_debug_message('PNG to JPG conversion turned on'); // 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 { ewwwio_debug_message('PNG to JPG conversion turned off'); // 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['ewww_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_png_level') >= 20 && ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_key')) { list($file, $converted, $result, $new_size) = ewww_image_optimizer_cloud_optimizer($file, $type, $convert, $jpgfile, 'image/jpeg', $skip_lossy, array('r' => $r, 'g' => $g, 'b' => $b, 'quality' => $gquality)); if ($converted) { // 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($original); } $converted = $filenum; ewww_image_optimizer_webp_create($file, $new_size, 'image/jpeg', null, $orig_size != $new_size); } else { ewww_image_optimizer_webp_create($file, $new_size, $type, null, $orig_size != $new_size); } break; } if ($convert) { $tools = ewww_image_optimizer_path_check(!$skip['jpegtran'], !$skip['optipng'], false, !$skip['pngout'], !$skip['pngquant'], !$skip['webp']); } else { $tools = ewww_image_optimizer_path_check(false, !$skip['optipng'], false, !$skip['pngout'], !$skip['pngquant'], !$skip['webp']); } // 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') || ewww_image_optimizer_get_option('ewww_image_optimizer_png_level') == 0) { // tell the user all PNG tools are disabled $result = __('PNG optimization is disabled', EWWW_IMAGE_OPTIMIZER_DOMAIN); // if the utility checking is on, optipng is enabled, but optipng cannot be found } elseif (!$skip['optipng'] && !$tools['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'] && !$tools['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_level') == 40 && $tools['PNGQUANT'] && !$skip_lossy) { ewwwio_debug_message('attempting lossy reduction'); exec("{$nice} " . $tools['PNGQUANT'] . " " . ewww_image_optimizer_escapeshellarg($file)); $quantfile = preg_replace('/\\.\\w+$/', '-fs8.png', $file); if (is_file($quantfile) && filesize($file) > filesize($quantfile) && ewww_image_optimizer_mimetype($quantfile, 'i') == $type) { ewwwio_debug_message("lossy reduction is better: original - " . filesize($file) . " vs. lossy - " . filesize($quantfile)); rename($quantfile, $file); } elseif (is_file($quantfile)) { ewwwio_debug_message("lossy reduction is worse: original - " . filesize($file) . " vs. lossy - " . filesize($quantfile)); unlink($quantfile); } else { ewwwio_debug_message('pngquant did not produce any output'); } } $tempfile = $file . '.tmp.png'; copy($file, $tempfile); // if optipng is enabled if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng')) { // retrieve the optimization level for optipng $optipng_level = (int) 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')) && !$keep_metadata) { $strip = '-strip all '; } else { $strip = ''; } // run optipng on the PNG file exec("{$nice} " . $tools['OPTIPNG'] . " -o{$optipng_level} -quiet {$strip} " . ewww_image_optimizer_escapeshellarg($tempfile)); } // if pngout is enabled if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_pngout')) { // retrieve the optimization level for pngout $pngout_level = (int) 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($tempfile)); } // retrieve the filesize of the temporary PNG $new_size = ewww_image_optimizer_filesize($tempfile); // if the new PNG is smaller if ($orig_size > $new_size && $new_size != 0 && ewww_image_optimizer_mimetype($tempfile, 'i') == $type) { // 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 PNG } 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 disabled we are done here } elseif (!$convert) { ewwwio_debug_message('calling webp, but neither convert or optimize'); ewww_image_optimizer_webp_create($file, $orig_size, $type, $tools['WEBP']); break; } // retrieve the new filesize of the PNG $new_size = ewww_image_optimizer_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() ) ) { if ($convert) { ewwwio_debug_message("attempting to convert PNG to JPG: {$jpgfile}"); if (empty($new_size)) { $new_size = $orig_size; } $magick_background = ewww_image_optimizer_jpg_background(); if (empty($magick_background)) { $magick_background = '000000'; } // convert the PNG to a JPG with all the proper options if (ewww_image_optimizer_gmagick_support()) { try { if (ewww_image_optimizer_png_alpha($file)) { $gmagick_overlay = new Gmagick($file); $gmagick = new Gmagick(); $gmagick->newimage($gmagick_overlay->getimagewidth(), $gmagick_overlay->getimageheight(), '#' . $magick_background); $gmagick->compositeimage($gmagick_overlay, 1, 0, 0); } else { $gmagick = new Gmagick($file); } $gmagick->setimageformat('JPG'); $gmagick->setcompressionquality($gquality); $gmagick->writeimage($jpgfile); } catch (Exception $gmagick_error) { ewwwio_debug_message($gmagick_error->getMessage()); } $jpg_size = ewww_image_optimizer_filesize($jpgfile); } if (!$jpg_size && ewww_image_optimizer_imagick_support()) { try { $imagick = new Imagick($file); if (ewww_image_optimizer_png_alpha($file)) { $imagick->setImageBackgroundColor(new ImagickPixel('#' . $magick_background)); $imagick->setImageAlphaChannel(11); } $imagick->setImageFormat('JPG'); $imagick->setCompressionQuality($gquality); $imagick->writeImage($jpgfile); } catch (Exception $imagick_error) { ewwwio_debug_message($imagick_error->getMessage()); } $jpg_size = ewww_image_optimizer_filesize($jpgfile); } if (!$jpg_size) { // retrieve version info for ImageMagick $convert_path = ewww_image_optimizer_find_nix_binary('convert', 'i'); if (!empty($convert_path)) { ewwwio_debug_message('converting with ImageMagick'); ewwwio_debug_message("using command: {$convert_path} {$background} -alpha remove {$cquality} {$file} {$jpgfile}"); exec("{$convert_path} {$background} -alpha remove {$cquality} " . ewww_image_optimizer_escapeshellarg($file) . " " . ewww_image_optimizer_escapeshellarg($jpgfile)); $jpg_size = ewww_image_optimizer_filesize($jpgfile); } } if (!$jpg_size && ewww_image_optimizer_gd_support()) { ewwwio_debug_message('converting with GD'); // 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); } $jpg_size = ewww_image_optimizer_filesize($jpgfile); if ($jpg_size) { ewwwio_debug_message("converted JPG filesize: {$jpg_size}"); } else { ewwwio_debug_message('unable to convert to JPG'); } // next we need to optimize that JPG if jpegtran is enabled if (ewww_image_optimizer_get_option('ewww_image_optimizer_jpg_level') == 10 && 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') && !$keep_metadata) { // 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)); // check the filesize of the non-progressive JPG $non_size = ewww_image_optimizer_filesize($tempfile); ewwwio_debug_message("non-progressive JPG filesize: {$non_size}"); // check the filesize of the progressive JPG $prog_size = ewww_image_optimizer_filesize($progfile); ewwwio_debug_message("progressive JPG filesize: {$prog_size}"); // 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); } ewwwio_debug_message('keeping non-progressive JPG'); // 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); ewwwio_debug_message('keeping progressive JPG'); } // 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; ewwwio_debug_message('optimized JPG was smaller than un-optimized version'); // if the optimization didn't produce a smaller JPG } elseif (is_file($tempfile)) { // delete the optimized file unlink($tempfile); } } ewwwio_debug_message("converted JPG size: {$jpg_size}"); // if the new JPG is smaller than the original PNG if ($new_size > $jpg_size && $jpg_size != 0 && ewww_image_optimizer_mimetype($jpgfile, 'i') == 'image/jpeg') { // 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; // let webp know what we're dealing with now $type = 'image/jpeg'; // successful conversion, so we store the increment $converted = $filenum; } else { $converted = FALSE; if (is_file($jpgfile)) { // otherwise delete the new JPG unlink($jpgfile); } } } ewww_image_optimizer_webp_create($file, $new_size, $type, $tools['WEBP'], $orig_size != $new_size); 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') || !empty($_GET['ewww_convert'])) && $gallery_type == 1 && !ewww_image_optimizer_is_animated($file)) { // 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['ewww_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_key') && ewww_image_optimizer_get_option('ewww_image_optimizer_gif_level') == 10) { list($file, $converted, $result, $new_size) = ewww_image_optimizer_cloud_optimizer($file, $type, $convert, $pngfile, 'image/png', $skip_lossy); if ($converted) { // 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($original); } $converted = $filenum; ewww_image_optimizer_webp_create($file, $new_size, 'image/png', null, $orig_size != $new_size); } break; } if ($convert) { $tools = ewww_image_optimizer_path_check(false, !$skip['optipng'], !$skip['gifsicle'], !$skip['pngout'], !$skip['pngquant'], !$skip['webp']); } else { $tools = ewww_image_optimizer_path_check(false, false, !$skip['gifsicle'], false, false, false); } // if gifsicle is disabled if (ewww_image_optimizer_get_option('ewww_image_optimizer_gif_level') == 0) { // return an appropriate message $result = __('GIF optimization is disabled', EWWW_IMAGE_OPTIMIZER_DOMAIN); // if utility checking is on, and gifsicle is not installed } elseif (!$skip['gifsicle'] && !$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'] . " -O3 --careful -o {$tempfile} " . ewww_image_optimizer_escapeshellarg($file)); // retrieve the filesize of the temporary GIF $new_size = ewww_image_optimizer_filesize($tempfile); // if the new GIF is smaller if ($orig_size > $new_size && $new_size != 0 && ewww_image_optimizer_mimetype($tempfile, 'i') == $type) { // 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; } // get the new filesize for the GIF $new_size = ewww_image_optimizer_filesize($file); // if conversion is ON and the GIF isn't animated if ($convert && !ewww_image_optimizer_is_animated($file)) { if (empty($new_size)) { $new_size = $orig_size; } // if optipng is enabled if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_optipng') && $tools['OPTIPNG']) { // retrieve the optipng optimization level $optipng_level = (int) 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')) && !$keep_metadata) { $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 = (int) 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)); } } // retrieve the filesize of the PNG $png_size = ewww_image_optimizer_filesize($pngfile); // if the new PNG is smaller than the original GIF if ($new_size > $png_size && $png_size != 0 && ewww_image_optimizer_mimetype($pngfile, 'i') == 'image/png') { // 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; // let webp know what we're dealing with now $type = 'image/png'; // normally this would be at the end of the section, but we only want to do webp if the image was successfully converted to a png ewww_image_optimizer_webp_create($file, $new_size, $type, $tools['WEBP'], $orig_size != $new_size); // successful conversion (for now), so we store the increment $converted = $filenum; } else { $converted = FALSE; if (is_file($pngfile)) { unlink($pngfile); } } } break; case 'application/pdf': if (empty($_REQUEST['ewww_force'])) { if ($results_msg = ewww_image_optimizer_check_table($file, $orig_size)) { return array($file, $results_msg, false, $original); } } if (ewww_image_optimizer_get_option('ewww_image_optimizer_pdf_level') > 0) { list($file, $converted, $result, $new_size) = ewww_image_optimizer_cloud_optimizer($file, $type); } 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); } // allow other plugins to run operations on the images after optimization. // NOTE: it is recommended to do any image modifications prior to optimization, otherwise you risk un-optimizing your images here. do_action('ewww_image_optimizer_post_optimization', $file, $type); // 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)) { // Set correct file permissions $stat = stat(dirname($file)); $perms = $stat['mode'] & 0666; //same permissions as parent folder, strip off the executable bits @chmod($file, $perms); $results_msg = ewww_image_optimizer_update_table($file, $new_size, $orig_size, $new); ewwwio_memory(__FUNCTION__); return array($file, $results_msg, $converted, $original); } ewwwio_memory(__FUNCTION__); // 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); }
function ewww_image_optimizer_aux_images_script($hook) { ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); // make sure we are being called from the proper page if ('ewww-image-optimizer-auto' !== $hook && empty($_REQUEST['ewww_scan'])) { return; } session_write_close(); global $wpdb; if (!empty($_REQUEST['ewww_force'])) { ewwwio_debug_message('forcing re-optimize: true'); } // initialize the $attachments variable for auxiliary images $attachments = null; // check the 'bulk resume' option $resume = get_option('ewww_image_optimizer_aux_resume'); // check if there is a previous bulk operation to resume if (!empty($resume)) { ewwwio_debug_message('resuming from where we left off, no scanning needed'); // retrieve the attachment IDs that have not been finished from the 'bulk attachments' option $attachments = get_option('ewww_image_optimizer_aux_attachments'); } else { ewwwio_debug_message('getting fresh list of files to optimize'); $attachments = array(); // collect a list of images from the current theme $child_path = get_stylesheet_directory(); $parent_path = get_template_directory(); $attachments = ewww_image_optimizer_image_scan($child_path); if ($child_path !== $parent_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($parent_path)); } if (!function_exists('is_plugin_active')) { // need to include the plugin library for the is_plugin_active function require_once ABSPATH . 'wp-admin/includes/plugin.php'; } // collect a list of images for buddypress if (is_plugin_active('buddypress/bp-loader.php') || is_plugin_active_for_network('buddypress/bp-loader.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/avatars'), ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/group-avatars')); } if (is_plugin_active('buddypress-activity-plus/bpfb.php') || is_plugin_active_for_network('buddypress-activity-plus/bpfb.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/bpfb')); } if (is_plugin_active('grand-media/grand-media.php') || is_plugin_active_for_network('grand-media/grand-media.php')) { // scan the grand media folder for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(WP_CONTENT_DIR . '/grand-media')); } if (is_plugin_active('wp-symposium/wp-symposium.php') || is_plugin_active_for_network('wp-symposium/wp-symposium.php')) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(get_option('symposium_img_path'))); } if (is_plugin_active('ml-slider/ml-slider.php') || is_plugin_active_for_network('ml-slider/ml-slider.php')) { $slide_paths = array(); $slides = $wpdb->get_col("\n\t\t\t\tSELECT wpposts.ID \n\t\t\t\tFROM {$wpdb->posts} wpposts \n\t\t\t\tINNER JOIN {$wpdb->term_relationships} term_relationships\n\t\t\t\t\t\tON wpposts.ID = term_relationships.object_id\n\t\t\t\tINNER JOIN {$wpdb->terms} wpterms \n\t\t\t\t\t\tON term_relationships.term_taxonomy_id = wpterms.term_id\n\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} term_taxonomy\n\t\t\t\t\t\tON wpterms.term_id = term_taxonomy.term_id\n\t\t\t\tWHERE \tterm_taxonomy.taxonomy = 'ml-slider'\n\t\t\t\t\tAND wpposts.post_type = 'attachment'\n\t\t\t\t"); foreach ($slides as $slide) { $backup_sizes = get_post_meta($slide, '_wp_attachment_backup_sizes', true); $type = get_post_meta($slide, 'ml-slider_type', true); $type = $type ? $type : 'image'; // backwards compatibility, fall back to 'image' if ($type === 'image') { foreach ($backup_sizes as $backup_size => $meta) { if (preg_match('/resized-/', $backup_size)) { $path = $meta['path']; $image_size = ewww_image_optimizer_filesize($path); if (!$image_size) { continue; } $already_optimized = ewww_image_optimizer_find_already_optimized($path); $mimetype = ewww_image_optimizer_mimetype($path, 'i'); if (preg_match('/^image\\/(jpeg|png|gif)/', $mimetype) && empty($already_optimized)) { $slide_paths[] = $path; } } } } } $attachments = array_merge($attachments, $slide_paths); } // collect a list of images in auxiliary folders provided by user if ($aux_paths = ewww_image_optimizer_get_option('ewww_image_optimizer_aux_paths')) { foreach ($aux_paths as $aux_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($aux_path)); } } // scan images in two most recent media library folders if the option is enabled, and this is a scheduled optimization if ('ewww-image-optimizer-auto' == $hook && ewww_image_optimizer_get_option('ewww_image_optimizer_include_media_paths')) { // retrieve the location of the wordpress upload folder $upload_dir = wp_upload_dir(); // retrieve the path of the upload folder $upload_path = $upload_dir['basedir']; $this_month = date('m'); $this_year = date('Y'); $attachments = array_merge($attachments, ewww_image_optimizer_image_scan("{$upload_path}/{$this_year}/{$this_month}/")); if (class_exists('DateTime')) { $date = new DateTime(); $date->sub(new DateInterval('P1M')); $last_year = $date->format('Y'); $last_month = $date->format('m'); $attachments = array_merge($attachments, ewww_image_optimizer_image_scan("{$upload_path}/{$last_year}/{$last_month}/")); } } // store the filenames we retrieved in the 'bulk_attachments' option so we can keep track of our progress in the database update_option('ewww_image_optimizer_aux_attachments', $attachments); ewwwio_debug_message('found ' . count($attachments) . ' images to optimize while scanning'); } ewww_image_optimizer_debug_log(); if (!empty($_REQUEST['ewww_scan'])) { echo count($attachments); ewwwio_memory(__FUNCTION__); die; } else { ewwwio_memory(__FUNCTION__); return; } }
function ewww_image_optimizer_aux_images_script($hook) { // make sure we are being called from the proper page if ('ewww-image-optimizer-auto' !== $hook && empty($_REQUEST['scan'])) { return; } global $ewww_debug; global $wpdb; $ewww_debug .= "<b>ewww_image_optimizer_aux_images_script()</b><br>"; // initialize the $attachments variable for auxiliary images $attachments = null; // check the 'bulk resume' option $resume = get_option('ewww_image_optimizer_aux_resume'); // check if there is a previous bulk operation to resume if (!empty($resume)) { // retrieve the attachment IDs that have not been finished from the 'bulk attachments' option $attachments = get_option('ewww_image_optimizer_aux_attachments'); } else { // collect a list of images from the current theme $child_path = get_stylesheet_directory(); $parent_path = get_template_directory(); $attachments = ewww_image_optimizer_image_scan($child_path); if ($child_path !== $parent_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($parent_path)); } // collect a list of images in auxiliary folders provided by user if ($aux_paths = ewww_image_optimizer_get_option('ewww_image_optimizer_aux_paths')) { foreach ($aux_paths as $aux_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($aux_path)); } } // collect a list of images for buddypress if (is_plugin_active('buddypress/bp-loader.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('buddypress/bp-loader.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/avatars'), ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/group-avatars')); } if (is_plugin_active('buddypress-activity-plus/bpfb.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('buddypress-activity-plus/bpfb.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/bpfb')); } if (is_plugin_active('wp-symposium/wp-symposium.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('wp-symposium/wp-symposium.php')) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(get_option('symposium_img_path'))); } if (is_plugin_active('ml-slider/ml-slider.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('ml-slider/ml-slider.php')) { $slide_paths = array(); $sliders = get_posts(array('numberposts' => -1, 'post_type' => 'ml-slider', 'post_status' => 'any', 'fields' => 'ids')); foreach ($sliders as $slider) { $slides = get_posts(array('numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_type' => 'attachment', 'post_status' => 'inherit', 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'ml-slider', 'field' => 'slug', 'terms' => $slider)))); foreach ($slides as $slide) { $backup_sizes = get_post_meta($slide, '_wp_attachment_backup_sizes', true); $type = get_post_meta($slide, 'ml-slider_type', true); $type = $type ? $type : 'image'; // backwards compatibility, fall back to 'image' if ($type === 'image') { foreach ($backup_sizes as $backup_size => $meta) { if (preg_match('/resized-/', $backup_size)) { $path = $meta['path']; $image_size = filesize($path); $query = $wpdb->prepare("SELECT id FROM {$wpdb->ewwwio_images} WHERE BINARY path LIKE %s AND image_size LIKE '{$image_size}'", $path); $already_optimized = $wpdb->get_results($query); $mimetype = ewww_image_optimizer_mimetype($path, 'i'); if (preg_match('/^image\\/(jpeg|png|gif)/', $mimetype) && empty($already_optimized)) { $slide_paths[] = $path; } } } } } } $attachments = array_merge($attachments, $slide_paths); } // store the filenames we retrieved in the 'bulk_attachments' option so we can keep track of our progress in the database update_option('ewww_image_optimizer_aux_attachments', $attachments); } ewww_image_optimizer_debug_log(); // submit a couple variables to the javascript to work with $attachments = json_encode($attachments); if (!empty($_REQUEST['scan'])) { if (empty($attachments)) { _e('Nothing to optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN); } else { echo $attachments; } die; } else { return; } }
function ewww_image_optimizer_aux_images_script($hook) { // make sure we are being called from the proper page if ('ewww-image-optimizer-auto' !== $hook && empty($_REQUEST['ewww_scan'])) { return; } global $ewww_debug; global $wpdb; $ewww_debug .= "<b>ewww_image_optimizer_aux_images_script()</b><br>"; if (!empty($_REQUEST['ewww_force'])) { $ewww_debug .= "forcing re-optimize: true<br>"; } // initialize the $attachments variable for auxiliary images $attachments = null; // check the 'bulk resume' option $resume = get_option('ewww_image_optimizer_aux_resume'); // check if there is a previous bulk operation to resume if (!empty($resume)) { // retrieve the attachment IDs that have not been finished from the 'bulk attachments' option $attachments = get_option('ewww_image_optimizer_aux_attachments'); } else { $attachments = array(); // collect a list of images from the current theme $child_path = get_stylesheet_directory(); $parent_path = get_template_directory(); $attachments = ewww_image_optimizer_image_scan($child_path); if ($child_path !== $parent_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($parent_path)); } if (!function_exists('is_plugin_active')) { // need to include the plugin library for the is_plugin_active function require_once ABSPATH . 'wp-admin/includes/plugin.php'; } // collect a list of images for buddypress if (is_plugin_active('buddypress/bp-loader.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('buddypress/bp-loader.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/avatars'), ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/group-avatars')); } if (is_plugin_active('buddypress-activity-plus/bpfb.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('buddypress-activity-plus/bpfb.php')) { // get the value of the wordpress upload directory $upload_dir = wp_upload_dir(); // scan the 'avatars' and 'group-avatars' folders for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($upload_dir['basedir'] . '/bpfb')); } if (is_plugin_active('grand-media/grand-media.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('grand-media/grand-media.php')) { // scan the grand media folder for images $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(WP_CONTENT_DIR . '/grand-media')); } if (is_plugin_active('wp-symposium/wp-symposium.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('wp-symposium/wp-symposium.php')) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan(get_option('symposium_img_path'))); } if (is_plugin_active('ml-slider/ml-slider.php') || function_exists('is_plugin_active_for_network') && is_plugin_active_for_network('ml-slider/ml-slider.php')) { $slide_paths = array(); $sliders = get_posts(array('numberposts' => -1, 'post_type' => 'ml-slider', 'post_status' => 'any', 'fields' => 'ids')); foreach ($sliders as $slider) { $slides = get_posts(array('numberposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', 'post_type' => 'attachment', 'post_status' => 'inherit', 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'ml-slider', 'field' => 'slug', 'terms' => $slider)))); foreach ($slides as $slide) { $backup_sizes = get_post_meta($slide, '_wp_attachment_backup_sizes', true); $type = get_post_meta($slide, 'ml-slider_type', true); $type = $type ? $type : 'image'; // backwards compatibility, fall back to 'image' if ($type === 'image') { foreach ($backup_sizes as $backup_size => $meta) { if (preg_match('/resized-/', $backup_size)) { $path = $meta['path']; $image_size = filesize($path); $query = $wpdb->prepare("SELECT id FROM {$wpdb->ewwwio_images} WHERE path LIKE %s AND image_size LIKE '{$image_size}'", $path); $optimized_query = $wpdb->get_results($query, ARRAY_A); if (!empty($optimized_query)) { foreach ($optimized_query as $image) { if ($image['path'] != $path) { $ewww_debug .= "{$image['path']} does not match {$path}, continuing our search<br>"; } else { $already_optimized = $image; } } } $mimetype = ewww_image_optimizer_mimetype($path, 'i'); if (preg_match('/^image\\/(jpeg|png|gif)/', $mimetype) && empty($already_optimized)) { $slide_paths[] = $path; } } } } } } $attachments = array_merge($attachments, $slide_paths); } // collect a list of images in auxiliary folders provided by user if ($aux_paths = ewww_image_optimizer_get_option('ewww_image_optimizer_aux_paths')) { foreach ($aux_paths as $aux_path) { $attachments = array_merge($attachments, ewww_image_optimizer_image_scan($aux_path)); } } // scan images in two most recent media library folders if the option is enabled, and this is a scheduled optimization if ('ewww-image-optimizer-auto' == $hook && ewww_image_optimizer_get_option('ewww_image_optimizer_include_media_paths')) { // retrieve the location of the wordpress upload folder $upload_dir = wp_upload_dir(); // retrieve the path of the upload folder $upload_path = $upload_dir['basedir']; $this_month = date('m'); $this_year = date('Y'); $attachments = array_merge($attachments, ewww_image_optimizer_image_scan("{$upload_path}/{$this_year}/{$this_month}/")); if (class_exists('DateTime')) { $date = new DateTime(); $date->sub(new DateInterval('P1M')); $last_year = $date->format('Y'); $last_month = $date->format('m'); $attachments = array_merge($attachments, ewww_image_optimizer_image_scan("{$upload_path}/{$last_year}/{$last_month}/")); } } // store the filenames we retrieved in the 'bulk_attachments' option so we can keep track of our progress in the database update_option('ewww_image_optimizer_aux_attachments', $attachments); } ewww_image_optimizer_debug_log(); // submit a couple variables to the javascript to work with $attachments = json_encode($attachments); if (!empty($_REQUEST['ewww_scan'])) { if (empty($attachments)) { _e('Nothing to optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN); } else { echo $attachments; } ewwwio_memory(__FUNCTION__); die; } else { ewwwio_memory(__FUNCTION__); return; } }
function ewww_manage_image_custom_column( $column_name, $id ) { // once we've found our custom column if( $column_name == 'ewww_image_optimizer' ) { // get the metadata for the image $meta = new nggMeta( $id ); // get the optimization status for the image $status = $meta->get_META( 'ewww_image_optimizer' ); $msg = ''; // get the file path of the image $file_path = $meta->image->imagePath; // get the mimetype of the image $type = ewww_image_optimizer_mimetype($file_path, 'i'); // retrieve the human-readable filesize of the image $file_size = size_format( ewww_image_optimizer_filesize( $file_path ), 2 ); $file_size = str_replace( 'B ', 'B', $file_size ); $valid = true; // check to see if we have a tool to handle the mimetype detected $skip = ewww_image_optimizer_skip_tools(); switch ( $type ) { case 'image/jpeg': // if jpegtran is missing, tell the user if( ! EWWW_IMAGE_OPTIMIZER_JPEGTRAN && ! $skip['jpegtran'] ) { $valid = false; $msg = '<br>' . wp_kses( sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'), array( 'em' => array() ) ); } break; case 'image/png': // if the PNG tools are missing, tell the user if( ! EWWW_IMAGE_OPTIMIZER_PNGOUT && ! EWWW_IMAGE_OPTIMIZER_OPTIPNG && ! $skip['optipng'] && ! $skip['pngout'] ) { $valid = false; $msg = '<br>' . wp_kses( sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'), array( 'em' => array() ) ); } break; case 'image/gif': // if gifsicle is missing, tell the user if( ! EWWW_IMAGE_OPTIMIZER_GIFSICLE && ! $skip['gifsicle'] ) { $valid = false; $msg = '<br>' . wp_kses( sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'), array( 'em' => array() ) ); } break; default: $valid = false; } // file isn't in a format we can work with, we don't work with strangers if ( $valid == false ) { echo esc_html__( 'Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN ) . $msg; return; } $ewww_manual_nonce = wp_create_nonce( "ewww-manual-" . $id ); // if we have a valid status, display it, the image size, and give a re-optimize link if ( ! empty ( $status ) ) { echo esc_html( $status ); echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if ( current_user_can( apply_filters( 'ewww_image_optimizer_manual_permissions', '' ) ) ) { printf("<br><a href=\"admin.php?action=ewww_ngg_manual&ewww_manual_nonce=$ewww_manual_nonce&ewww_force=1&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } elseif ( get_transient( 'ewwwio-background-in-progress-ngg-' . $id ) ) { esc_html_e( 'In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN ); // otherwise, give the image size, and a link to optimize right now } else { esc_html_e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if ( current_user_can( apply_filters( 'ewww_image_optimizer_manual_permissions', '' ) ) ) { printf("<br><a href=\"admin.php?action=ewww_ngg_manual&ewww_manual_nonce=$ewww_manual_nonce&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } } }
/** * Print column data for optimizer results in the media library using * the `manage_media_custom_column` hook. */ function ewww_image_optimizer_custom_column($column_name, $id) { ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); // once we get to the EWWW IO custom column if ($column_name == 'ewww-image-optimizer') { // retrieve the metadata $meta = wp_get_attachment_metadata($id); if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) { $print_meta = print_r($meta, TRUE); $print_meta = preg_replace(array('/ /', '/\\n+/'), array(' ', '<br />'), $print_meta); echo '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>'; } // TODO: make sure meta is an array $ewww_cdn = false; if (!empty($meta['cloudinary'])) { esc_html_e('Cloudinary image', EWWW_IMAGE_OPTIMIZER_DOMAIN); return; } if (class_exists('WindowsAzureStorageUtil') && !empty($meta['url'])) { esc_html_e('Azure Storage image', EWWW_IMAGE_OPTIMIZER_DOMAIN); $ewww_cdn = true; } if (class_exists('Amazon_S3_And_CloudFront') && preg_match('/^(http|s3)\\w*:/', get_attached_file($id))) { esc_html_e('Amazon S3 image', EWWW_IMAGE_OPTIMIZER_DOMAIN); $ewww_cdn = true; } list($file_path, $upload_path) = ewww_image_optimizer_attachment_path($meta, $id); // if the file does not exist if (empty($file_path) && !$ewww_cdn) { esc_html_e('Could not retrieve file path.', EWWW_IMAGE_OPTIMIZER_DOMAIN); ewww_image_optimizer_debug_log(); return; } $msg = ''; $convert_desc = ''; $convert_link = ''; if ($ewww_cdn) { $type = get_post_mime_type($id); } else { // retrieve the mimetype of the attachment $type = ewww_image_optimizer_mimetype($file_path, 'i'); // get a human readable filesize $file_size = size_format(filesize($file_path), 2); $file_size = preg_replace('/\\.00 B /', ' B', $file_size); } $skip = ewww_image_optimizer_skip_tools(); // run the appropriate code based on the mimetype switch ($type) { case 'image/jpeg': // if jpegtran is missing, tell them that if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !$skip['jpegtran']) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'), array('em' => array())); } else { $convert_link = esc_html__('JPG to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN); $convert_desc = esc_attr__('WARNING: Removes metadata. Requires GD or ImageMagick. PNG is generally much better than JPG for logos and other images with a limited range of colors.', EWWW_IMAGE_OPTIMIZER_DOMAIN); } break; case 'image/png': // if pngout and optipng are missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !$skip['optipng'] && !$skip['pngout']) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'), array('em' => array())); } else { $convert_link = esc_html__('PNG to JPG', EWWW_IMAGE_OPTIMIZER_DOMAIN); $convert_desc = esc_attr__('WARNING: This is not a lossless conversion and requires GD or ImageMagick. JPG is much better than PNG for photographic use because it compresses the image and discards data. Transparent images will only be converted if a background color has been set.', EWWW_IMAGE_OPTIMIZER_DOMAIN); } break; case 'image/gif': // if gifsicle is missing, tell the user if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !$skip['gifsicle']) { $valid = false; $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'), array('em' => array())); } else { $convert_link = esc_html__('GIF to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN); $convert_desc = esc_attr__('PNG is generally better than GIF, but does not support animation. Animated images will not be converted.', EWWW_IMAGE_OPTIMIZER_DOMAIN); } break; case 'application/pdf': $convert_desc = ''; break; default: // not a supported mimetype esc_html_e('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN); ewww_image_optimizer_debug_log(); return; } $ewww_manual_nonce = wp_create_nonce("ewww-manual-{$id}"); if ($ewww_cdn) { // if the optimizer metadata exists if (!empty($meta['ewww_image_optimizer'])) { // output the optimizer results echo "<br>" . esc_html($meta['ewww_image_optimizer']); if (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { // output a link to re-optimize manually printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&ewww_manual_nonce={$ewww_manual_nonce}&ewww_force=1&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } elseif (get_transient('ewwwio-background-in-progress-' . $id)) { esc_html_e('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN); } elseif (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { // and give the user the option to optimize the image right now printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&ewww_manual_nonce={$ewww_manual_nonce}&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } return; } // if the optimizer metadata exists if (!empty($meta['ewww_image_optimizer'])) { // output the optimizer results echo esc_html($meta['ewww_image_optimizer']); // output the filesize echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { // output a link to re-optimize manually printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&ewww_manual_nonce={$ewww_manual_nonce}&ewww_force=1&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id) && !empty($convert_desc)) { echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&ewww_manual_nonce={$ewww_manual_nonce}&ewww_attachment_ID={$id}&ewww_convert=1&ewww_force=1'>{$convert_link}</a>"; } } else { echo $msg; } $restorable = false; if (!empty($meta['converted'])) { if (!empty($meta['orig_file']) && file_exists($meta['orig_file'])) { $restorable = true; } } if (isset($meta['sizes'])) { // meta sizes don't contain a path, so we calculate one $base_dir = trailingslashit(dirname($file_path)); foreach ($meta['sizes'] as $size => $data) { if (!empty($data['converted'])) { if (!empty($data['orig_file']) && file_exists($base_dir . $data['orig_file'])) { $restorable = true; } } } } if ($restorable && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_restore&ewww_manual_nonce={$ewww_manual_nonce}&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Restore original', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } // link to webp upgrade script $oldwebpfile = preg_replace('/\\.\\w+$/', '.webp', $file_path); if (file_exists($oldwebpfile) && current_user_can(apply_filters('ewww_image_optimizer_admin_permissions', ''))) { echo "<br><a href='options.php?page=ewww-image-optimizer-webp-migrate'>" . esc_html__('Run WebP upgrade', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "</a>"; } // determine filepath for webp $webpfile = $file_path . '.webp'; $webp_size = ewww_image_optimizer_filesize($webpfile); if ($webp_size) { $webp_size = size_format($webp_size, 2); $webpurl = esc_url(wp_get_attachment_url($id) . '.webp'); // get a human readable filesize $webp_size = preg_replace('/\\.00 B /', ' B', $webp_size); echo "<br>WebP: <a href='{$webpurl}'>{$webp_size}</a>"; } } elseif (get_transient('ewwwio-background-in-progress-' . $id)) { esc_html_e('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN); } else { // otherwise, this must be an image we haven't processed esc_html_e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); // tell them the filesize echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size); if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) { // and give the user the option to optimize the image right now printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&ewww_manual_nonce={$ewww_manual_nonce}&ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id) && !empty($convert_desc)) { echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&ewww_manual_nonce={$ewww_manual_nonce}&ewww_attachment_ID={$id}&ewww_convert=1&ewww_force=1'>{$convert_link}</a>"; } } else { echo $msg; } } } ewwwio_memory(__FUNCTION__); }
/** * Print column data for optimizer results in the media library using * the `manage_media_custom_column` hook. */ function ewww_image_optimizer_custom_column($column_name, $id) { global $ewww_debug; $ewww_debug = "{$ewww_debug} <b>ewww_image_optimizer_custom_column()</b><br>"; // once we get to the EWWW IO custom column if ($column_name == 'ewww-image-optimizer') { // retrieve the metadata $meta = wp_get_attachment_metadata($id); // if the filepath isn't set in the metadata if (empty($meta['file'])) { if (isset($meta['file'])) { unset($meta['file']); if (strpos($meta['ewww_image_optimizer'], 'Could not find') === 0) { unset($meta['ewww_image_optimizer']); } wp_update_attachment_metadata($id, $meta); } echo 'Metadata is missing file path.'; //print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg; return; } // retrieve the filepath from the metadata $file_path = $meta['file']; // retrieve the wordpress upload folder $upload_dir = wp_upload_dir(); // retrieve the wordpress upload folder path $upload_path = trailingslashit($upload_dir['basedir']); // if the path given is not the absolute path if (FALSE === file_exists($file_path)) { // find the absolute path $file_path = $upload_path . $file_path; } $msg = ''; $type = ewww_image_optimizer_mimetype($file_path, 'i'); // get a human readable filesize $file_size = ewww_image_optimizer_format_bytes(filesize($file_path)); // initialize $valid $valid = true; // run the appropriate code based on the mimetype switch ($type) { case 'image/jpeg': // if jpegtran is missing, tell them that if (EWWW_IMAGE_OPTIMIZER_JPEGTRAN == false) { $valid = false; $msg = '<br>' . __('<em>jpegtran</em> is missing'); } else { $convert_link = 'JPG to PNG'; $class_type = 'jpg'; $convert_desc = "WARNING: Removes metadata. Requires GD or ImageMagick. PNG is generally much better than JPG for logos and other images with a limited range of colors."; } break; case 'image/png': // if pngout and optipng are missing, tell the user if (EWWW_IMAGE_OPTIMIZER_PNGOUT == false && EWWW_IMAGE_OPTIMIZER_OPTIPNG == false) { $valid = false; $msg = '<br>' . __('<em>optipng/pngout</em> is missing'); } else { $convert_link = 'PNG to JPG'; $class_type = 'png'; $convert_desc = "WARNING: This is not a lossless conversion and requires GD or ImageMagick. JPG is much better than PNG for photographic use because it compresses the image and discards data. Transparent images will only be converted if a background color has been set."; } break; case 'image/gif': // if gifsicle is missing, tell the user if (EWWW_IMAGE_OPTIMIZER_GIFSICLE == false) { $valid = false; $msg = '<br>' . __('<em>gifsicle</em> is missing'); } else { $convert_link = 'GIF to PNG'; $class_type = 'gif'; $convert_desc = "PNG is generally better than GIF, but does not support animation. Animated images will not be converted."; } break; default: // not a supported mimetype $valid = false; } // if this is an unsupported filetype, tell the user we don't work with strangers if ($valid == false) { print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg; return; } // if the optimizer metadata exists if (isset($meta['ewww_image_optimizer']) && !empty($meta['ewww_image_optimizer'])) { // output the optimizer results print $meta['ewww_image_optimizer']; // output the filesize print "<br>Image Size: {$file_size}"; // output a link to re-optimize manually printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual&attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); if (!get_site_option('ewww_image_optimizer_disable_convert_links')) { echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual&attachment_ID={$id}&convert=1'>{$convert_link}</a>"; } $restorable = false; if (!empty($meta['converted'])) { if (!empty($meta['orig_file']) && file_exists($meta['orig_file'])) { $restorable = true; } } if (isset($meta['sizes'])) { // meta sizes don't contain a path, so we calculate one $base_dir = dirname($file_path) . '/'; foreach ($meta['sizes'] as $size => $data) { if (!empty($data['converted'])) { if (!empty($data['orig_file']) && file_exists($base_dir . $data['orig_file'])) { $restorable = true; } } } } if ($restorable) { printf("<br><a href=\"admin.php?action=ewww_image_optimizer_restore&attachment_ID=%d\">%s</a>", $id, __('Restore original', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } else { // otherwise, this must be an image we haven't processed print __('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); // tell them the filesize print "<br>Image Size: {$file_size}"; // and give the user the option to optimize the image right now printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual&attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); if (!get_site_option('ewww_image_optimizer_disable_convert_links')) { echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual&attachment_ID={$id}&convert=1'>{$convert_link}</a>"; } } } }
function ewww_manage_image_custom_column($column_name, $id) { // check to make sure we're outputing our custom column if ($column_name == 'ewww_image_optimizer') { // get the metadata $meta = new flagMeta($id); // grab the image status from the meta $status = $meta->get_META('ewww_image_optimizer'); $msg = ''; // get the image path from the meta $file_path = $meta->image->imagePath; // get the mimetype $type = ewww_image_optimizer_mimetype($file_path, 'i'); // get the file size $file_size = ewww_image_optimizer_format_bytes(filesize($file_path)); $valid = true; // if we don't have a valid tool for the image type, output the appropriate message switch ($type) { case 'image/jpeg': if (EWWW_IMAGE_OPTIMIZER_JPEGTRAN == false) { $valid = false; $msg = '<br>' . __('<em>jpegtran</em> is missing'); } break; case 'image/png': if (EWWW_IMAGE_OPTIMIZER_PNGOUT == false && EWWW_IMAGE_OPTIMIZER_OPTIPNG == false) { $valid = false; $msg = '<br>' . __('<em>optipng/pngout</em> is missing'); } break; case 'image/gif': if (EWWW_IMAGE_OPTIMIZER_GIFSICLE == false) { $valid = false; $msg = '<br>' . __('<em>gifsicle</em> is missing'); } break; default: $valid = false; } // let user know if the file type is unsupported if ($valid == false) { print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg; return; } // output the image status if we know it if (!empty($status)) { echo $status; print "<br>Image Size: {$file_size}"; printf("<br><a href=\"admin.php?action=ewww_flag_manual&attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN)); // otherwise, tell the user that they can optimize the image now } else { print __('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN); print "<br>Image Size: {$file_size}"; printf("<br><a href=\"admin.php?action=ewww_flag_manual&attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } } }