ing. */ Helper::cache_delete( 'should_resize' ); } return $meta; } /** * Generates the new image for specified width and height, * Checks if the size of generated image is greater, * * @param string $file_path Original File path. * @param int $original_file_size File size before optimisation. * @param int $id Attachment ID. * @param array $meta Attachment Metadata. * @param bool $unlink Whether to unlink the original image or not. * * @return array|bool|false If the image generation was successful */ public function perform_resize( $file_path, $original_file_size, $id, $meta = array(), $unlink = true ) { /** * Filter the resize image dimensions * * @since 2.3 * * @param array $sizes { * Array of sizes containing max width and height for all the uploaded images. * * @type int $width Maximum Width For resizing * @type int $height Maximum Height for resizing * } * * @param string $file_path Original Image file path * * @param array $upload { * Array of upload data. * * @type string $file Filename of the newly-uploaded file. * @type string $url URL of the uploaded file. * @type string $type File type. * } * * @hooked Png2jpg::cache_can_be_converted_status() Save transparent status before resizing the image. */ $sizes = apply_filters( 'wp_smush_resize_sizes', array( 'width' => $this->max_w, 'height' => $this->max_h, ), $file_path, $id ); $data = image_make_intermediate_size( $file_path, $sizes['width'], $sizes['height'] ); // If the image wasn't resized. if ( empty( $data['file'] ) ) { if ( $this->try_gd_fallback() ) { $data = image_make_intermediate_size( $file_path, $sizes['width'], $sizes['height'] ); } if ( empty( $data['file'] ) ) { Helper::logger()->resize()->warning( sprintf( 'Cannot resize image [%s(%d)].', Helper::clean_file_path( $file_path ), $id ) ); return false; } } // Check if file size is lesser than original image. $resize_path = path_join( dirname( $file_path ), $data['file'] ); if ( ! file_exists( $resize_path ) ) { Helper::logger()->resize()->notice( sprintf( 'The resized image [%s(%d)] does not exist.', Helper::clean_file_path( $resize_path ), Helper::clean_file_path( $file_path ), $id ) ); return false; } $data['file_path'] = $resize_path; $file_size = filesize( $resize_path ); $data['filesize'] = $file_size; if ( $file_size > $original_file_size ) { // Don't Unlink for nextgen images. if ( $unlink ) { $this->maybe_unlink( $resize_path, $meta ); } Helper::logger()->resize()->notice( sprintf( 'The resized image [%s](%s) is larger than the original image [%s(%d)](%s).', Helper::clean_file_path( $resize_path ), size_format( $file_size ), Helper::clean_file_path( $file_path ), $id, size_format( $original_file_size ) ) ); } return $data; } /** * Fix for WP Engine 'width or height exceeds limit' Imagick error. * * If unable to resize with Imagick, try to fallback to GD. * * @since 3.4.0 */ private function try_gd_fallback() { if ( ! function_exists( 'gd_info' ) ) { return false; } return add_filter( 'wp_image_editors', function( $editors ) { $editors = array_diff( $editors, array( 'WP_Image_Editor_GD' ) ); array_unshift( $editors, 'WP_Image_Editor_GD' ); return $editors; } ); } /** * Replace the original file with resized file * * @param string $file_path File path. * @param mixed $resized Resized. * @param array $meta Meta. * * @return bool */ private function replace_original_image( $file_path, $resized, $meta = array() ) { $replaced = copy( $resized['file_path'], $file_path ); $this->maybe_unlink( $resized['file_path'], $meta ); return $replaced; } /** * Return Filename. * * @param string $filename Filename. * * @return mixed */ public function file_name( $filename ) { if ( empty( $filename ) ) { return $filename; } return $filename . 'tmp'; } /** * Do not unlink the resized file if the name is similar to one of the image sizes * * @param string $path Image File Path. * @param array $meta Image Meta. * * @return bool */ private function maybe_unlink( $path, $meta ) { if ( empty( $path ) ) { return true; } // Unlink directly if meta value is not specified. if ( empty( $meta['sizes'] ) ) { unlink( $path ); } $unlink = true; // Check if the file name is similar to one of the image sizes. $path_parts = pathinfo( $path ); $filename = ! empty( $path_parts['basename'] ) ? $path_parts['basename'] : $path_parts['filename']; if ( ! empty( $meta['sizes'] ) ) { foreach ( $meta['sizes'] as $image_size ) { if ( false === strpos( $image_size['file'], $filename ) ) { continue; } $unlink = false; break; } } if ( $unlink ) { unlink( $path ); } return true; } }