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; }
/** * Read the image paths from an attachment's meta data and process each image * with ewww_image_optimizer(). * * This method also adds a `ewww_image_optimizer` meta key for use in the media library * and may add a 'converted' and 'orig_file' key if conversion is enabled. * * Called after `wp_generate_attachment_metadata` is completed. */ function ewww_image_optimizer_resize_from_meta_data($meta, $ID = null, $log = true, $background_new = false) { ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); if (!is_array($meta) && empty($meta)) { $meta = array(); } elseif (!is_array($meta)) { if (is_string($meta) && is_int($ID) && 'processing' == $meta) { ewwwio_debug_message("attempting to rebuild attachment meta for {$ID}"); $new_meta = ewww_image_optimizer_rebuild_meta($ID); if (!is_array($new_meta)) { ewwwio_debug_message('attempt to rebuild attachment meta failed'); return $meta; } else { $meta = $new_meta; } } else { ewwwio_debug_message('attachment meta is not a usable array'); return $meta; } } elseif (is_array($meta) && !empty($meta[0]) && 'processing' == $meta[0]) { ewwwio_debug_message("attempting to rebuild attachment meta for {$ID}"); $new_meta = ewww_image_optimizer_rebuild_meta($ID); if (!is_array($new_meta)) { ewwwio_debug_message('attempt to rebuild attachment meta failed'); return $meta; } else { $meta = $new_meta; } } global $wpdb; global $ewww_defer; global $ewww_new_image; $gallery_type = 1; ewwwio_debug_message("attachment id: {$ID}"); session_write_close(); //if ( ! metadata_exists( 'post', $ID, '_wp_attachment_metadata' ) ) { if (!empty($ewww_new_image)) { ewwwio_debug_message('this is a newly uploaded image with no metadata yet'); $new_image = true; } else { ewwwio_debug_message('this image already has metadata, so it is not new'); $new_image = false; } list($file_path, $upload_path) = ewww_image_optimizer_attachment_path($meta, $ID); // if the attachment has been uploaded via the image store plugin if ('ims_image' == get_post_type($ID)) { $gallery_type = 6; } if (!$new_image && class_exists('Amazon_S3_And_CloudFront') && strpos($file_path, 's3') === 0) { ewww_image_optimizer_check_table_as3cf($meta, $ID, $file_path); } // if the local file is missing and we have valid metadata, see if we can fetch via CDN if (!is_file($file_path) || strpos($file_path, 's3') === 0) { $file_path = ewww_image_optimizer_remote_fetch($ID, $meta); if (!$file_path) { ewwwio_debug_message('could not retrieve path'); $meta['ewww_image_optimizer'] = __('Could not find image', EWWW_IMAGE_OPTIMIZER_DOMAIN); return $meta; } } ewwwio_debug_message("retrieved file path: {$file_path}"); $type = ewww_image_optimizer_mimetype($file_path, 'i'); $supported_types = array('image/jpeg', 'image/png', 'image/gif', 'application/pdf'); if (!in_array($type, $supported_types)) { ewwwio_debug_message("mimetype not supported: {$ID}"); return $meta; } // see if this is a new image and Imsanity resized it (which means it could be already optimized) if (!empty($new_image) && function_exists('imsanity_get_max_width_height')) { list($maxW, $maxH) = imsanity_get_max_width_height(IMSANITY_SOURCE_LIBRARY); list($oldW, $oldH) = getimagesize($file_path); list($newW, $newH) = wp_constrain_dimensions($oldW, $oldH, $maxW, $maxH); $path_parts = pathinfo($file_path); $imsanity_path = trailingslashit($path_parts['dirname']) . $path_parts['filename'] . '-' . $newW . 'x' . $newH . '.' . $path_parts['extension']; ewwwio_debug_message("imsanity path: {$imsanity_path}"); $image_size = ewww_image_optimizer_filesize($file_path); $already_optimized = ewww_image_optimizer_find_already_optimized($imsanity_path); if (is_array($already_optimized)) { ewwwio_debug_message("updating existing record, path: {$file_path}, size: " . $image_size); // store info on the current image for future reference $wpdb->update($wpdb->ewwwio_images, array('path' => $file_path), array('id' => $already_optimized['id'])); } } if ((!empty($new_image) || ewww_image_optimizer_get_option('ewww_image_optimizer_resize_existing')) && !function_exists('imsanity_get_max_width_height')) { $new_dimensions = ewww_image_optimizer_resize_upload($file_path); if (is_array($new_dimensions)) { $meta['width'] = $new_dimensions[0]; $meta['height'] = $new_dimensions[1]; } } if ($ewww_defer && !ewww_image_optimizer_detect_wpsf_location_lock()) { add_filter('http_headers_useragent', 'ewww_image_optimizer_cloud_useragent', PHP_INT_MAX); global $ewwwio_media_background; if (!class_exists('WP_Background_Process')) { require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'background.php'; } if (!is_object($ewwwio_media_background)) { $ewwwio_media_background = new EWWWIO_Media_Background_Process(); } ewwwio_debug_message("backgrounding optimization for {$ID}"); $ewwwio_media_background->push_to_queue(array('id' => $ID, 'new' => $new_image, 'type' => $type)); $ewwwio_media_background->save()->dispatch(); set_transient('ewwwio-background-in-progress-' . $ID, true, 24 * HOUR_IN_SECONDS); ewww_image_optimizer_debug_log(); return $meta; } if ($background_new) { $new_image = true; } if (ewww_image_optimizer_test_parallel_opt($ID) && $type != 'application/pdf') { ewwwio_debug_message('running in parallel'); $parallel_opt = true; } else { ewwwio_debug_message('running in sequence'); $parallel_opt = false; } $parallel_sizes = array(); if ($parallel_opt) { touch($file_path . '.processing'); if (!empty($_REQUEST['ewww_force'])) { $force = true; } else { $force = false; } add_filter('http_headers_useragent', 'ewww_image_optimizer_cloud_useragent', PHP_INT_MAX); global $ewwwio_async_optimize_media; if (!class_exists('WP_Background_Process')) { require_once EWWW_IMAGE_OPTIMIZER_PLUGIN_PATH . 'background.php'; } if (!is_object($ewwwio_async_optimize_media)) { $ewwwio_async_optimize_media = new EWWWIO_Async_Request(); } $async_path = str_replace($upload_path, '', $file_path); $ewwwio_async_optimize_media->data(array('ewwwio_path' => $async_path, 'ewwwio_size' => 'full', 'ewww_force' => $force))->dispatch(); $parallel_sizes['full'] = $file_path; } else { list($file, $msg, $conv, $original) = ewww_image_optimizer($file_path, $gallery_type, false, $new_image, true); // update the optimization results in the metadata $meta['ewww_image_optimizer'] = $msg; if ($file === false) { return $meta; } $meta['file'] = str_replace($upload_path, '', $file); // if the file was converted if ($conv) { // update the filename in the metadata $new_file = substr($meta['file'], 0, -3); // change extension $new_ext = substr($file, -3); $meta['file'] = $new_file . $new_ext; ewwwio_debug_message('image was converted'); // if we don't already have the update attachment filter if (FALSE === has_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment')) { // add the update attachment filter add_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment', 10, 2); } // store the conversion status in the metadata $meta['converted'] = 1; // store the old filename in the database $meta['orig_file'] = $original; } else { remove_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment', 10); } } ewww_image_optimizer_hidpi_optimize($file_path); // resized versions, so we can continue if (isset($meta['sizes'])) { $disabled_sizes = ewww_image_optimizer_get_option('ewww_image_optimizer_disable_resizes_opt'); // ewwwio_debug_message( "disabled sizes: " . print_r( $disabled_sizes, true ) ); ewwwio_debug_message('processing resizes'); // meta sizes don't contain a path, so we calculate one if ($gallery_type === 6) { $base_ims_dir = trailingslashit(dirname($file_path)) . '_resized/'; } $base_dir = trailingslashit(dirname($file_path)); // process each resized version $processed = array(); foreach ($meta['sizes'] as $size => $data) { ewwwio_debug_message("processing size: {$size}"); if (strpos($size, 'webp') === 0) { continue; } if (!empty($disabled_sizes[$size])) { continue; } if ($gallery_type === 6) { $base_dir = dirname($file_path) . '/'; $image_path = $base_dir . $data['file']; $ims_path = $base_ims_dir . $data['file']; if (file_exists($ims_path)) { ewwwio_debug_message('ims resize already exists, wahoo'); ewwwio_debug_message("ims path: {$ims_path}"); $image_size = ewww_image_optimizer_filesize($ims_path); $already_optimized = ewww_image_optimizer_find_already_optimized($image_path); if (is_array($already_optimized)) { ewwwio_debug_message("updating existing record, path: {$ims_path}, size: " . $image_size); // store info on the current image for future reference $wpdb->update($wpdb->ewwwio_images, array('path' => $ims_path), array('id' => $already_optimized['id'])); } $base_dir = $base_ims_dir; } } // initialize $dup_size $dup_size = false; // check through all the sizes we've processed so far foreach ($processed as $proc => $scan) { // if a previous resize had identical dimensions if ($scan['height'] == $data['height'] && $scan['width'] == $data['width']) { // found a duplicate resize $dup_size = true; // point this resize at the same image as the previous one $meta['sizes'][$size]['file'] = $meta['sizes'][$proc]['file']; // and tell the user we didn't do any further optimization $meta['sizes'][$size]['ewww_image_optimizer'] = __('No savings', EWWW_IMAGE_OPTIMIZER_DOMAIN); } } // if this is a unique size if (!$dup_size) { $resize_path = $base_dir . $data['file']; // run the optimization and store the results if ($parallel_opt) { touch($resize_path . '.processing'); $async_path = str_replace($upload_path, '', $resize_path); add_filter('http_headers_useragent', 'ewww_image_optimizer_cloud_useragent', PHP_INT_MAX); $ewwwio_async_optimize_media->data(array('ewwwio_path' => $async_path, 'ewwwio_size' => $size, 'ewww_force' => $force))->dispatch(); $parallel_sizes[$size] = $resize_path; } else { list($optimized_file, $results, $resize_conv, $original) = ewww_image_optimizer($resize_path, $gallery_type, $conv, $new_image); // if the resize was converted, store the result and the original filename in the metadata for later recovery if ($resize_conv) { // if we don't already have the update attachment filter if (FALSE === has_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment')) { // add the update attachment filter add_filter('wp_update_attachment_metadata', 'ewww_image_optimizer_update_attachment', 10, 2); } $meta['sizes'][$size]['converted'] = 1; $meta['sizes'][$size]['orig_file'] = str_replace($base_dir, '', $original); ewwwio_debug_message("original filename: {$original}"); $meta['sizes'][$size]['real_orig_file'] = str_replace($base_dir, '', $resize_path); ewwwio_debug_message("resize path: {$resize_path}"); } if ($optimized_file !== false) { // update the filename $meta['sizes'][$size]['file'] = str_replace($base_dir, '', $optimized_file); } // update the optimization results $meta['sizes'][$size]['ewww_image_optimizer'] = $results; } // optimize retina images, if they exist if (function_exists('wr2x_get_retina') && ($retina_path = wr2x_get_retina($resize_path))) { if ($parallel_opt) { add_filter('http_headers_useragent', 'ewww_image_optimizer_cloud_useragent', PHP_INT_MAX); $async_path = str_replace($upload_path, '', $retina_path); $ewwwio_async_optimize_media->data(array('path' => $async_path, 'size' => '', 'ewww_force' => $force))->dispatch(); } else { ewww_image_optimizer($retina_path); } } else { ewww_image_optimizer_hidpi_optimize($resize_path); } } // store info on the sizes we've processed, so we can check the list for duplicate sizes $processed[$size]['width'] = $data['width']; $processed[$size]['height'] = $data['height']; } } if (!empty($new_dimensions)) { $prev_string = " - " . __('Previously Optimized', EWWW_IMAGE_OPTIMIZER_DOMAIN); $meta['ewww_image_optimizer'] = preg_replace("/{$prev_string}/", '', $meta['ewww_image_optimizer']); } // process size from a custom theme if (isset($meta['image_meta']['resized_images'])) { $imagemeta_resize_pathinfo = pathinfo($file_path); $imagemeta_resize_path = ''; foreach ($meta['image_meta']['resized_images'] as $imagemeta_resize) { $imagemeta_resize_path = $imagemeta_resize_pathinfo['dirname'] . '/' . $imagemeta_resize_pathinfo['filename'] . '-' . $imagemeta_resize . '.' . $imagemeta_resize_pathinfo['extension']; if ($parallel_opt) { add_filter('http_headers_useragent', 'ewww_image_optimizer_cloud_useragent', PHP_INT_MAX); $async_path = str_replace($upload_path, '', $imagemeta_resize_path); $ewwwio_async_optimize_media->data(array('path' => $async_path, 'size' => '', 'ewww_force' => $force))->dispatch(); } else { ewww_image_optimizer($imagemeta_resize_path); } } } // and another custom theme if (isset($meta['custom_sizes'])) { $custom_sizes_pathinfo = pathinfo($file_path); $custom_size_path = ''; foreach ($meta['custom_sizes'] as $custom_size) { $custom_size_path = $custom_sizes_pathinfo['dirname'] . '/' . $custom_size['file']; if ($parallel_opt) { add_filter('http_headers_useragent', 'ewww_image_optimizer_cloud_useragent', PHP_INT_MAX); $async_path = str_replace($upload_path, '', $custom_size_path); $ewwwio_async_optimize_media->data(array('path' => $async_path, 'size' => '', 'ewww_force' => $force))->dispatch(); } else { ewww_image_optimizer($custom_size_path); } } } // OK, here we start checking to see what sizes are done, and populate the metadata with the results $processing = true; $timer = apply_filters('ewww_image_optimizer_background_timer_init', 1); $increment = apply_filters('ewww_image_optimizer_background_timer_increment', 1); while ($parallel_opt && $processing) { $processing = false; foreach ($parallel_sizes as $size => $filename) { if (is_file($filename . '.processing')) { ewwwio_debug_message("still processing {$size}"); $processing = true; continue; } if ($size == 'full') { $image = ewww_image_optimizer_find_already_optimized($filename); $meta['ewww_image_optimizer'] = $image['results']; unset($parallel_sizes[$size]); ewwwio_debug_message('got results for full size'); } else { $image = ewww_image_optimizer_find_already_optimized($filename); $meta['sizes'][$size]['ewww_image_optimizer'] = $image['results']; unset($parallel_sizes[$size]); ewwwio_debug_message("got results for {$size} size"); } } if ($processing) { ewwwio_debug_message("sleeping for {$timer} seconds"); sleep($timer); $timer += $increment; clearstatcache(); } if ($timer > 35) { foreach ($parallel_sizes as $filename) { if (is_file($filename . '.processing')) { unlink($filename . '.processing'); } } $meta['processing'] = 1; ewww_image_optimizer_debug_log(); return $meta; } // ewww_image_optimizer_debug_log(); } unset($meta['processing']); if (!empty($new_image)) { $meta = ewww_image_optimizer_update_attachment_metadata($meta, $ID); } if (!preg_match('/' . __('Previously Optimized', EWWW_IMAGE_OPTIMIZER_DOMAIN) . '/', $meta['ewww_image_optimizer']) && class_exists('Amazon_S3_And_CloudFront')) { global $as3cf; if (method_exists($as3cf, 'wp_update_attachment_metadata')) { $as3cf->wp_update_attachment_metadata($meta, $ID); } elseif (method_exists($as3cf, 'wp_generate_attachment_metadata')) { $as3cf->wp_generate_attachment_metadata($meta, $ID); } ewwwio_debug_message('uploading to Amazon S3'); } if (!preg_match('/' . __('Previously Optimized', EWWW_IMAGE_OPTIMIZER_DOMAIN) . '/', $meta['ewww_image_optimizer']) && class_exists('DreamSpeed_Services')) { global $dreamspeed; $dreamspeed->wp_generate_attachment_metadata($meta, $ID); ewwwio_debug_message('uploading to Dreamspeed'); } if (class_exists('Cloudinary') && Cloudinary::config_get("api_secret") && ewww_image_optimizer_get_option('ewww_image_optimizer_enable_cloudinary') && !empty($new_image)) { try { $result = CloudinaryUploader::upload($file, array('use_filename' => true)); } catch (Exception $e) { $error = $e->getMessage(); } if (!empty($error)) { ewwwio_debug_message("Cloudinary error: {$error}"); } else { ewwwio_debug_message('successfully uploaded to Cloudinary'); // register the attachment in the database as a cloudinary attachment $old_url = wp_get_attachment_url($ID); wp_update_post(array('ID' => $ID, 'guid' => $result['url'])); update_attached_file($ID, $result['url']); $meta['cloudinary'] = TRUE; $errors = array(); // update the image location for the attachment CloudinaryPlugin::update_image_src_all($ID, $result, $old_url, $result['url'], TRUE, $errors); if (count($errors) > 0) { ewwwio_debug_message("Cannot migrate the following posts:"); foreach ($errors as $error) { ewwwio_debug_message($error); } } } } if ($log) { ewww_image_optimizer_debug_log(); } ewwwio_memory(__FUNCTION__); // send back the updated metadata return $meta; }
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_resize_upload($file) { // parts adapted from Imsanity (THANKS Jason!) ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); if (!$file) { return false; } if (!empty($_REQUEST['post_id']) || !empty($_REQUEST['action']) && $_REQUEST['action'] === 'upload-attachment') { $maxwidth = ewww_image_optimizer_get_option('ewww_image_optimizer_maxmediawidth'); $maxheight = ewww_image_optimizer_get_option('ewww_image_optimizer_maxmediaheight'); ewwwio_debug_message('resizing image from media library or attached to post'); } else { $maxwidth = ewww_image_optimizer_get_option('ewww_image_optimizer_maxotherwidth'); $maxheight = ewww_image_optimizer_get_option('ewww_image_optimizer_maxotherheight'); ewwwio_debug_message('resizing images from somewhere else'); } //check that options are not == 0 if ($maxwidth == 0 && $maxheight == 0) { return false; } //check file type $type = ewww_image_optimizer_mimetype($file, 'i'); if (strpos($type, 'image') === FALSE) { ewwwio_debug_message('not an image, cannot resize'); return false; } //check file size (dimensions) list($oldwidth, $oldheight) = getimagesize($file); if ($oldwidth <= $maxwidth && $oldheight <= $maxheight) { ewwwio_debug_message('image too small for resizing'); return false; } list($newwidth, $newheight) = wp_constrain_dimensions($oldwidth, $oldheight, $maxwidth, $maxheight); if (!function_exists('wp_get_image_editor')) { ewwwio_debug_message('no image editor function'); return false; } $editor = wp_get_image_editor($file); if (is_wp_error($editor)) { ewwwio_debug_message('could not get image editor'); return false; } if (function_exists('exif_read_data') && $type === 'image/jpeg') { $exif = @exif_read_data($file); if (is_array($exif) && array_key_exists('Orientation', $exif)) { $orientation = $exif['Orientation']; switch ($orientation) { case 3: $editor->rotate(180); break; case 6: $editor->rotate(-90); break; case 8: $editor->rotate(90); break; } } } $resized_image = $editor->resize($newwidth, $newheight); if (is_wp_error($resized_image)) { ewwwio_debug_message('error during resizing'); return false; } // $new_file = $editor->generate_filename(); // while ( file_exists( $new_file ) ) { $new_file = $editor->generate_filename('tmp'); // } $orig_size = filesize($file); $saved = $editor->save($new_file); if (is_wp_error($saved)) { ewwwio_debug_message('error saving resized image'); } if ($new_size = ewww_image_optimizer_filesize($new_file) && $new_size < $orig_size) { // generate a retina file from the full original if they have WP Retina 2x Pro if (function_exists('wr2x_is_pro') && wr2x_is_pro()) { $full_size_needed = wr2x_getoption("full_size", "wr2x_basics", false); if ($full_size_needed) { // Is the file related to this size there? $retina_file = ''; $pathinfo = pathinfo($file); $retina_file = trailingslashit($pathinfo['dirname']) . $pathinfo['filename'] . wr2x_retina_extension() . $pathinfo['extension']; if ($retina_file && !file_exists($retina_file) && wr2x_are_dimensions_ok($oldwidth, $oldheight, $newwidth * 2, $newheight * 2)) { $image = wr2x_vt_resize($file, $newwidth * 2, $newheight * 2, false, $retina_file); } } } rename($new_file, $file); // store info on the current image for future reference global $wpdb; $already_optimized = ewww_image_optimizer_find_already_optimized($file); // if the original file has never been optimized, then just update the record that was created with the proper filename (because the resized file has usually been optimized) if (empty($already_optimized)) { $wpdb->update($wpdb->ewwwio_images, array('path' => $file), array('path' => $new_file)); // otherwise, we delete the record created from optimizing the resized file, and update our records for the original file } else { $temp_optimized = ewww_image_optimizer_find_already_optimized($new_file); if (is_array($temp_optimized) && !empty($temp_optimized['id'])) { $wpdb->delete($wpdb->ewwwio_images, array('id' => $temp_optimized['id']), array('%d')); } ewww_image_optimizer_update_table($file, $new_size, $orig_size); } return array($newwidth, $newheight); } if (file_exists($new_file)) { unlink($new_file); } return false; }