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