WP_Image_Editor_Imagick

WordPress Image Editor Class for Image Manipulation through Imagick PHP Module.

Defined (1)

The class is defined in the following location(s).

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