/wp-includes/class-wp-image-editor-imagick.php

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