ReSizer.php
255 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Image resize Class |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Models; |
| 9 | |
| 10 | use WP_Error; |
| 11 | |
| 12 | // Do not allow directly accessing this file. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit( 'This script cannot be accessed directly.' ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Image resize Class |
| 19 | */ |
| 20 | class ReSizer { |
| 21 | /** |
| 22 | * The singleton instance |
| 23 | */ |
| 24 | private static $instance = null; |
| 25 | |
| 26 | /** |
| 27 | * Should an RtTpgException be thrown on error? |
| 28 | * If false (default), then the error will just be logged. |
| 29 | */ |
| 30 | public $throwOnError = false; |
| 31 | |
| 32 | /** |
| 33 | * No cloning allowed |
| 34 | */ |
| 35 | private function __clone() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * For your custom default usage you may want to initialize an rtTPGReSizer object by yourself and then have own defaults |
| 40 | */ |
| 41 | public static function getInstance() { |
| 42 | if ( self::$instance == null ) { |
| 43 | self::$instance = new self(); |
| 44 | } |
| 45 | |
| 46 | return self::$instance; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Run, forest. |
| 51 | */ |
| 52 | public function process( $url, $width = null, $height = null, $crop = null, $single = true, $upscale = false ) { |
| 53 | try { |
| 54 | // Validate inputs. |
| 55 | if ( ! $url ) { |
| 56 | return new WP_Error( |
| 57 | 'broke', |
| 58 | sprintf( |
| 59 | /* Translators: %s URL */ |
| 60 | esc_html__( '%s parameter is required', 'the-post-grid' ), |
| 61 | $url |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | if ( ! $width ) { |
| 66 | return new WP_Error( |
| 67 | 'broke', |
| 68 | sprintf( |
| 69 | /* Translators: %s Width */ |
| 70 | esc_html__( '%s parameter is required', 'the-post-grid' ), |
| 71 | $width |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | if ( ! $height ) { |
| 76 | return new WP_Error( |
| 77 | 'broke', |
| 78 | sprintf( |
| 79 | /* Translators: %s Height */ |
| 80 | esc_html__( '%s parameter is required', 'the-post-grid' ), |
| 81 | $height |
| 82 | ) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | // Caipt'n, ready to hook. |
| 87 | if ( true === $upscale ) { |
| 88 | add_filter( 'image_resize_dimensions', [ $this, 'aq_upscale' ], 10, 6 ); |
| 89 | } |
| 90 | |
| 91 | // Define upload path & dir. |
| 92 | $upload_info = wp_upload_dir(); |
| 93 | $upload_dir = $upload_info['basedir']; |
| 94 | $upload_url = $upload_info['baseurl']; |
| 95 | |
| 96 | $http_prefix = 'http://'; |
| 97 | $https_prefix = 'https://'; |
| 98 | $relative_prefix = '//'; // The protocol-relative URL |
| 99 | |
| 100 | /* |
| 101 | if the $url scheme differs from $upload_url scheme, make them match |
| 102 | if the schemes differe, images don't show up. */ |
| 103 | if ( ! strncmp( $url, $https_prefix, strlen( $https_prefix ) ) ) { // if url begins with https:// make $upload_url begin with https:// as well |
| 104 | $upload_url = str_replace( $http_prefix, $https_prefix, $upload_url ); |
| 105 | } elseif ( ! strncmp( $url, $http_prefix, strlen( $http_prefix ) ) ) { // if url begins with http:// make $upload_url begin with http:// as well |
| 106 | $upload_url = str_replace( $https_prefix, $http_prefix, $upload_url ); |
| 107 | } elseif ( ! strncmp( $url, $relative_prefix, strlen( $relative_prefix ) ) ) { // if url begins with // make $upload_url begin with // as well |
| 108 | $upload_url = str_replace( |
| 109 | [ |
| 110 | 0 => "$http_prefix", |
| 111 | 1 => "$https_prefix", |
| 112 | ], |
| 113 | $relative_prefix, |
| 114 | $upload_url |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | // Check if $img_url is local. |
| 119 | if ( false === strpos( $url, $upload_url ) ) { |
| 120 | return new WP_Error( |
| 121 | 'broke', |
| 122 | sprintf( |
| 123 | /* Translators: %s URL */ |
| 124 | esc_html__( 'Image must be local: %s', 'the-post-grid' ), |
| 125 | $url |
| 126 | ) |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | // Define path of image. |
| 131 | $rel_path = str_replace( $upload_url, '', $url ); |
| 132 | $img_path = $upload_dir . $rel_path; |
| 133 | |
| 134 | // Check if img path exists, and is an image indeed. |
| 135 | if ( ! file_exists( $img_path ) or ! getimagesize( $img_path ) ) { |
| 136 | return new WP_Error( 'broke', 'Image file does not exist (or is not an image): ' . $img_path ); |
| 137 | // throw new RtTpgException( 'Image file does not exist (or is not an image): ' . $img_path ); |
| 138 | } |
| 139 | |
| 140 | // Get image info. |
| 141 | $info = pathinfo( $img_path ); |
| 142 | $ext = $info['extension']; |
| 143 | list( $orig_w, $orig_h ) = getimagesize( $img_path ); |
| 144 | |
| 145 | // Get image size after cropping. |
| 146 | $dims = image_resize_dimensions( $orig_w, $orig_h, $width, $height, $crop ); |
| 147 | $dst_w = is_array( $dims ) && isset( $dims[4] ) ? $dims[4] : $orig_w; |
| 148 | $dst_h = is_array( $dims ) && isset( $dims[5] ) ? $dims[5] : $orig_h; |
| 149 | |
| 150 | // Return the original image only if it exactly fits the needed measures. |
| 151 | if ( ! $dims && ( ( ( null === $height && $orig_w == $width ) xor ( null === $width && $orig_h == $height ) ) xor ( $height == $orig_h && $width == $orig_w ) ) ) { |
| 152 | $img_url = $url; |
| 153 | $dst_w = $orig_w; |
| 154 | $dst_h = $orig_h; |
| 155 | } else { |
| 156 | // Use this to check if cropped image already exists, so we can return that instead. |
| 157 | $suffix = "{$dst_w}x{$dst_h}"; |
| 158 | $dst_rel_path = str_replace( '.' . $ext, '', $rel_path ); |
| 159 | $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}"; |
| 160 | |
| 161 | if ( ! $dims || ( true == $crop && false == $upscale && ( $dst_w < $width || $dst_h < $height ) ) ) { |
| 162 | // Can't resize, so return false saying that the action to do could not be processed as planned. |
| 163 | return new WP_Error( 'broke', 'Image file does not exist (or is not an image): ' . $img_path ); |
| 164 | // throw new RtTpgException( 'Unable to resize image because image_resize_dimensions() failed' ); |
| 165 | } // Else check if cache exists. |
| 166 | elseif ( file_exists( $destfilename ) && getimagesize( $destfilename ) ) { |
| 167 | $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}"; |
| 168 | } // Else, we resize the image and return the new resized image url. |
| 169 | else { |
| 170 | |
| 171 | $editor = wp_get_image_editor( $img_path ); |
| 172 | |
| 173 | if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) { |
| 174 | return new WP_Error( 'broke', 'Image file does not exist (or is not an image): ' . $img_path ); |
| 175 | // throw new RtTpgException( 'Unable to get WP_Image_Editor: ' . |
| 176 | // $editor->get_error_message() . ' (is GD or ImageMagick installed?)' ); |
| 177 | } |
| 178 | |
| 179 | $resized_file = $editor->save(); |
| 180 | |
| 181 | if ( ! is_wp_error( $resized_file ) ) { |
| 182 | $resized_rel_path = str_replace( $upload_dir, '', $resized_file['path'] ); |
| 183 | $img_url = $upload_url . $resized_rel_path; |
| 184 | } else { |
| 185 | return new WP_Error( 'broke', 'Image file does not exist (or is not an image): ' . $img_path ); |
| 186 | // throw new RtTpgException( 'Unable to save resized image file: ' . $editor->get_error_message() ); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Okay, leave the ship. |
| 192 | if ( true === $upscale ) { |
| 193 | remove_filter( 'image_resize_dimensions', [ $this, 'aq_upscale' ] ); |
| 194 | } |
| 195 | |
| 196 | // Return the output. |
| 197 | if ( $single ) { |
| 198 | // str return. |
| 199 | $image = $img_url; |
| 200 | } else { |
| 201 | // array return. |
| 202 | $image = [ |
| 203 | 0 => $img_url, |
| 204 | 1 => $dst_w, |
| 205 | 2 => $dst_h, |
| 206 | ]; |
| 207 | } |
| 208 | |
| 209 | return $image; |
| 210 | } catch ( RtTpgException $ex ) { |
| 211 | error_log( 'rtTPGReSizer.process() error: ' . $ex->getMessage() ); |
| 212 | |
| 213 | if ( $this->throwOnError ) { |
| 214 | // Bubble up exception. |
| 215 | throw $ex; |
| 216 | } else { |
| 217 | // Return false, so that this patch is backwards-compatible. |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Callback to overwrite WP computing of thumbnail measures |
| 225 | */ |
| 226 | function aq_upscale( $default, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) { |
| 227 | if ( ! $crop ) { |
| 228 | return null; |
| 229 | } // Let the WordPress default function handle this. |
| 230 | |
| 231 | // Here is the point we allow to use larger image size than the original one. |
| 232 | $aspect_ratio = $orig_w / $orig_h; |
| 233 | $new_w = $dest_w; |
| 234 | $new_h = $dest_h; |
| 235 | |
| 236 | if ( ! $new_w ) { |
| 237 | $new_w = intval( $new_h * $aspect_ratio ); |
| 238 | } |
| 239 | |
| 240 | if ( ! $new_h ) { |
| 241 | $new_h = intval( $new_w / $aspect_ratio ); |
| 242 | } |
| 243 | |
| 244 | $size_ratio = max( $new_w / $orig_w, $new_h / $orig_h ); |
| 245 | |
| 246 | $crop_w = round( $new_w / $size_ratio ); |
| 247 | $crop_h = round( $new_h / $size_ratio ); |
| 248 | |
| 249 | $s_x = floor( ( $orig_w - $crop_w ) / 2 ); |
| 250 | $s_y = floor( ( $orig_h - $crop_h ) / 2 ); |
| 251 | |
| 252 | return [ 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ]; |
| 253 | } |
| 254 | } |
| 255 |