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