You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

770 lines
22 KiB

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