[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-image-editor-gd.php (source)

   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          }
  73  
  74          return false;
  75      }
  76  
  77      /**
  78       * Loads image from $this->file into new GD Resource.
  79       *
  80       * @since 3.5.0
  81       *
  82       * @return bool|WP_Error True if loaded successfully; WP_Error on failure.
  83       */
  84  	public function load() {
  85          if ( $this->image ) {
  86              return true;
  87          }
  88  
  89          if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
  90              return new WP_Error( 'error_loading_image', __( 'File doesn&#8217;t exist?' ), $this->file );
  91          }
  92  
  93          // Set artificially high because GD uses uncompressed images in memory.
  94          wp_raise_memory_limit( 'image' );
  95  
  96          $file_contents = @file_get_contents( $this->file );
  97  
  98          if ( ! $file_contents ) {
  99              return new WP_Error( 'error_loading_image', __( 'File doesn&#8217;t exist?' ), $this->file );
 100          }
 101  
 102          $this->image = @imagecreatefromstring( $file_contents );
 103  
 104          if ( ! is_gd_image( $this->image ) ) {
 105              return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
 106          }
 107  
 108          $size = @getimagesize( $this->file );
 109  
 110          if ( ! $size ) {
 111              return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );
 112          }
 113  
 114          if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
 115              imagealphablending( $this->image, false );
 116              imagesavealpha( $this->image, true );
 117          }
 118  
 119          $this->update_size( $size[0], $size[1] );
 120          $this->mime_type = $size['mime'];
 121  
 122          return $this->set_quality();
 123      }
 124  
 125      /**
 126       * Sets or updates current image size.
 127       *
 128       * @since 3.5.0
 129       *
 130       * @param int $width
 131       * @param int $height
 132       * @return true
 133       */
 134  	protected function update_size( $width = false, $height = false ) {
 135          if ( ! $width ) {
 136              $width = imagesx( $this->image );
 137          }
 138  
 139          if ( ! $height ) {
 140              $height = imagesy( $this->image );
 141          }
 142  
 143          return parent::update_size( $width, $height );
 144      }
 145  
 146      /**
 147       * Resizes current image.
 148       *
 149       * Wraps `::_resize()` which returns a GD resource or GdImage instance.
 150       *
 151       * At minimum, either a height or width must be provided. If one of the two is set
 152       * to null, the resize will maintain aspect ratio according to the provided dimension.
 153       *
 154       * @since 3.5.0
 155       *
 156       * @param int|null $max_w Image width.
 157       * @param int|null $max_h Image height.
 158       * @param bool     $crop
 159       * @return true|WP_Error
 160       */
 161  	public function resize( $max_w, $max_h, $crop = false ) {
 162          if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) {
 163              return true;
 164          }
 165  
 166          $resized = $this->_resize( $max_w, $max_h, $crop );
 167  
 168          if ( is_gd_image( $resized ) ) {
 169              imagedestroy( $this->image );
 170              $this->image = $resized;
 171              return true;
 172  
 173          } elseif ( is_wp_error( $resized ) ) {
 174              return $resized;
 175          }
 176  
 177          return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file );
 178      }
 179  
 180      /**
 181       * @param int        $max_w
 182       * @param int        $max_h
 183       * @param bool|array $crop
 184       * @return resource|GdImage|WP_Error
 185       */
 186  	protected function _resize( $max_w, $max_h, $crop = false ) {
 187          $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
 188  
 189          if ( ! $dims ) {
 190              return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file );
 191          }
 192  
 193          list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
 194  
 195          $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
 196          imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
 197  
 198          if ( is_gd_image( $resized ) ) {
 199              $this->update_size( $dst_w, $dst_h );
 200              return $resized;
 201          }
 202  
 203          return new WP_Error( 'image_resize_error', __( 'Image resize failed.' ), $this->file );
 204      }
 205  
 206      /**
 207       * Create multiple smaller images from a single source.
 208       *
 209       * Attempts to create all sub-sizes and returns the meta data at the end. This
 210       * may result in the server running out of resources. When it fails there may be few
 211       * "orphaned" images left over as the meta data is never returned and saved.
 212       *
 213       * As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates
 214       * the new images one at a time and allows for the meta data to be saved after
 215       * each new image is created.
 216       *
 217       * @since 3.5.0
 218       *
 219       * @param array $sizes {
 220       *     An array of image size data arrays.
 221       *
 222       *     Either a height or width must be provided.
 223       *     If one of the two is set to null, the resize will
 224       *     maintain aspect ratio according to the source image.
 225       *
 226       *     @type array $size {
 227       *         Array of height, width values, and whether to crop.
 228       *
 229       *         @type int  $width  Image width. Optional if `$height` is specified.
 230       *         @type int  $height Image height. Optional if `$width` is specified.
 231       *         @type bool $crop   Optional. Whether to crop the image. Default false.
 232       *     }
 233       * }
 234       * @return array An array of resized images' metadata by size.
 235       */
 236  	public function multi_resize( $sizes ) {
 237          $metadata = array();
 238  
 239          foreach ( $sizes as $size => $size_data ) {
 240              $meta = $this->make_subsize( $size_data );
 241  
 242              if ( ! is_wp_error( $meta ) ) {
 243                  $metadata[ $size ] = $meta;
 244              }
 245          }
 246  
 247          return $metadata;
 248      }
 249  
 250      /**
 251       * Create an image sub-size and return the image meta data value for it.
 252       *
 253       * @since 5.3.0
 254       *
 255       * @param array $size_data {
 256       *     Array of size data.
 257       *
 258       *     @type int  $width  The maximum width in pixels.
 259       *     @type int  $height The maximum height in pixels.
 260       *     @type bool $crop   Whether to crop the image to exact dimensions.
 261       * }
 262       * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta,
 263       *                        WP_Error object on error.
 264       */
 265  	public function make_subsize( $size_data ) {
 266          if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
 267              return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
 268          }
 269  
 270          $orig_size = $this->size;
 271  
 272          if ( ! isset( $size_data['width'] ) ) {
 273              $size_data['width'] = null;
 274          }
 275  
 276          if ( ! isset( $size_data['height'] ) ) {
 277              $size_data['height'] = null;
 278          }
 279  
 280          if ( ! isset( $size_data['crop'] ) ) {
 281              $size_data['crop'] = false;
 282          }
 283  
 284          $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
 285  
 286          if ( is_wp_error( $resized ) ) {
 287              $saved = $resized;
 288          } else {
 289              $saved = $this->_save( $resized );
 290              imagedestroy( $resized );
 291          }
 292  
 293          $this->size = $orig_size;
 294  
 295          if ( ! is_wp_error( $saved ) ) {
 296              unset( $saved['path'] );
 297          }
 298  
 299          return $saved;
 300      }
 301  
 302      /**
 303       * Crops Image.
 304       *
 305       * @since 3.5.0
 306       *
 307       * @param int  $src_x   The start x position to crop from.
 308       * @param int  $src_y   The start y position to crop from.
 309       * @param int  $src_w   The width to crop.
 310       * @param int  $src_h   The height to crop.
 311       * @param int  $dst_w   Optional. The destination width.
 312       * @param int  $dst_h   Optional. The destination height.
 313       * @param bool $src_abs Optional. If the source crop points are absolute.
 314       * @return bool|WP_Error
 315       */
 316  	public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
 317          // If destination width/height isn't specified,
 318          // use same as width/height from source.
 319          if ( ! $dst_w ) {
 320              $dst_w = $src_w;
 321          }
 322          if ( ! $dst_h ) {
 323              $dst_h = $src_h;
 324          }
 325  
 326          $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
 327  
 328          if ( $src_abs ) {
 329              $src_w -= $src_x;
 330              $src_h -= $src_y;
 331          }
 332  
 333          if ( function_exists( 'imageantialias' ) ) {
 334              imageantialias( $dst, true );
 335          }
 336  
 337          imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
 338  
 339          if ( is_gd_image( $dst ) ) {
 340              imagedestroy( $this->image );
 341              $this->image = $dst;
 342              $this->update_size();
 343              return true;
 344          }
 345  
 346          return new WP_Error( 'image_crop_error', __( 'Image crop failed.' ), $this->file );
 347      }
 348  
 349      /**
 350       * Rotates current image counter-clockwise by $angle.
 351       * Ported from image-edit.php
 352       *
 353       * @since 3.5.0
 354       *
 355       * @param float $angle
 356       * @return true|WP_Error
 357       */
 358  	public function rotate( $angle ) {
 359          if ( function_exists( 'imagerotate' ) ) {
 360              $transparency = imagecolorallocatealpha( $this->image, 255, 255, 255, 127 );
 361              $rotated      = imagerotate( $this->image, $angle, $transparency );
 362  
 363              if ( is_gd_image( $rotated ) ) {
 364                  imagealphablending( $rotated, true );
 365                  imagesavealpha( $rotated, true );
 366                  imagedestroy( $this->image );
 367                  $this->image = $rotated;
 368                  $this->update_size();
 369                  return true;
 370              }
 371          }
 372  
 373          return new WP_Error( 'image_rotate_error', __( 'Image rotate failed.' ), $this->file );
 374      }
 375  
 376      /**
 377       * Flips current image.
 378       *
 379       * @since 3.5.0
 380       *
 381       * @param bool $horz Flip along Horizontal Axis.
 382       * @param bool $vert Flip along Vertical Axis.
 383       * @return true|WP_Error
 384       */
 385  	public function flip( $horz, $vert ) {
 386          $w   = $this->size['width'];
 387          $h   = $this->size['height'];
 388          $dst = wp_imagecreatetruecolor( $w, $h );
 389  
 390          if ( is_gd_image( $dst ) ) {
 391              $sx = $vert ? ( $w - 1 ) : 0;
 392              $sy = $horz ? ( $h - 1 ) : 0;
 393              $sw = $vert ? -$w : $w;
 394              $sh = $horz ? -$h : $h;
 395  
 396              if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
 397                  imagedestroy( $this->image );
 398                  $this->image = $dst;
 399                  return true;
 400              }
 401          }
 402  
 403          return new WP_Error( 'image_flip_error', __( 'Image flip failed.' ), $this->file );
 404      }
 405  
 406      /**
 407       * Saves current in-memory image to file.
 408       *
 409       * @since 3.5.0
 410       *
 411       * @param string|null $filename
 412       * @param string|null $mime_type
 413       * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
 414       */
 415  	public function save( $filename = null, $mime_type = null ) {
 416          $saved = $this->_save( $this->image, $filename, $mime_type );
 417  
 418          if ( ! is_wp_error( $saved ) ) {
 419              $this->file      = $saved['path'];
 420              $this->mime_type = $saved['mime-type'];
 421          }
 422  
 423          return $saved;
 424      }
 425  
 426      /**
 427       * @param resource|GdImage $image
 428       * @param string|null      $filename
 429       * @param string|null      $mime_type
 430       * @return array|WP_Error
 431       */
 432  	protected function _save( $image, $filename = null, $mime_type = null ) {
 433          list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
 434  
 435          if ( ! $filename ) {
 436              $filename = $this->generate_filename( null, null, $extension );
 437          }
 438  
 439          if ( 'image/gif' === $mime_type ) {
 440              if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) {
 441                  return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
 442              }
 443          } elseif ( 'image/png' === $mime_type ) {
 444              // Convert from full colors to index colors, like original PNG.
 445              if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) {
 446                  imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
 447              }
 448  
 449              if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) ) {
 450                  return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
 451              }
 452          } elseif ( 'image/jpeg' === $mime_type ) {
 453              if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) {
 454                  return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
 455              }
 456          } else {
 457              return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) );
 458          }
 459  
 460          // Set correct file permissions.
 461          $stat  = stat( dirname( $filename ) );
 462          $perms = $stat['mode'] & 0000666; // Same permissions as parent folder, strip off the executable bits.
 463          chmod( $filename, $perms );
 464  
 465          return array(
 466              'path'      => $filename,
 467              /**
 468               * Filters the name of the saved image file.
 469               *
 470               * @since 2.6.0
 471               *
 472               * @param string $filename Name of the file.
 473               */
 474              'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
 475              'width'     => $this->size['width'],
 476              'height'    => $this->size['height'],
 477              'mime-type' => $mime_type,
 478          );
 479      }
 480  
 481      /**
 482       * Returns stream of current image.
 483       *
 484       * @since 3.5.0
 485       *
 486       * @param string $mime_type The mime type of the image.
 487       * @return bool True on success, false on failure.
 488       */
 489  	public function stream( $mime_type = null ) {
 490          list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
 491  
 492          switch ( $mime_type ) {
 493              case 'image/png':
 494                  header( 'Content-Type: image/png' );
 495                  return imagepng( $this->image );
 496              case 'image/gif':
 497                  header( 'Content-Type: image/gif' );
 498                  return imagegif( $this->image );
 499              default:
 500                  header( 'Content-Type: image/jpeg' );
 501                  return imagejpeg( $this->image, null, $this->get_quality() );
 502          }
 503      }
 504  
 505      /**
 506       * Either calls editor's save function or handles file as a stream.
 507       *
 508       * @since 3.5.0
 509       *
 510       * @param string|stream $filename
 511       * @param callable      $function
 512       * @param array         $arguments
 513       * @return bool
 514       */
 515  	protected function make_image( $filename, $function, $arguments ) {
 516          if ( wp_is_stream( $filename ) ) {
 517              $arguments[1] = null;
 518          }
 519  
 520          return parent::make_image( $filename, $function, $arguments );
 521      }
 522  }


Generated : Sat Oct 31 08:20:01 2020 Cross-referenced by PHPXref