| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * WordPress GD Image Editor |
|---|
| 4 | * |
|---|
| 5 | * @package WordPress |
|---|
| 6 | * @subpackage Image_Editor |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * WordPress Image Editor Class for Image Manipulation through GD |
|---|
| 11 | * |
|---|
| 12 | * @since 3.5.0 |
|---|
| 13 | * @package WordPress |
|---|
| 14 | * @subpackage Image_Editorsave |
|---|
| 15 | * @uses WP_Image_Editor Extends class |
|---|
| 16 | */ |
|---|
| 17 | class WP_Image_Editor_GD extends WP_Image_Editor { |
|---|
| 18 | |
|---|
| 19 | protected $image = false; // GD Resource |
|---|
| 20 | |
|---|
| 21 | function __destruct() { |
|---|
| 22 | if ( $this->image ) { |
|---|
| 23 | // we don't need the original in memory anymore |
|---|
| 24 | imagedestroy( $this->image ); |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * Checks to see if current environment supports GD. |
|---|
| 30 | * |
|---|
| 31 | * @since 3.5.0 |
|---|
| 32 | * @access public |
|---|
| 33 | * |
|---|
| 34 | * @return boolean |
|---|
| 35 | */ |
|---|
| 36 | public static function test( $args = array() ) { |
|---|
| 37 | if ( ! extension_loaded('gd') || ! function_exists('gd_info') ) |
|---|
| 38 | return false; |
|---|
| 39 | |
|---|
| 40 | // On some setups GD library does not provide imagerotate() - Ticket #11536 |
|---|
| 41 | if ( isset( $args['methods'] ) && |
|---|
| 42 | in_array( 'rotate', $args['methods'] ) && |
|---|
| 43 | ! function_exists('imagerotate') ){ |
|---|
| 44 | |
|---|
| 45 | return false; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | return true; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Checks to see if editor supports the mime-type specified. |
|---|
| 53 | * |
|---|
| 54 | * @since 3.5.0 |
|---|
| 55 | * @access public |
|---|
| 56 | * |
|---|
| 57 | * @param string $mime_type |
|---|
| 58 | * @return boolean |
|---|
| 59 | */ |
|---|
| 60 | public static function supports_mime_type( $mime_type ) { |
|---|
| 61 | $image_types = imagetypes(); |
|---|
| 62 | switch( $mime_type ) { |
|---|
| 63 | case 'image/jpeg': |
|---|
| 64 | return ($image_types & IMG_JPG) != 0; |
|---|
| 65 | case 'image/png': |
|---|
| 66 | return ($image_types & IMG_PNG) != 0; |
|---|
| 67 | case 'image/gif': |
|---|
| 68 | return ($image_types & IMG_GIF) != 0; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | return false; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Loads image from $this->file into new GD Resource. |
|---|
| 76 | * |
|---|
| 77 | * @since 3.5.0 |
|---|
| 78 | * @access protected |
|---|
| 79 | * |
|---|
| 80 | * @return boolean|\WP_Error |
|---|
| 81 | */ |
|---|
| 82 | public function load() { |
|---|
| 83 | if ( $this->image ) |
|---|
| 84 | return true; |
|---|
| 85 | |
|---|
| 86 | if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) |
|---|
| 87 | return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file ); |
|---|
| 88 | |
|---|
| 89 | // Set artificially high because GD uses uncompressed images in memory |
|---|
| 90 | @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) ); |
|---|
| 91 | $this->image = @imagecreatefromstring( file_get_contents( $this->file ) ); |
|---|
| 92 | |
|---|
| 93 | if ( ! is_resource( $this->image ) ) |
|---|
| 94 | return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file ); |
|---|
| 95 | |
|---|
| 96 | $size = @getimagesize( $this->file ); |
|---|
| 97 | if ( ! $size ) |
|---|
| 98 | return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file ); |
|---|
| 99 | |
|---|
| 100 | $this->update_size( $size[0], $size[1] ); |
|---|
| 101 | $this->mime_type = $size['mime']; |
|---|
| 102 | |
|---|
| 103 | return true; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Sets or updates current image size. |
|---|
| 108 | * |
|---|
| 109 | * @since 3.5.0 |
|---|
| 110 | * @access protected |
|---|
| 111 | * |
|---|
| 112 | * @param int $width |
|---|
| 113 | * @param int $height |
|---|
| 114 | */ |
|---|
| 115 | protected function update_size( $width = false, $height = false ) { |
|---|
| 116 | if ( ! $width ) |
|---|
| 117 | $width = imagesx( $this->image ); |
|---|
| 118 | |
|---|
| 119 | if ( ! $height ) |
|---|
| 120 | $height = imagesy( $this->image ); |
|---|
| 121 | |
|---|
| 122 | return parent::update_size( $width, $height ); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * Resizes current image. |
|---|
| 127 | * Wraps _resize, since _resize returns a GD Resource. |
|---|
| 128 | * |
|---|
| 129 | * @since 3.5.0 |
|---|
| 130 | * @access public |
|---|
| 131 | * |
|---|
| 132 | * @param int $max_w |
|---|
| 133 | * @param int $max_h |
|---|
| 134 | * @param boolean $crop |
|---|
| 135 | * @return boolean|WP_Error |
|---|
| 136 | */ |
|---|
| 137 | public function resize( $max_w, $max_h, $crop = false ) { |
|---|
| 138 | if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) |
|---|
| 139 | return true; |
|---|
| 140 | |
|---|
| 141 | $resized = $this->_resize( $max_w, $max_h, $crop ); |
|---|
| 142 | |
|---|
| 143 | if ( is_resource( $resized ) ) { |
|---|
| 144 | imagedestroy( $this->image ); |
|---|
| 145 | $this->image = $resized; |
|---|
| 146 | return true; |
|---|
| 147 | |
|---|
| 148 | } elseif ( is_wp_error( $resized ) ) |
|---|
| 149 | return $resized; |
|---|
| 150 | |
|---|
| 151 | return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | protected function _resize( $max_w, $max_h, $crop = false ) { |
|---|
| 155 | $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); |
|---|
| 156 | if ( ! $dims ) { |
|---|
| 157 | return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file ); |
|---|
| 158 | } |
|---|
| 159 | list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; |
|---|
| 160 | |
|---|
| 161 | $resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|---|
| 162 | imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|---|
| 163 | |
|---|
| 164 | if ( is_resource( $resized ) ) { |
|---|
| 165 | $this->update_size( $dst_w, $dst_h ); |
|---|
| 166 | return $resized; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file ); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Processes current image and saves to disk |
|---|
| 174 | * multiple sizes from single source. |
|---|
| 175 | * |
|---|
| 176 | * @since 3.5.0 |
|---|
| 177 | * @access public |
|---|
| 178 | * |
|---|
| 179 | * @param array $sizes { {'width'=>int, 'height'=>int, 'crop'=>bool}, ... } |
|---|
| 180 | * @return array |
|---|
| 181 | */ |
|---|
| 182 | public function multi_resize( $sizes ) { |
|---|
| 183 | $metadata = array(); |
|---|
| 184 | $orig_size = $this->size; |
|---|
| 185 | |
|---|
| 186 | foreach ( $sizes as $size => $size_data ) { |
|---|
| 187 | $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
|---|
| 188 | |
|---|
| 189 | if( ! is_wp_error( $image ) ) { |
|---|
| 190 | $resized = $this->_save( $image ); |
|---|
| 191 | |
|---|
| 192 | imagedestroy( $image ); |
|---|
| 193 | |
|---|
| 194 | if ( ! is_wp_error( $resized ) && $resized ) { |
|---|
| 195 | unset( $resized['path'] ); |
|---|
| 196 | $metadata[$size] = $resized; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | $this->size = $orig_size; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | return $metadata; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /** |
|---|
| 207 | * Crops Image. |
|---|
| 208 | * |
|---|
| 209 | * @since 3.5.0 |
|---|
| 210 | * @access public |
|---|
| 211 | * |
|---|
| 212 | * @param string|int $src The source file or Attachment ID. |
|---|
| 213 | * @param int $src_x The start x position to crop from. |
|---|
| 214 | * @param int $src_y The start y position to crop from. |
|---|
| 215 | * @param int $src_w The width to crop. |
|---|
| 216 | * @param int $src_h The height to crop. |
|---|
| 217 | * @param int $dst_w Optional. The destination width. |
|---|
| 218 | * @param int $dst_h Optional. The destination height. |
|---|
| 219 | * @param boolean $src_abs Optional. If the source crop points are absolute. |
|---|
| 220 | * @return boolean|WP_Error |
|---|
| 221 | */ |
|---|
| 222 | public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { |
|---|
| 223 | // If destination width/height isn't specified, use same as |
|---|
| 224 | // width/height from source. |
|---|
| 225 | if ( ! $dst_w ) |
|---|
| 226 | $dst_w = $src_w; |
|---|
| 227 | if ( ! $dst_h ) |
|---|
| 228 | $dst_h = $src_h; |
|---|
| 229 | |
|---|
| 230 | $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|---|
| 231 | |
|---|
| 232 | if ( $src_abs ) { |
|---|
| 233 | $src_w -= $src_x; |
|---|
| 234 | $src_h -= $src_y; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | if ( function_exists( 'imageantialias' ) ) |
|---|
| 238 | imageantialias( $dst, true ); |
|---|
| 239 | |
|---|
| 240 | imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|---|
| 241 | |
|---|
| 242 | if ( is_resource( $dst ) ) { |
|---|
| 243 | imagedestroy( $this->image ); |
|---|
| 244 | $this->image = $dst; |
|---|
| 245 | $this->update_size(); |
|---|
| 246 | return true; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file ); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | /** |
|---|
| 253 | * Rotates current image counter-clockwise by $angle. |
|---|
| 254 | * Ported from image-edit.php |
|---|
| 255 | * |
|---|
| 256 | * @since 3.5.0 |
|---|
| 257 | * @access public |
|---|
| 258 | * |
|---|
| 259 | * @param float $angle |
|---|
| 260 | * @return boolean|WP_Error |
|---|
| 261 | */ |
|---|
| 262 | public function rotate( $angle ) { |
|---|
| 263 | if ( function_exists('imagerotate') ) { |
|---|
| 264 | $rotated = imagerotate( $this->image, $angle, 0 ); |
|---|
| 265 | |
|---|
| 266 | if ( is_resource( $rotated ) ) { |
|---|
| 267 | imagedestroy( $this->image ); |
|---|
| 268 | $this->image = $rotated; |
|---|
| 269 | $this->update_size(); |
|---|
| 270 | return true; |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | return new WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file ); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | /** |
|---|
| 277 | * Flips current image. |
|---|
| 278 | * |
|---|
| 279 | * @since 3.5.0 |
|---|
| 280 | * @access public |
|---|
| 281 | * |
|---|
| 282 | * @param boolean $horz Horizontal Flip |
|---|
| 283 | * @param boolean $vert Vertical Flip |
|---|
| 284 | * @returns boolean|WP_Error |
|---|
| 285 | */ |
|---|
| 286 | public function flip( $horz, $vert ) { |
|---|
| 287 | $w = $this->size['width']; |
|---|
| 288 | $h = $this->size['height']; |
|---|
| 289 | $dst = wp_imagecreatetruecolor( $w, $h ); |
|---|
| 290 | |
|---|
| 291 | if ( is_resource( $dst ) ) { |
|---|
| 292 | $sx = $vert ? ($w - 1) : 0; |
|---|
| 293 | $sy = $horz ? ($h - 1) : 0; |
|---|
| 294 | $sw = $vert ? -$w : $w; |
|---|
| 295 | $sh = $horz ? -$h : $h; |
|---|
| 296 | |
|---|
| 297 | if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { |
|---|
| 298 | imagedestroy( $this->image ); |
|---|
| 299 | $this->image = $dst; |
|---|
| 300 | return true; |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | return new WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file ); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | /** |
|---|
| 307 | * Saves current in-memory image to file. |
|---|
| 308 | * |
|---|
| 309 | * @since 3.5.0 |
|---|
| 310 | * @access public |
|---|
| 311 | * |
|---|
| 312 | * @param string $destfilename |
|---|
| 313 | * @param string $mime_type |
|---|
| 314 | * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
|---|
| 315 | */ |
|---|
| 316 | public function save( $filename = null, $mime_type = null ) { |
|---|
| 317 | $saved = $this->_save( $this->image, $filename, $mime_type ); |
|---|
| 318 | |
|---|
| 319 | if ( ! is_wp_error( $saved ) ) { |
|---|
| 320 | $this->file = $saved['path']; |
|---|
| 321 | $this->mime_type = $saved['mime-type']; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | return $saved; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | protected function _save( $image, $filename = null, $mime_type = null ) { |
|---|
| 328 | list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); |
|---|
| 329 | |
|---|
| 330 | if ( ! $filename ) |
|---|
| 331 | $filename = $this->generate_filename( null, null, $extension ); |
|---|
| 332 | |
|---|
| 333 | if ( 'image/gif' == $mime_type ) { |
|---|
| 334 | if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) |
|---|
| 335 | return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|---|
| 336 | } |
|---|
| 337 | elseif ( 'image/png' == $mime_type ) { |
|---|
| 338 | // convert from full colors to index colors, like original PNG. |
|---|
| 339 | if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) ) |
|---|
| 340 | imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); |
|---|
| 341 | |
|---|
| 342 | $compression = -((9/100*$this->quality)-9); |
|---|
| 343 | |
|---|
| 344 | if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename, $compression ) ) ) |
|---|
| 345 | return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|---|
| 346 | } |
|---|
| 347 | elseif ( 'image/jpeg' == $mime_type ) { |
|---|
| 348 | if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, apply_filters( 'jpeg_quality', $this->quality, 'image_resize' ) ) ) ) |
|---|
| 349 | return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|---|
| 350 | } |
|---|
| 351 | else { |
|---|
| 352 | return new WP_Error( 'image_save_error', __('Image Editor Save Failed') ); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | // Set correct file permissions |
|---|
| 356 | $stat = stat( dirname( $filename ) ); |
|---|
| 357 | $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits |
|---|
| 358 | @ chmod( $filename, $perms ); |
|---|
| 359 | |
|---|
| 360 | return array( |
|---|
| 361 | 'path' => $filename, |
|---|
| 362 | 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), |
|---|
| 363 | 'width' => $this->size['width'], |
|---|
| 364 | 'height' => $this->size['height'], |
|---|
| 365 | 'mime-type'=> $mime_type, |
|---|
| 366 | ); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | /** |
|---|
| 370 | * Returns stream of current image. |
|---|
| 371 | * |
|---|
| 372 | * @since 3.5.0 |
|---|
| 373 | * @access public |
|---|
| 374 | * |
|---|
| 375 | * @param string $mime_type |
|---|
| 376 | */ |
|---|
| 377 | public function stream( $mime_type = null ) { |
|---|
| 378 | list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); |
|---|
| 379 | |
|---|
| 380 | switch ( $mime_type ) { |
|---|
| 381 | case 'image/png': |
|---|
| 382 | header( 'Content-Type: image/png' ); |
|---|
| 383 | return imagepng( $this->image ); |
|---|
| 384 | case 'image/gif': |
|---|
| 385 | header( 'Content-Type: image/gif' ); |
|---|
| 386 | return imagegif( $this->image ); |
|---|
| 387 | default: |
|---|
| 388 | header( 'Content-Type: image/jpeg' ); |
|---|
| 389 | return imagejpeg( $this->image, null, $this->quality ); |
|---|
| 390 | } |
|---|
| 391 | } |
|---|
| 392 | } |
|---|