diff --git a/imagify.php b/imagify.php index 8d49e434e..47868b6fa 100644 --- a/imagify.php +++ b/imagify.php @@ -3,7 +3,7 @@ Plugin Name: Imagify Plugin URI: https://wordpress.org/plugins/imagify/ Description: Dramaticaly reduce image file sizes without losing quality, make your website load faster, boost your SEO and save money on your bandwith using Imagify, the new most advanced image optimization tool. -Version: 1.5.6 +Version: 1.5.7 Author: WP Media Author URI: http://wp-media.me Licence: GPLv2 @@ -17,7 +17,7 @@ defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); // Imagify defines -define( 'IMAGIFY_VERSION' , '1.5.6' ); +define( 'IMAGIFY_VERSION' , '1.5.7' ); define( 'IMAGIFY_SLUG' , 'imagify' ); define( 'IMAGIFY_SETTINGS_SLUG' , IMAGIFY_SLUG . '_settings' ); define( 'IMAGIFY_WEB_MAIN' , 'https://imagify.io' ); diff --git a/inc/3rd-party/nextgen-gallery/inc/classes/class-attachment.php b/inc/3rd-party/nextgen-gallery/inc/classes/class-attachment.php index 89dd43e1e..3b02d6521 100644 --- a/inc/3rd-party/nextgen-gallery/inc/classes/class-attachment.php +++ b/inc/3rd-party/nextgen-gallery/inc/classes/class-attachment.php @@ -243,8 +243,9 @@ public function optimize( $optimization_level = null, $metadata = array() ) { ); // Get file path & URL for original image - $attachment_path = $this->get_original_path(); - $attachment_url = $this->get_original_url(); + $attachment_path = $this->get_original_path(); + $attachment_url = $this->get_original_url(); + $attachment_original_size = $this->get_original_size( false ); // Check if the full size is already optimized if ( $this->is_optimized() && ( $this->get_optimization_level() == $optimization_level ) ) { @@ -269,21 +270,42 @@ public function optimize( $optimization_level = null, $metadata = array() ) { set_transient( 'imagify-ngg-async-in-progress-' . $id, true, 10 * MINUTE_IN_SECONDS ); // Get the resize values for the original size - $resize = array(); + $resized = false; $do_resize = get_imagify_option( 'resize_larger', false ); $resize_width = get_imagify_option( 'resize_larger_w' ); $attachment_size = @getimagesize( $attachment_path ); if ( $do_resize && isset( $attachment_size[0] ) && $resize_width < $attachment_size[0] ) { - $resize['width'] = $resize_width; - } + $resized_attachment_path = $this->resize( $attachment_path, $attachment_size, $resize_width ); + + if ( ! is_wp_error( $resized_attachment_path ) ) { + $backup_path = get_imagify_attachment_backup_path( $attachment_path ); + $backup_path_info = pathinfo( $backup_path ); + + wp_mkdir_p( $backup_path_info['dirname'] ); + + // TO DO - check and send a error message if the backup can't be created + @copy( $attachment_path, $backup_path ); + imagify_chmod_file( $backup_path ); + + @rename( $resized_attachment_path, $attachment_path ); + imagify_chmod_file( $attachment_path ); + + // If resized temp file still exists, delete it + if ( file_exists( $resized_attachment_path ) ) { + unlink( $resized_attachment_path ); + } + + $resized = true; + } + } // Optimize the original size $response = do_imagify( $attachment_path, array( 'optimization_level' => $optimization_level, - 'resize' => $resize, 'context' => 'ngg', - 'original_size' => $this->get_original_size( false ) + 'resized' => $resized, + 'original_size' => $attachment_original_size, ) ); $data = $this->fill_data( $data, $response, $id, $attachment_url ); @@ -303,7 +325,7 @@ public function optimize( $optimization_level = null, $metadata = array() ) { // If we resized the original with success, we have to update the attachment metadata // If not, WordPress keeps the old attachment size. - if ( $do_resize && isset( $resize['width'] ) ) { + if ( $do_resize && $resized ) { $this->update_metadata_size(); } diff --git a/inc/classes/abstracts/abstract-attachment.php b/inc/classes/abstracts/abstract-attachment.php index ef829ed26..a1da2da74 100755 --- a/inc/classes/abstracts/abstract-attachment.php +++ b/inc/classes/abstracts/abstract-attachment.php @@ -380,4 +380,61 @@ public function optimize( $optimization_level = null, $metadata = array() ) {} * @return void */ public function restore() {} + + /** + * Resize an image if bigger than the maximum width defined in the settings. + * + * @since 1.5.7 + * @author Remy Perona + * + * @param string $attachment_path Path to the image + * @param array $attachment_sizes Array of original image dimensions + * @param int $max_width Maximum width defined in the settings + * @return string Path the the resized image or the original image if the resize failed + */ + function resize( $attachment_path, $attachment_sizes, $max_width ) { + $new_sizes = wp_constrain_dimensions( $attachment_sizes[0], $attachment_sizes[1], $max_width ); + + $editor = wp_get_image_editor( $attachment_path ); + + if ( is_wp_error( $editor ) ) { + return $editor; + } + + $image_type = pathinfo( $attachment_path, PATHINFO_EXTENSION ); + + // try to correct for auto-rotation if the info is available + if ( function_exists( 'exif_read_data' ) && ( $image_type == 'jpg' || $image_type == 'jpeg' ) ) { + $exif = @exif_read_data( $attachment_path ); + $orientation = is_array( $exif ) && array_key_exists( 'Orientation', $exif ) ? $exif['Orientation'] : 0; + + switch( $orientation ) { + case 3: + $editor->rotate( 180 ); + break; + case 6: + $editor->rotate( -90 ); + break; + case 8: + $editor->rotate( 90 ); + break; + } + } + + $resized = $editor->resize( $new_sizes[0], $new_sizes[1], false ); + + if ( is_wp_error( $resized ) ) { + return $resized; + } + + $resized_image_path = $editor->generate_filename( null ); + + $resized_image_saved = $editor->save( $resized_image_path ); + + if ( is_wp_error( $resized_image_saved ) ) { + return $resized_image_saved; + } + + return $resized_image_path; + } } \ No newline at end of file diff --git a/inc/classes/class-attachment.php b/inc/classes/class-attachment.php index 6009b60c7..15b41791d 100644 --- a/inc/classes/class-attachment.php +++ b/inc/classes/class-attachment.php @@ -199,8 +199,9 @@ public function optimize( $optimization_level = null, $metadata = array() ) { ); // Get file path & URL for original image - $attachment_path = $this->get_original_path(); - $attachment_url = $this->get_original_url(); + $attachment_path = $this->get_original_path(); + $attachment_url = $this->get_original_url(); + $attachment_original_size = $this->get_original_size( false ); // Check if the attachment extension is allowed if ( ! $id || ! wp_attachment_is_image( $id ) ) { @@ -230,21 +231,42 @@ public function optimize( $optimization_level = null, $metadata = array() ) { set_transient( 'imagify-async-in-progress-' . $id, true, 10 * MINUTE_IN_SECONDS ); // Get the resize values for the original size - $resize = array(); + $resized = false; $do_resize = get_imagify_option( 'resize_larger', false ); $resize_width = get_imagify_option( 'resize_larger_w' ); $attachment_size = @getimagesize( $attachment_path ); if ( $do_resize && isset( $attachment_size[0] ) && $resize_width < $attachment_size[0] ) { - $resize['width'] = $resize_width; - } + $resized_attachment_path = $this->resize( $attachment_path, $attachment_size, $resize_width ); + + if ( ! is_wp_error( $resized_attachment_path ) ) { + $backup_path = get_imagify_attachment_backup_path( $attachment_path ); + $backup_path_info = pathinfo( $backup_path ); + + wp_mkdir_p( $backup_path_info['dirname'] ); + + // TO DO - check and send a error message if the backup can't be created + @copy( $attachment_path, $backup_path ); + imagify_chmod_file( $backup_path ); + + @rename( $resized_attachment_path, $attachment_path ); + imagify_chmod_file( $attachment_path ); + + // If resized temp file still exists, delete it + if ( file_exists( $resized_attachment_path ) ) { + unlink( $resized_attachment_path ); + } + + $resized = true; + } + } // Optimize the original size $response = do_imagify( $attachment_path, array( 'optimization_level' => $optimization_level, - 'resize' => $resize, 'context' => 'wp', - 'original_size' => $this->get_original_size( false ) + 'resized' => $resized, + 'original_size' => $attachment_original_size, ) ); $data = $this->fill_data( $data, $response, $id, $attachment_url ); @@ -258,7 +280,7 @@ public function optimize( $optimization_level = null, $metadata = array() ) { // If we resized the original with success, we have to update the attachment metadata // If not, WordPress keeps the old attachment size. - if ( $do_resize && isset( $resize['width'] ) ) { + if ( $do_resize && $resized ) { $this->update_metadata_size(); } diff --git a/inc/functions/process.php b/inc/functions/process.php index 551e6e1cb..e1288e243 100755 --- a/inc/functions/process.php +++ b/inc/functions/process.php @@ -9,7 +9,6 @@ * @param string $file_path Absolute path to the image file. * @param bool $backup Force a backup of the original file. * @param int $optimization_level The optimization level (2=ultra, 1=aggressive, 0=normal). - * @param array $resize The resize parameters (with & height). * @param bool $keep_exif To keep exif data or not * @return obj|array Error message | Optimized image data */ @@ -19,10 +18,10 @@ function do_imagify( $file_path, $args = array() ) { array( 'backup' => get_imagify_option( 'backup', false ), 'optimization_level' => get_imagify_option( 'optimization_level', 1 ), - 'resize' => array(), 'keep_exif' => get_imagify_option( 'exif', false ), 'context' => 'wp', - 'original_size' => 0 + 'resized' => false, + 'original_size' => 0, ), $args ); @@ -92,7 +91,6 @@ function do_imagify( $file_path, $args = array() ) { array( 'aggressive' => ( 1 === (int) $args['optimization_level'] ) ? true : false, 'ultra' => ( 2 === (int) $args['optimization_level'] ) ? true : false, - 'resize' => $args['resize'], 'keep_exif' => $args['keep_exif'], 'context' => $args['context'], 'original_size' => $args['original_size'] @@ -108,7 +106,7 @@ function do_imagify( $file_path, $args = array() ) { } // Create a backup file - if ( 'wp' === $args['context'] && $args['backup'] ) { + if ( 'wp' === $args['context'] && $args['backup'] && ! $args['resized'] ) { $backup_path = get_imagify_attachment_backup_path( $file_path ); $backup_path_info = pathinfo( $backup_path ); diff --git a/readme.txt b/readme.txt index 1b18d8dc0..fab6cde2b 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: wp_media Tags: compress image, images, performance, optimization, photos, upload, resize, gif, png, jpg, reduce image size, retina Requires at least: 3.7.0 Tested up to: 4.6 -Stable tag: 1.5.6 +Stable tag: 1.5.7 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -135,6 +135,10 @@ When the plugin is disabled, your existing images remain optimized. Backups of t 3. Media Page == Changelog == += 1.5.7 = +* Improvement + * Resize images bigger than the maximum width defined in the settings using WP Image Editor instead of Imagify API + = 1.5.6 = * Improvement * Dynamically update from the API the maximum image size allowed in bulk optimization