[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress Imagick Image Editor
   4   *
   5   * @package WordPress
   6   * @subpackage Image_Editor
   7   */
   8  
   9  /**
  10   * WordPress Image Editor Class for Image Manipulation through Imagick PHP Module
  11   *
  12   * @since 3.5.0
  13   *
  14   * @see WP_Image_Editor
  15   */
  16  class WP_Image_Editor_Imagick extends WP_Image_Editor {
  17      /**
  18       * Imagick object.
  19       *
  20       * @var Imagick
  21       */
  22      protected $image;
  23  
  24  	public function __destruct() {
  25          if ( $this->image instanceof Imagick ) {
  26              // we don't need the original in memory anymore
  27              $this->image->clear();
  28              $this->image->destroy();
  29          }
  30      }
  31  
  32      /**
  33       * Checks to see if current environment supports Imagick.
  34       *
  35       * We require Imagick 2.2.0 or greater, based on whether the queryFormats()
  36       * method can be called statically.
  37       *
  38       * @since 3.5.0
  39       *
  40       * @param array $args
  41       * @return bool
  42       */
  43  	public static function test( $args = array() ) {
  44  
  45          // First, test Imagick's extension and classes.
  46          if ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick', false ) || ! class_exists( 'ImagickPixel', false ) ) {
  47              return false;
  48          }
  49  
  50          if ( version_compare( phpversion( 'imagick' ), '2.2.0', '<' ) ) {
  51              return false;
  52          }
  53  
  54          $required_methods = array(
  55              'clear',
  56              'destroy',
  57              'valid',
  58              'getimage',
  59              'writeimage',
  60              'getimageblob',
  61              'getimagegeometry',
  62              'getimageformat',
  63              'setimageformat',
  64              'setimagecompression',
  65              'setimagecompressionquality',
  66              'setimagepage',
  67              'setoption',
  68              'scaleimage',
  69              'cropimage',
  70              'rotateimage',
  71              'flipimage',
  72              'flopimage',
  73              'readimage',
  74          );
  75  
  76          // Now, test for deep requirements within Imagick.
  77          if ( ! defined( 'imagick::COMPRESSION_JPEG' ) ) {
  78              return false;
  79          }
  80  
  81          $class_methods = array_map( 'strtolower', get_class_methods( 'Imagick' ) );
  82          if ( array_diff( $required_methods, $class_methods ) ) {
  83              return false;
  84          }
  85  
  86          return true;
  87      }
  88  
  89      /**
  90       * Checks to see if editor supports the mime-type specified.
  91       *
  92       * @since 3.5.0
  93       *
  94       * @param string $mime_type
  95       * @return bool
  96       */
  97  	public static function supports_mime_type( $mime_type ) {
  98          $imagick_extension = strtoupper( self::get_extension( $mime_type ) );
  99  
 100          if ( ! $imagick_extension ) {
 101              return false;
 102          }
 103  
 104          // setIteratorIndex is optional unless mime is an animated format.
 105          // Here, we just say no if you are missing it and aren't loading a jpeg.
 106          if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type !== 'image/jpeg' ) {
 107                  return false;
 108          }
 109  
 110          try {
 111              // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
 112              return ( (bool) @Imagick::queryFormats( $imagick_extension ) );
 113          } catch ( Exception $e ) {
 114              return false;
 115          }
 116      }
 117  
 118      /**
 119       * Loads image from $this->file into new Imagick Object.
 120       *
 121       * @since 3.5.0
 122       *
 123       * @return true|WP_Error True if loaded; WP_Error on failure.
 124       */
 125  	public function load() {
 126          if ( $this->image instanceof Imagick ) {
 127              return true;
 128          }
 129  
 130          if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {
 131              return new WP_Error( 'error_loading_image', __( 'File doesn&#8217;t exist?' ), $this->file );
 132          }
 133  
 134          /*
 135           * Even though Imagick uses less PHP memory than GD, set higher limit
 136           * for users that have low PHP.ini limits.
 137           */
 138          wp_raise_memory_limit( 'image' );
 139  
 140          try {
 141              $this->image    = new Imagick();
 142              $file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
 143              $filename       = $this->file;
 144  
 145              if ( 'pdf' === $file_extension ) {
 146                  $filename = $this->pdf_setup();
 147              }
 148  
 149              // Reading image after Imagick instantiation because `setResolution`
 150              // only applies correctly before the image is read.
 151              $this->image->readImage( $filename );
 152  
 153              if ( ! $this->image->valid() ) {
 154                  return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
 155              }
 156  
 157              // Select the first frame to handle animated images properly
 158              if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) ) {
 159                  $this->image->setIteratorIndex( 0 );
 160              }
 161  
 162              $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
 163          } catch ( Exception $e ) {
 164              return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
 165          }
 166  
 167          $updated_size = $this->update_size();
 168          if ( is_wp_error( $updated_size ) ) {
 169              return $updated_size;
 170          }
 171  
 172          return $this->set_quality();
 173      }
 174  
 175      /**
 176       * Sets Image Compression quality on a 1-100% scale.
 177       *
 178       * @since 3.5.0
 179       *
 180       * @param int $quality Compression Quality. Range: [1,100]
 181       * @return true|WP_Error True if set successfully; WP_Error on failure.
 182       */
 183  	public function set_quality( $quality = null ) {
 184          $quality_result = parent::set_quality( $quality );
 185          if ( is_wp_error( $quality_result ) ) {
 186              return $quality_result;
 187          } else {
 188              $quality = $this->get_quality();
 189          }
 190  
 191          try {
 192              if ( 'image/jpeg' === $this->mime_type ) {
 193                  $this->image->setImageCompressionQuality( $quality );
 194                  $this->image->setImageCompression( imagick::COMPRESSION_JPEG );
 195              } else {
 196                  $this->image->setImageCompressionQuality( $quality );
 197              }
 198          } catch ( Exception $e ) {
 199              return new WP_Error( 'image_quality_error', $e->getMessage() );
 200          }
 201  
 202          return true;
 203      }
 204  
 205      /**
 206       * Sets or updates current image size.
 207       *
 208       * @since 3.5.0
 209       *
 210       * @param int $width
 211       * @param int $height
 212       *
 213       * @return true|WP_Error
 214       */
 215  	protected function update_size( $width = null, $height = null ) {
 216          $size = null;
 217          if ( ! $width || ! $height ) {
 218              try {
 219                  $size = $this->image->getImageGeometry();
 220              } catch ( Exception $e ) {
 221                  return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );
 222              }
 223          }
 224  
 225          if ( ! $width ) {
 226              $width = $size['width'];
 227          }
 228  
 229          if ( ! $height ) {
 230              $height = $size['height'];
 231          }
 232  
 233          return parent::update_size( $width, $height );
 234      }
 235  
 236      /**
 237       * Resizes current image.
 238       *
 239       * At minimum, either a height or width must be provided.
 240       * If one of the two is set to null, the resize will
 241       * maintain aspect ratio according to the provided dimension.
 242       *
 243       * @since 3.5.0
 244       *
 245       * @param  int|null $max_w Image width.
 246       * @param  int|null $max_h Image height.
 247       * @param  bool     $crop
 248       * @return bool|WP_Error
 249       */
 250  	public function resize( $max_w, $max_h, $crop = false ) {
 251          if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) {
 252              return true;
 253          }
 254  
 255          $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
 256          if ( ! $dims ) {
 257              return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) );
 258          }
 259  
 260          list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
 261  
 262          if ( $crop ) {
 263              return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
 264          }
 265  
 266          // Execute the resize
 267          $thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
 268          if ( is_wp_error( $thumb_result ) ) {
 269              return $thumb_result;
 270          }
 271  
 272          return $this->update_size( $dst_w, $dst_h );
 273      }
 274  
 275      /**
 276       * Efficiently resize the current image
 277       *
 278       * This is a WordPress specific implementation of Imagick::thumbnailImage(),
 279       * which resizes an image to given dimensions and removes any associated profiles.
 280       *
 281       * @since 4.5.0
 282       *
 283       * @param int    $dst_w       The destination width.
 284       * @param int    $dst_h       The destination height.
 285       * @param string $filter_name Optional. The Imagick filter to use when resizing. Default 'FILTER_TRIANGLE'.
 286       * @param bool   $strip_meta  Optional. Strip all profiles, excluding color profiles, from the image. Default true.
 287       * @return bool|WP_Error
 288       */
 289  	protected function thumbnail_image( $dst_w, $dst_h, $filter_name = 'FILTER_TRIANGLE', $strip_meta = true ) {
 290          $allowed_filters = array(
 291              'FILTER_POINT',
 292              'FILTER_BOX',
 293              'FILTER_TRIANGLE',
 294              'FILTER_HERMITE',
 295              'FILTER_HANNING',
 296              'FILTER_HAMMING',
 297              'FILTER_BLACKMAN',
 298              'FILTER_GAUSSIAN',
 299              'FILTER_QUADRATIC',
 300              'FILTER_CUBIC',
 301              'FILTER_CATROM',
 302              'FILTER_MITCHELL',
 303              'FILTER_LANCZOS',
 304              'FILTER_BESSEL',
 305              'FILTER_SINC',
 306          );
 307  
 308          /**
 309           * Set the filter value if '$filter_name' name is in our whitelist and the related
 310           * Imagick constant is defined or fall back to our default filter.
 311           */
 312          if ( in_array( $filter_name, $allowed_filters, true ) && defined( 'Imagick::' . $filter_name ) ) {
 313              $filter = constant( 'Imagick::' . $filter_name );
 314          } else {
 315              $filter = defined( 'Imagick::FILTER_TRIANGLE' ) ? Imagick::FILTER_TRIANGLE : false;
 316          }
 317  
 318          /**
 319           * Filters whether to strip metadata from images when they're resized.
 320           *
 321           * This filter only applies when resizing using the Imagick editor since GD
 322           * always strips profiles by default.
 323           *
 324           * @since 4.5.0
 325           *
 326           * @param bool $strip_meta Whether to strip image metadata during resizing. Default true.
 327           */
 328          if ( apply_filters( 'image_strip_meta', $strip_meta ) ) {
 329              $this->strip_meta(); // Fail silently if not supported.
 330          }
 331  
 332          try {
 333              /*
 334               * To be more efficient, resample large images to 5x the destination size before resizing
 335               * whenever the output size is less that 1/3 of the original image size (1/3^2 ~= .111),
 336               * unless we would be resampling to a scale smaller than 128x128.
 337               */
 338              if ( is_callable( array( $this->image, 'sampleImage' ) ) ) {
 339                  $resize_ratio  = ( $dst_w / $this->size['width'] ) * ( $dst_h / $this->size['height'] );
 340                  $sample_factor = 5;
 341  
 342                  if ( $resize_ratio < .111 && ( $dst_w * $sample_factor > 128 && $dst_h * $sample_factor > 128 ) ) {
 343                      $this->image->sampleImage( $dst_w * $sample_factor, $dst_h * $sample_factor );
 344                  }
 345              }
 346  
 347              /*
 348               * Use resizeImage() when it's available and a valid filter value is set.
 349               * Otherwise, fall back to the scaleImage() method for resizing, which
 350               * results in better image quality over resizeImage() with default filter
 351               * settings and retains backward compatibility with pre 4.5 functionality.
 352               */
 353              if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) {
 354                  $this->image->setOption( 'filter:support', '2.0' );
 355                  $this->image->resizeImage( $dst_w, $dst_h, $filter, 1 );
 356              } else {
 357                  $this->image->scaleImage( $dst_w, $dst_h );
 358              }
 359  
 360              // Set appropriate quality settings after resizing.
 361              if ( 'image/jpeg' === $this->mime_type ) {
 362                  if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) {
 363                      $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 );
 364                  }
 365  
 366                  $this->image->setOption( 'jpeg:fancy-upsampling', 'off' );
 367              }
 368  
 369              if ( 'image/png' === $this->mime_type ) {
 370                  $this->image->setOption( 'png:compression-filter', '5' );
 371                  $this->image->setOption( 'png:compression-level', '9' );
 372                  $this->image->setOption( 'png:compression-strategy', '1' );
 373                  $this->image->setOption( 'png:exclude-chunk', 'all' );
 374              }
 375  
 376              /*
 377               * If alpha channel is not defined, set it opaque.
 378               *
 379               * Note that Imagick::getImageAlphaChannel() is only available if Imagick
 380               * has been compiled against ImageMagick version 6.4.0 or newer.
 381               */
 382              if ( is_callable( array( $this->image, 'getImageAlphaChannel' ) )
 383                  && is_callable( array( $this->image, 'setImageAlphaChannel' ) )
 384                  && defined( 'Imagick::ALPHACHANNEL_UNDEFINED' )
 385                  && defined( 'Imagick::ALPHACHANNEL_OPAQUE' )
 386              ) {
 387                  if ( $this->image->getImageAlphaChannel() === Imagick::ALPHACHANNEL_UNDEFINED ) {
 388                      $this->image->setImageAlphaChannel( Imagick::ALPHACHANNEL_OPAQUE );
 389                  }
 390              }
 391  
 392              // Limit the bit depth of resized images to 8 bits per channel.
 393              if ( is_callable( array( $this->image, 'getImageDepth' ) ) && is_callable( array( $this->image, 'setImageDepth' ) ) ) {
 394                  if ( 8 < $this->image->getImageDepth() ) {
 395                      $this->image->setImageDepth( 8 );
 396                  }
 397              }
 398  
 399              if ( is_callable( array( $this->image, 'setInterlaceScheme' ) ) && defined( 'Imagick::INTERLACE_NO' ) ) {
 400                  $this->image->setInterlaceScheme( Imagick::INTERLACE_NO );
 401              }
 402          } catch ( Exception $e ) {
 403              return new WP_Error( 'image_resize_error', $e->getMessage() );
 404          }
 405      }
 406  
 407      /**
 408       * Create multiple smaller images from a single source.
 409       *
 410       * Attempts to create all sub-sizes and returns the meta data at the end. This
 411       * may result in the server running out of resources. When it fails there may be few
 412       * "orphaned" images left over as the meta data is never returned and saved.
 413       *
 414       * As of 5.3.0 the preferred way to do this is with `make_subsize()`. It creates
 415       * the new images one at a time and allows for the meta data to be saved after
 416       * each new image is created.
 417       *
 418       * @since 3.5.0
 419       *
 420       * @param array $sizes {
 421       *     An array of image size data arrays.
 422       *
 423       *     Either a height or width must be provided.
 424       *     If one of the two is set to null, the resize will
 425       *     maintain aspect ratio according to the provided dimension.
 426       *
 427       *     @type array $size {
 428       *         Array of height, width values, and whether to crop.
 429       *
 430       *         @type int  $width  Image width. Optional if `$height` is specified.
 431       *         @type int  $height Image height. Optional if `$width` is specified.
 432       *         @type bool $crop   Optional. Whether to crop the image. Default false.
 433       *     }
 434       * }
 435       * @return array An array of resized images' metadata by size.
 436       */
 437  	public function multi_resize( $sizes ) {
 438          $metadata = array();
 439  
 440          foreach ( $sizes as $size => $size_data ) {
 441              $meta = $this->make_subsize( $size_data );
 442  
 443              if ( ! is_wp_error( $meta ) ) {
 444                  $metadata[ $size ] = $meta;
 445              }
 446          }
 447  
 448          return $metadata;
 449      }
 450  
 451      /**
 452       * Create an image sub-size and return the image meta data value for it.
 453       *
 454       * @since 5.3.0
 455       *
 456       * @param array $size_data Array of width, height, and whether to crop.
 457       * @return WP_Error|array WP_Error on error, or the image data array for inclusion in the `sizes` array in the image meta.
 458       */
 459  	public function make_subsize( $size_data ) {
 460          if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
 461              return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
 462          }
 463  
 464          $orig_size  = $this->size;
 465          $orig_image = $this->image->getImage();
 466  
 467          if ( ! isset( $size_data['width'] ) ) {
 468              $size_data['width'] = null;
 469          }
 470  
 471          if ( ! isset( $size_data['height'] ) ) {
 472              $size_data['height'] = null;
 473          }
 474  
 475          if ( ! isset( $size_data['crop'] ) ) {
 476              $size_data['crop'] = false;
 477          }
 478  
 479          $resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
 480  
 481          if ( is_wp_error( $resized ) ) {
 482              $saved = $resized;
 483          } else {
 484              $saved = $this->_save( $this->image );
 485  
 486              $this->image->clear();
 487              $this->image->destroy();
 488              $this->image = null;
 489          }
 490  
 491          $this->size  = $orig_size;
 492          $this->image = $orig_image;
 493  
 494          if ( ! is_wp_error( $saved ) ) {
 495              unset( $saved['path'] );
 496          }
 497  
 498          return $saved;
 499      }
 500  
 501      /**
 502       * Crops Image.
 503       *
 504       * @since 3.5.0
 505       *
 506       * @param int  $src_x The start x position to crop from.
 507       * @param int  $src_y The start y position to crop from.
 508       * @param int  $src_w The width to crop.
 509       * @param int  $src_h The height to crop.
 510       * @param int  $dst_w Optional. The destination width.
 511       * @param int  $dst_h Optional. The destination height.
 512       * @param bool $src_abs Optional. If the source crop points are absolute.
 513       * @return bool|WP_Error
 514       */
 515  	public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
 516          if ( $src_abs ) {
 517              $src_w -= $src_x;
 518              $src_h -= $src_y;
 519          }
 520  
 521          try {
 522              $this->image->cropImage( $src_w, $src_h, $src_x, $src_y );
 523              $this->image->setImagePage( $src_w, $src_h, 0, 0 );
 524  
 525              if ( $dst_w || $dst_h ) {
 526                  // If destination width/height isn't specified, use same as
 527                  // width/height from source.
 528                  if ( ! $dst_w ) {
 529                      $dst_w = $src_w;
 530                  }
 531                  if ( ! $dst_h ) {
 532                      $dst_h = $src_h;
 533                  }
 534  
 535                  $thumb_result = $this->thumbnail_image( $dst_w, $dst_h );
 536                  if ( is_wp_error( $thumb_result ) ) {
 537                      return $thumb_result;
 538                  }
 539  
 540                  return $this->update_size();
 541              }
 542          } catch ( Exception $e ) {
 543              return new WP_Error( 'image_crop_error', $e->getMessage() );
 544          }
 545          return $this->update_size();
 546      }
 547  
 548      /**
 549       * Rotates current image counter-clockwise by $angle.
 550       *
 551       * @since 3.5.0
 552       *
 553       * @param float $angle
 554       * @return true|WP_Error
 555       */
 556  	public function rotate( $angle ) {
 557          /**
 558           * $angle is 360-$angle because Imagick rotates clockwise
 559           * (GD rotates counter-clockwise)
 560           */
 561          try {
 562              $this->image->rotateImage( new ImagickPixel( 'none' ), 360 - $angle );
 563  
 564              // Normalise EXIF orientation data so that display is consistent across devices.
 565              if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) {
 566                  $this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT );
 567              }
 568  
 569              // Since this changes the dimensions of the image, update the size.
 570              $result = $this->update_size();
 571              if ( is_wp_error( $result ) ) {
 572                  return $result;
 573              }
 574  
 575              $this->image->setImagePage( $this->size['width'], $this->size['height'], 0, 0 );
 576          } catch ( Exception $e ) {
 577              return new WP_Error( 'image_rotate_error', $e->getMessage() );
 578          }
 579          return true;
 580      }
 581  
 582      /**
 583       * Flips current image.
 584       *
 585       * @since 3.5.0
 586       *
 587       * @param bool $horz Flip along Horizontal Axis
 588       * @param bool $vert Flip along Vertical Axis
 589       * @return true|WP_Error
 590       */
 591  	public function flip( $horz, $vert ) {
 592          try {
 593              if ( $horz ) {
 594                  $this->image->flipImage();
 595              }
 596  
 597              if ( $vert ) {
 598                  $this->image->flopImage();
 599              }
 600  
 601              // Normalise EXIF orientation data so that display is consistent across devices.
 602              if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) {
 603                  $this->image->setImageOrientation( Imagick::ORIENTATION_TOPLEFT );
 604              }
 605          } catch ( Exception $e ) {
 606              return new WP_Error( 'image_flip_error', $e->getMessage() );
 607          }
 608  
 609          return true;
 610      }
 611  
 612      /**
 613       * Check if a JPEG image has EXIF Orientation tag and rotate it if needed.
 614       *
 615       * As ImageMagick copies the EXIF data to the flipped/rotated image, proceed only
 616       * if EXIF Orientation can be reset afterwards.
 617       *
 618       * @since 5.3.0
 619       *
 620       * @return bool|WP_Error True if the image was rotated. False if no EXIF data or if the image doesn't need rotation.
 621       *                       WP_Error if error while rotating.
 622       */
 623  	public function maybe_exif_rotate() {
 624          if ( is_callable( array( $this->image, 'setImageOrientation' ) ) && defined( 'Imagick::ORIENTATION_TOPLEFT' ) ) {
 625              return parent::maybe_exif_rotate();
 626          } else {
 627              return new WP_Error( 'write_exif_error', __( 'The image cannot be rotated because the embedded meta data cannot be updated.' ) );
 628          }
 629      }
 630  
 631      /**
 632       * Saves current image to file.
 633       *
 634       * @since 3.5.0
 635       *
 636       * @param string $destfilename
 637       * @param string $mime_type
 638       * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
 639       */
 640  	public function save( $destfilename = null, $mime_type = null ) {
 641          $saved = $this->_save( $this->image, $destfilename, $mime_type );
 642  
 643          if ( ! is_wp_error( $saved ) ) {
 644              $this->file      = $saved['path'];
 645              $this->mime_type = $saved['mime-type'];
 646  
 647              try {
 648                  $this->image->setImageFormat( strtoupper( $this->get_extension( $this->mime_type ) ) );
 649              } catch ( Exception $e ) {
 650                  return new WP_Error( 'image_save_error', $e->getMessage(), $this->file );
 651              }
 652          }
 653  
 654          return $saved;
 655      }
 656  
 657      /**
 658       * @param Imagick $image
 659       * @param string $filename
 660       * @param string $mime_type
 661       * @return array|WP_Error
 662       */
 663  	protected function _save( $image, $filename = null, $mime_type = null ) {
 664          list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
 665  
 666          if ( ! $filename ) {
 667              $filename = $this->generate_filename( null, null, $extension );
 668          }
 669  
 670          try {
 671              // Store initial Format
 672              $orig_format = $this->image->getImageFormat();
 673  
 674              $this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) );
 675              $this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) );
 676  
 677              // Reset original Format
 678              $this->image->setImageFormat( $orig_format );
 679          } catch ( Exception $e ) {
 680              return new WP_Error( 'image_save_error', $e->getMessage(), $filename );
 681          }
 682  
 683          // Set correct file permissions
 684          $stat  = stat( dirname( $filename ) );
 685          $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
 686          chmod( $filename, $perms );
 687  
 688          return array(
 689              'path'      => $filename,
 690              /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
 691              'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
 692              'width'     => $this->size['width'],
 693              'height'    => $this->size['height'],
 694              'mime-type' => $mime_type,
 695          );
 696      }
 697  
 698      /**
 699       * Streams current image to browser.
 700       *
 701       * @since 3.5.0
 702       *
 703       * @param string $mime_type The mime type of the image.
 704       * @return bool|WP_Error True on success, WP_Error object on failure.
 705       */
 706  	public function stream( $mime_type = null ) {
 707          list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
 708  
 709          try {
 710              // Temporarily change format for stream
 711              $this->image->setImageFormat( strtoupper( $extension ) );
 712  
 713              // Output stream of image content
 714              header( "Content-Type: $mime_type" );
 715              print $this->image->getImageBlob();
 716  
 717              // Reset Image to original Format
 718              $this->image->setImageFormat( $this->get_extension( $this->mime_type ) );
 719          } catch ( Exception $e ) {
 720              return new WP_Error( 'image_stream_error', $e->getMessage() );
 721          }
 722  
 723          return true;
 724      }
 725  
 726      /**
 727       * Strips all image meta except color profiles from an image.
 728       *
 729       * @since 4.5.0
 730       *
 731       * @return true|WP_Error True if stripping metadata was successful. WP_Error object on error.
 732       */
 733  	protected function strip_meta() {
 734  
 735          if ( ! is_callable( array( $this->image, 'getImageProfiles' ) ) ) {
 736              /* translators: %s: ImageMagick method name. */
 737              return new WP_Error( 'image_strip_meta_error', sprintf( __( '%s is required to strip image meta.' ), '<code>Imagick::getImageProfiles()</code>' ) );
 738          }
 739  
 740          if ( ! is_callable( array( $this->image, 'removeImageProfile' ) ) ) {
 741              /* translators: %s: ImageMagick method name. */
 742              return new WP_Error( 'image_strip_meta_error', sprintf( __( '%s is required to strip image meta.' ), '<code>Imagick::removeImageProfile()</code>' ) );
 743          }
 744  
 745          /*
 746           * Protect a few profiles from being stripped for the following reasons:
 747           *
 748           * - icc:  Color profile information
 749           * - icm:  Color profile information
 750           * - iptc: Copyright data
 751           * - exif: Orientation data
 752           * - xmp:  Rights usage data
 753           */
 754          $protected_profiles = array(
 755              'icc',
 756              'icm',
 757              'iptc',
 758              'exif',
 759              'xmp',
 760          );
 761  
 762          try {
 763              // Strip profiles.
 764              foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) {
 765                  if ( ! in_array( $key, $protected_profiles, true ) ) {
 766                      $this->image->removeImageProfile( $key );
 767                  }
 768              }
 769          } catch ( Exception $e ) {
 770              return new WP_Error( 'image_strip_meta_error', $e->getMessage() );
 771          }
 772  
 773          return true;
 774      }
 775  
 776      /**
 777       * Sets up Imagick for PDF processing.
 778       * Increases rendering DPI and only loads first page.
 779       *
 780       * @since 4.7.0
 781       *
 782       * @return string|WP_Error File to load or WP_Error on failure.
 783       */
 784  	protected function pdf_setup() {
 785          try {
 786              // By default, PDFs are rendered in a very low resolution.
 787              // We want the thumbnail to be readable, so increase the rendering DPI.
 788              $this->image->setResolution( 128, 128 );
 789  
 790              // When generating thumbnails from cropped PDF pages, Imagemagick uses the uncropped
 791              // area (resulting in unnecessary whitespace) unless the following option is set.
 792              $this->image->setOption( 'pdf:use-cropbox', true );
 793  
 794              // Only load the first page.
 795              return $this->file . '[0]';
 796          } catch ( Exception $e ) {
 797              return new WP_Error( 'pdf_setup_failed', $e->getMessage(), $this->file );
 798          }
 799      }
 800  
 801  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7