[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 * 14 * @see WP_Image_Editor 15 */ 16 class WP_Image_Editor_GD extends WP_Image_Editor { 17 /** 18 * GD Resource. 19 * 20 * @var resource|GdImage 21 */ 22 protected $image; 23 24 public function __destruct() { 25 if ( $this->image ) { 26 // We don't need the original in memory anymore. 27 imagedestroy( $this->image ); 28 } 29 } 30 31 /** 32 * Checks to see if current environment supports GD. 33 * 34 * @since 3.5.0 35 * 36 * @param array $args 37 * @return bool 38 */ 39 public static function test( $args = array() ) { 40 if ( ! extension_loaded( 'gd' ) || ! function_exists( 'gd_info' ) ) { 41 return false; 42 } 43 44 // On some setups GD library does not provide imagerotate() - Ticket #11536. 45 if ( isset( $args['methods'] ) && 46 in_array( 'rotate', $args['methods'], true ) && 47 ! function_exists( 'imagerotate' ) ) { 48 49 return false; 50 } 51 52 return true; 53 } 54 55 /** 56 * Checks to see if editor supports the mime-type specified. 57 * 58 * @since 3.5.0 59 * 60 * @param string $mime_type 61 * @return bool 62 */ 63 public static function supports_mime_type( $mime_type ) { 64 $image_types = imagetypes(); 65 switch ( $mime_type ) { 66 case 'image/jpeg': 67 return ( $image_types & IMG_JPG ) !== 0; 68 case 'image/png': 69 return ( $image_types & IMG_PNG ) !== 0; 70 case 'image/gif': 71 return ( $image_types & IMG_GIF ) !== 0; 72 case 'image/webp': 73 return ( $image_types & IMG_WEBP ) !== 0; 74 case 'image/avif': 75 return ( $image_types & IMG_AVIF ) !== 0; 76 } 77 78 return false; 79 } 80 81 /** 82 * Loads image from $this->file into new GD Resource. 83 * 84 * @since 3.5.0 85 * 86 * @return true|WP_Error True if loaded successfully; WP_Error on failure. 87 */ 88 public function load() { 89 if ( $this->image ) { 90 return true; 91 } 92 93 if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) { 94 return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file ); 95 } 96 97 // Set artificially high because GD uses uncompressed images in memory. 98 wp_raise_memory_limit( 'image' ); 99 100 $file_contents = @file_get_contents( $this->file ); 101 102 if ( ! $file_contents ) { 103 return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file ); 104 } 105 106 // WebP may not work with imagecreatefromstring(). 107 if ( 108 function_exists( 'imagecreatefromwebp' ) && 109 ( 'image/webp' === wp_get_image_mime( $this->file ) ) 110 ) { 111 $this->image = @imagecreatefromwebp( $this->file ); 112 } else { 113 $this->image = @imagecreatefromstring( $file_contents ); 114 } 115 116 // AVIF may not work with imagecreatefromstring(). 117 if ( 118 function_exists( 'imagecreatefromavif' ) && 119 ( 'image/avif' === wp_get_image_mime( $this->file ) ) 120 ) { 121 $this->image = @imagecreatefromavif( $this->file ); 122 } else { 123 $this->image = @imagecreatefromstring( $file_contents ); 124 } 125 126 if ( ! is_gd_image( $this->image ) ) { 127 return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); 128 } 129 130 $size = wp_getimagesize( $this->file ); 131 132 if ( ! $size ) { 133 return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file ); 134 } 135 136 if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { 137 imagealphablending( $this->image, false ); 138 imagesavealpha( $this->image, true ); 139 } 140 141 $this->update_size( $size[0], $size[1] ); 142 $this->mime_type = $size['mime']; 143 144 return $this->set_quality(); 145 } 146 147 /** 148 * Sets or updates current image size. 149 * 150 * @since 3.5.0 151 * 152 * @param int $width 153 * @param int $height 154 * @return true 155 */ 156 protected function update_size( $width = false, $height = false ) { 157 if ( ! $width ) { 158 $width = imagesx( $this->image ); 159 } 160 161 if ( ! $height ) { 162 $height = imagesy( $this->image ); 163 } 164 165 return parent::update_size( $width, $height ); 166 } 167 168 /** 169 * Resizes current image. 170 * 171 * Wraps `::_resize()` which returns a GD resource or GdImage instance. 172 * 173 * At minimum, either a height or width must be provided. If one of the two is set 174 * to null, the resize will maintain aspect ratio according to the provided dimension. 175 * 176 * @since 3.5.0 177 * 178 * @param int|null $max_w Image width. 179 * @param int|null $max_h Image height. 180 * @param bool|array $crop { 181 * Optional. Image cropping behavior. If false, the image will be scaled (default). 182 * If true, image will be cropped to the specified dimensions using center positions. 183 * If an array, the image will be cropped using the array to specify the crop location: 184 * 185 * @type string $0 The x crop position. Accepts 'left' 'center', or 'right'. 186 * @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'. 187 * } 188 * @return true|WP_Error 189 */ 190 public function resize( $max_w, $max_h, $crop = false ) { 191 if ( ( $this->size['width'] === $max_w ) && ( $this->size['height'] === $max_h ) ) { 192 return true; 193 } 194 195 $resized = $this->_resize( $max_w, $max_h, $crop ); 196 197 if ( is_gd_image( $resized ) ) { 198 imagedestroy( $this->image ); 199 $this->image = $resized; 200 return true; 201 202 } elseif ( is_wp_error( $resized ) ) { 203 return $resized; 204 } 205 206 return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); 207 } 208 209 /** 210 * @param int $max_w 211 * @param int $max_h 212 * @param bool|array $crop { 213 * Optional. Image cropping behavior. If false, the image will be scaled (default). 214 * If true, image will be cropped to the specified dimensions using center positions. 215 * If an array, the image will be cropped using the array to specify the crop location: 216 * 217 * @type string $0 The x crop position. Accepts 'left' 'center', or 'right'. 218 * @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'. 219 * } 220 * @return resource|GdImage|WP_Error 221 */ 222 protected function _resize( $max_w, $max_h, $crop = false ) { 223 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 224 225 if ( ! $dims ) { 226 return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file ); 227 } 228 229 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 230 231 $resized = wp_imagecreatetruecolor( $dst_w, $dst_h ); 232 imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); 233 234 if ( is_gd_image( $resized ) ) { 235 $this->update_size( $dst_w, $dst_h ); 236 return $resized; 237 } 238 239 return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file ); 240 } 241 242 /** 243 * Create multiple smaller images from a single source. 244 * 245 * Attempts to create all sub-sizes and returns the meta data at the end. This 246 * may result in the server running out of resources. When it fails there may be few 247 * "orphaned" images left over as the meta data is never returned and saved. 248 * 249 * As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates 250 * the new images one at a time and allows for the meta data to be saved after 251 * each new image is created. 252 * 253 * @since 3.5.0 254 * 255 * @param array $sizes { 256 * An array of image size data arrays. 257 * 258 * Either a height or width must be provided. 259 * If one of the two is set to null, the resize will 260 * maintain aspect ratio according to the source image. 261 * 262 * @type array ...$0 { 263 * Array of height, width values, and whether to crop. 264 * 265 * @type int $width Image width. Optional if `$height` is specified. 266 * @type int $height Image height. Optional if `$width` is specified. 267 * @type bool|array $crop Optional. Whether to crop the image. Default false. 268 * } 269 * } 270 * @return array An array of resized images' metadata by size. 271 */ 272 public function multi_resize( $sizes ) { 273 $metadata = array(); 274 275 foreach ( $sizes as $size => $size_data ) { 276 $meta = $this->make_subsize( $size_data ); 277 278 if ( ! is_wp_error( $meta ) ) { 279 $metadata[ $size ] = $meta; 280 } 281 } 282 283 return $metadata; 284 } 285 286 /** 287 * Create an image sub-size and return the image meta data value for it. 288 * 289 * @since 5.3.0 290 * 291 * @param array $size_data { 292 * Array of size data. 293 * 294 * @type int $width The maximum width in pixels. 295 * @type int $height The maximum height in pixels. 296 * @type bool|array $crop Whether to crop the image to exact dimensions. 297 * } 298 * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, 299 * WP_Error object on error. 300 */ 301 public function make_subsize( $size_data ) { 302 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 303 return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); 304 } 305 306 $orig_size = $this->size; 307 308 if ( ! isset( $size_data['width'] ) ) { 309 $size_data['width'] = null; 310 } 311 312 if ( ! isset( $size_data['height'] ) ) { 313 $size_data['height'] = null; 314 } 315 316 if ( ! isset( $size_data['crop'] ) ) { 317 $size_data['crop'] = false; 318 } 319 320 $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 321 322 if ( is_wp_error( $resized ) ) { 323 $saved = $resized; 324 } else { 325 $saved = $this->_save( $resized ); 326 imagedestroy( $resized ); 327 } 328 329 $this->size = $orig_size; 330 331 if ( ! is_wp_error( $saved ) ) { 332 unset( $saved['path'] ); 333 } 334 335 return $saved; 336 } 337 338 /** 339 * Crops Image. 340 * 341 * @since 3.5.0 342 * 343 * @param int $src_x The start x position to crop from. 344 * @param int $src_y The start y position to crop from. 345 * @param int $src_w The width to crop. 346 * @param int $src_h The height to crop. 347 * @param int $dst_w Optional. The destination width. 348 * @param int $dst_h Optional. The destination height. 349 * @param bool $src_abs Optional. If the source crop points are absolute. 350 * @return true|WP_Error 351 */ 352 public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) { 353 /* 354 * If destination width/height isn't specified, 355 * use same as width/height from source. 356 */ 357 if ( ! $dst_w ) { 358 $dst_w = $src_w; 359 } 360 if ( ! $dst_h ) { 361 $dst_h = $src_h; 362 } 363 364 foreach ( array( $src_w, $src_h, $dst_w, $dst_h ) as $value ) { 365 if ( ! is_numeric( $value ) || (int) $value <= 0 ) { 366 return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); 367 } 368 } 369 370 $dst = wp_imagecreatetruecolor( (int) $dst_w, (int) $dst_h ); 371 372 if ( $src_abs ) { 373 $src_w -= $src_x; 374 $src_h -= $src_y; 375 } 376 377 if ( function_exists( 'imageantialias' ) ) { 378 imageantialias( $dst, true ); 379 } 380 381 imagecopyresampled( $dst, $this->image, 0, 0, (int) $src_x, (int) $src_y, (int) $dst_w, (int) $dst_h, (int) $src_w, (int) $src_h ); 382 383 if ( is_gd_image( $dst ) ) { 384 imagedestroy( $this->image ); 385 $this->image = $dst; 386 $this->update_size(); 387 return true; 388 } 389 390 return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file ); 391 } 392 393 /** 394 * Rotates current image counter-clockwise by $angle. 395 * Ported from image-edit.php 396 * 397 * @since 3.5.0 398 * 399 * @param float $angle 400 * @return true|WP_Error 401 */ 402 public function rotate( $angle ) { 403 if ( function_exists( 'imagerotate' ) ) { 404 $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 ); 405 $rotated = imagerotate( $this->image, $angle, $transparency ); 406 407 if ( is_gd_image( $rotated ) ) { 408 imagealphablending( $rotated, true ); 409 imagesavealpha( $rotated, true ); 410 imagedestroy( $this->image ); 411 $this->image = $rotated; 412 $this->update_size(); 413 return true; 414 } 415 } 416 417 return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file ); 418 } 419 420 /** 421 * Flips current image. 422 * 423 * @since 3.5.0 424 * 425 * @param bool $horz Flip along Horizontal Axis. 426 * @param bool $vert Flip along Vertical Axis. 427 * @return true|WP_Error 428 */ 429 public function flip( $horz, $vert ) { 430 $w = $this->size['width']; 431 $h = $this->size['height']; 432 $dst = wp_imagecreatetruecolor( $w, $h ); 433 434 if ( is_gd_image( $dst ) ) { 435 $sx = $vert ? ( $w - 1 ) : 0; 436 $sy = $horz ? ( $h - 1 ) : 0; 437 $sw = $vert ? -$w : $w; 438 $sh = $horz ? -$h : $h; 439 440 if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) { 441 imagedestroy( $this->image ); 442 $this->image = $dst; 443 return true; 444 } 445 } 446 447 return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file ); 448 } 449 450 /** 451 * Saves current in-memory image to file. 452 * 453 * @since 3.5.0 454 * @since 5.9.0 Renamed `$filename` to `$destfilename` to match parent class 455 * for PHP 8 named parameter support. 456 * @since 6.0.0 The `$filesize` value was added to the returned array. 457 * 458 * @param string|null $destfilename Optional. Destination filename. Default null. 459 * @param string|null $mime_type Optional. The mime-type. Default null. 460 * @return array|WP_Error { 461 * Array on success or WP_Error if the file failed to save. 462 * 463 * @type string $path Path to the image file. 464 * @type string $file Name of the image file. 465 * @type int $width Image width. 466 * @type int $height Image height. 467 * @type string $mime-type The mime type of the image. 468 * @type int $filesize File size of the image. 469 * } 470 */ 471 public function save( $destfilename = null, $mime_type = null ) { 472 $saved = $this->_save( $this->image, $destfilename, $mime_type ); 473 474 if ( ! is_wp_error( $saved ) ) { 475 $this->file = $saved['path']; 476 $this->mime_type = $saved['mime-type']; 477 } 478 479 return $saved; 480 } 481 482 /** 483 * @since 3.5.0 484 * @since 6.0.0 The `$filesize` value was added to the returned array. 485 * 486 * @param resource|GdImage $image 487 * @param string|null $filename 488 * @param string|null $mime_type 489 * @return array|WP_Error { 490 * Array on success or WP_Error if the file failed to save. 491 * 492 * @type string $path Path to the image file. 493 * @type string $file Name of the image file. 494 * @type int $width Image width. 495 * @type int $height Image height. 496 * @type string $mime-type The mime type of the image. 497 * @type int $filesize File size of the image. 498 * } 499 */ 500 protected function _save( $image, $filename = null, $mime_type = null ) { 501 list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type ); 502 503 if ( ! $filename ) { 504 $filename = $this->generate_filename( null, null, $extension ); 505 } 506 507 if ( function_exists( 'imageinterlace' ) ) { 508 /** 509 * Filters whether to output progressive images (if available). 510 * 511 * @since 6.5.0 512 * 513 * @param bool $interlace Whether to use progressive images for output if available. Default false. 514 * @param string $mime_type The mime type being saved. 515 */ 516 imageinterlace( $image, apply_filters( 'image_save_progressive', false, $mime_type ) ); 517 } 518 519 if ( 'image/gif' === $mime_type ) { 520 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { 521 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 522 } 523 } elseif ( 'image/png' === $mime_type ) { 524 // Convert from full colors to index colors, like original PNG. 525 if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) { 526 imagetruecolortopalette( $image, false, imagecolorstotal( $image ) ); 527 } 528 529 if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) { 530 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 531 } 532 } elseif ( 'image/jpeg' === $mime_type ) { 533 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) { 534 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 535 } 536 } elseif ( 'image/webp' === $mime_type ) { 537 if ( ! function_exists( 'imagewebp' ) 538 || ! $this->make_image( $filename, 'imagewebp', array( $image, $filename, $this->get_quality() ) ) 539 ) { 540 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 541 } 542 } elseif ( 'image/avif' === $mime_type ) { 543 if ( ! function_exists( 'imageavif' ) 544 || ! $this->make_image( $filename, 'imageavif', array( $image, $filename, $this->get_quality() ) ) 545 ) { 546 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 547 } 548 } else { 549 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 550 } 551 552 // Set correct file permissions. 553 $stat = stat( dirname( $filename ) ); 554 $perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits. 555 chmod( $filename, $perms ); 556 557 return array( 558 'path' => $filename, 559 /** 560 * Filters the name of the saved image file. 561 * 562 * @since 2.6.0 563 * 564 * @param string $filename Name of the file. 565 */ 566 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ), 567 'width' => $this->size['width'], 568 'height' => $this->size['height'], 569 'mime-type' => $mime_type, 570 'filesize' => wp_filesize( $filename ), 571 ); 572 } 573 574 /** 575 * Returns stream of current image. 576 * 577 * @since 3.5.0 578 * 579 * @param string $mime_type The mime type of the image. 580 * @return bool True on success, false on failure. 581 */ 582 public function stream( $mime_type = null ) { 583 list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type ); 584 585 switch ( $mime_type ) { 586 case 'image/png': 587 header( 'Content-Type: image/png' ); 588 return imagepng( $this->image ); 589 case 'image/gif': 590 header( 'Content-Type: image/gif' ); 591 return imagegif( $this->image ); 592 case 'image/webp': 593 if ( function_exists( 'imagewebp' ) ) { 594 header( 'Content-Type: image/webp' ); 595 return imagewebp( $this->image, null, $this->get_quality() ); 596 } else { 597 // Fall back to JPEG. 598 header( 'Content-Type: image/jpeg' ); 599 return imagejpeg( $this->image, null, $this->get_quality() ); 600 } 601 case 'image/avif': 602 if ( function_exists( 'imageavif' ) ) { 603 header( 'Content-Type: image/avif' ); 604 return imageavif( $this->image, null, $this->get_quality() ); 605 } 606 // Fall back to JPEG. 607 default: 608 header( 'Content-Type: image/jpeg' ); 609 return imagejpeg( $this->image, null, $this->get_quality() ); 610 } 611 } 612 613 /** 614 * Either calls editor's save function or handles file as a stream. 615 * 616 * @since 3.5.0 617 * 618 * @param string $filename 619 * @param callable $callback 620 * @param array $arguments 621 * @return bool 622 */ 623 protected function make_image( $filename, $callback, $arguments ) { 624 if ( wp_is_stream( $filename ) ) { 625 $arguments[1] = null; 626 } 627 628 return parent::make_image( $filename, $callback, $arguments ); 629 } 630 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu May 9 08:20:02 2024 | Cross-referenced by PHPXref |