function ewww_ngg_image_save($filename)
 {
     ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
     global $ewww_defer;
     if (file_exists($filename)) {
         if ($ewww_defer && ewww_image_optimizer_get_option('ewww_image_optimizer_defer')) {
             ewww_image_optimizer_add_deferred_attachment("file,{$filename}");
             return $saved;
         }
         ewww_image_optimizer($filename);
         ewwwio_debug_message("ngg_Thumbnail saved: {$filename}");
         $image_size = ewww_image_optimizer_filesize($filename);
         ewwwio_debug_message("image editor size: {$image_size}");
     }
     ewww_image_optimizer_debug_log();
     ewwwio_memory(__FUNCTION__);
 }
 function ewww_added_new_image_slow($image)
 {
     // query the filesystem path of the gallery from the database
     global $ewww_defer;
     global $wpdb;
     $q = $wpdb->prepare("SELECT path FROM {$wpdb->prefix}ngg_gallery WHERE gid = %d LIMIT 1", $image['galleryID']);
     $gallery_path = $wpdb->get_var($q);
     // if we have a path to work with
     if ($gallery_path) {
         // construct the absolute path of the current image
         $file_path = trailingslashit($gallery_path) . $image['filename'];
         if ($ewww_defer && ewww_image_optimizer_get_option('ewww_image_optimizer_defer')) {
             ewww_image_optimizer_add_deferred_attachment("nextcellent,{$image['id']}");
             return;
         }
         // run the optimizer on the current image
         $res = ewww_image_optimizer(ABSPATH . $file_path, 2, false, false, true);
         // update the metadata for the optimized image
         nggdb::update_image_meta($image['id'], array('ewww_image_optimizer' => $res[1]));
     }
 }
 function generate_image_size($image, $size, $params = null, $skip_defaults = false)
 {
     ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
     global $ewww_defer;
     if (!defined('EWWW_IMAGE_OPTIMIZER_CLOUD')) {
         ewww_image_optimizer_init();
     }
     $success = $this->call_parent('generate_image_size', $image, $size, $params, $skip_defaults);
     if ($success) {
         $filename = $success->fileName;
         if ($ewww_defer && ewww_image_optimizer_get_option('ewww_image_optimizer_defer')) {
             ewww_image_optimizer_add_deferred_attachment("file,{$filename}");
             return $saved;
         }
         ewww_image_optimizer($filename);
         ewwwio_debug_message("nextgen dynamic thumb saved: {$filename}");
         $image_size = ewww_image_optimizer_filesize($filename);
         ewwwio_debug_message("optimized size: {$image_size}");
     }
     ewww_image_optimizer_debug_log();
     ewwwio_memory(__FUNCTION__);
     return $success;
 }
Example #4
0
/**
 * 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)
{
    ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
    if (!is_array($meta)) {
        ewwwio_debug_message('attachment meta is not a usable array');
        return $meta;
    }
    global $wpdb;
    global $ewww_defer;
    $gallery_type = 1;
    ewwwio_debug_message("attachment id: {$ID}");
    if (!metadata_exists('post', $ID, '_wp_attachment_metadata')) {
        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}");
    // 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);
        $query = $wpdb->prepare("SELECT id,path FROM {$wpdb->ewwwio_images} WHERE path = %s AND image_size = '{$image_size}'", $imsanity_path);
        $optimized_query = $wpdb->get_results($query, ARRAY_A);
        if (!empty($optimized_query)) {
            foreach ($optimized_query as $image) {
                if ($image['path'] != $imsanity_path) {
                    ewwwio_debug_message("{$image['path']} does not match {$imsanity_path}, continuing our search");
                } else {
                    $already_optimized = $image;
                }
            }
        }
        if (!empty($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 ($ewww_defer && ewww_image_optimizer_get_option('ewww_image_optimizer_defer')) {
        ewww_image_optimizer_add_deferred_attachment("media,{$ID}");
        return $meta;
    }
    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;
    }
    ewww_image_optimizer_hidpi_optimize($file_path);
    $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);
    }
    // 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);
                    $query = $wpdb->prepare("SELECT id,path FROM {$wpdb->ewwwio_images} WHERE path = %s AND image_size = '{$image_size}'", $image_path);
                    $optimized_query = $wpdb->get_results($query, ARRAY_A);
                    if (!empty($optimized_query)) {
                        foreach ($optimized_query as $image) {
                            if ($image['path'] != $image_path) {
                                ewwwio_debug_message("{$image['path']} does not match {$image_path}, continuing our search");
                            } else {
                                $already_optimized = $image;
                            }
                        }
                    }
                    if (!empty($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
                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))) {
                    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'];
        }
    }
    // 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'];
            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'];
            ewww_image_optimizer($custom_size_path);
        }
    }
    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;
}
Example #5
0
 function ewww_added_new_image($image)
 {
     ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
     global $ewww_defer;
     // make sure the image path is set
     if (isset($image->imagePath)) {
         // get the image ID
         $pid = $image->pid;
         if ($ewww_defer && ewww_image_optimizer_get_option('ewww_image_optimizer_defer')) {
             ewww_image_optimizer_add_deferred_attachment("flag,{$pid}");
             return;
         }
         // optimize the full size
         $res = ewww_image_optimizer($image->imagePath, 3, false, false, true);
         // optimize the web optimized version
         $wres = ewww_image_optimizer($image->webimagePath, 3, false, true);
         // optimize the thumbnail
         $tres = ewww_image_optimizer($image->thumbPath, 3, false, true);
         // retrieve the metadata for the image ID
         $meta = new flagMeta($pid);
         ewwwio_debug_message(print_r($meta->image->meta_data, TRUE));
         $meta->image->meta_data['ewww_image_optimizer'] = $res[1];
         if (!empty($meta->image->meta_data['webview'])) {
             $meta->image->meta_data['webview']['ewww_image_optimizer'] = $wres[1];
         }
         $meta->image->meta_data['thumbnail']['ewww_image_optimizer'] = $tres[1];
         // update the image metadata in the db
         flagdb::update_image_meta($pid, $meta->image->meta_data);
     }
     ewww_image_optimizer_debug_log();
 }