PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/html/blog/wp-includes/class-wp-image-editor-gd.php

https://github.com/jimmytidey/jimmytidey.co.uk
PHP | 459 lines | 222 code | 65 blank | 172 comment | 54 complexity | bfc53719d646e63af4165d955ef07357 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress GD Image Editor
  4. *
  5. * @package WordPress
  6. * @subpackage Image_Editor
  7. */
  8. /**
  9. * WordPress Image Editor Class for Image Manipulation through GD
  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_GD extends WP_Image_Editor {
  17. protected $image = false; // GD Resource
  18. function __destruct() {
  19. if ( $this->image ) {
  20. // we don't need the original in memory anymore
  21. imagedestroy( $this->image );
  22. }
  23. }
  24. /**
  25. * Checks to see if current environment supports GD.
  26. *
  27. * @since 3.5.0
  28. * @access public
  29. *
  30. * @return boolean
  31. */
  32. public static function test( $args = array() ) {
  33. if ( ! extension_loaded('gd') || ! function_exists('gd_info') )
  34. return false;
  35. // On some setups GD library does not provide imagerotate() - Ticket #11536
  36. if ( isset( $args['methods'] ) &&
  37. in_array( 'rotate', $args['methods'] ) &&
  38. ! function_exists('imagerotate') ){
  39. return false;
  40. }
  41. return true;
  42. }
  43. /**
  44. * Checks to see if editor supports the mime-type specified.
  45. *
  46. * @since 3.5.0
  47. * @access public
  48. *
  49. * @param string $mime_type
  50. * @return boolean
  51. */
  52. public static function supports_mime_type( $mime_type ) {
  53. $image_types = imagetypes();
  54. switch( $mime_type ) {
  55. case 'image/jpeg':
  56. return ($image_types & IMG_JPG) != 0;
  57. case 'image/png':
  58. return ($image_types & IMG_PNG) != 0;
  59. case 'image/gif':
  60. return ($image_types & IMG_GIF) != 0;
  61. }
  62. return false;
  63. }
  64. /**
  65. * Loads image from $this->file into new GD Resource.
  66. *
  67. * @since 3.5.0
  68. * @access protected
  69. *
  70. * @return boolean|WP_Error True if loaded successfully; WP_Error on failure.
  71. */
  72. public function load() {
  73. if ( $this->image )
  74. return true;
  75. if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
  76. return new WP_Error( 'error_loading_image', __('File doesn&#8217;t exist?'), $this->file );
  77. /**
  78. * Filter the memory limit allocated for image manipulation.
  79. *
  80. * @since 3.5.0
  81. *
  82. * @param int|string $limit Maximum memory limit to allocate for images. Default WP_MAX_MEMORY_LIMIT.
  83. * Accepts an integer (bytes), or a shorthand string notation, such as '256M'.
  84. */
  85. // Set artificially high because GD uses uncompressed images in memory
  86. @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
  87. $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
  88. if ( ! is_resource( $this->image ) )
  89. return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file );
  90. $size = @getimagesize( $this->file );
  91. if ( ! $size )
  92. return new WP_Error( 'invalid_image', __('Could not read image size.'), $this->file );
  93. if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
  94. imagealphablending( $this->image, false );
  95. imagesavealpha( $this->image, true );
  96. }
  97. $this->update_size( $size[0], $size[1] );
  98. $this->mime_type = $size['mime'];
  99. return $this->set_quality( $this->quality );
  100. }
  101. /**
  102. * Sets or updates current image size.
  103. *
  104. * @since 3.5.0
  105. * @access protected
  106. *
  107. * @param int $width
  108. * @param int $height
  109. */
  110. protected function update_size( $width = false, $height = false ) {
  111. if ( ! $width )
  112. $width = imagesx( $this->image );
  113. if ( ! $height )
  114. $height = imagesy( $this->image );
  115. return parent::update_size( $width, $height );
  116. }
  117. /**
  118. * Resizes current image.
  119. * Wraps _resize, since _resize returns a GD Resource.
  120. *
  121. * At minimum, either a height or width must be provided.
  122. * If one of the two is set to null, the resize will
  123. * maintain aspect ratio according to the provided dimension.
  124. *
  125. * @since 3.5.0
  126. * @access public
  127. *
  128. * @param int|null $max_w Image width.
  129. * @param int|null $max_h Image height.
  130. * @param boolean $crop
  131. * @return boolean|WP_Error
  132. */
  133. public function resize( $max_w, $max_h, $crop = false ) {
  134. if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) )
  135. return true;
  136. $resized = $this->_resize( $max_w, $max_h, $crop );
  137. if ( is_resource( $resized ) ) {
  138. imagedestroy( $this->image );
  139. $this->image = $resized;
  140. return true;
  141. } elseif ( is_wp_error( $resized ) )
  142. return $resized;
  143. return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
  144. }
  145. protected function _resize( $max_w, $max_h, $crop = false ) {
  146. $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
  147. if ( ! $dims ) {
  148. return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions'), $this->file );
  149. }
  150. list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
  151. $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
  152. imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
  153. if ( is_resource( $resized ) ) {
  154. $this->update_size( $dst_w, $dst_h );
  155. return $resized;
  156. }
  157. return new WP_Error( 'image_resize_error', __('Image resize failed.'), $this->file );
  158. }
  159. /**
  160. * Resize multiple images from a single source.
  161. *
  162. * @since 3.5.0
  163. * @access public
  164. *
  165. * @param array $sizes {
  166. * An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
  167. *
  168. * Either a height or width must be provided.
  169. * If one of the two is set to null, the resize will
  170. * maintain aspect ratio according to the provided dimension.
  171. *
  172. * @type array $size {
  173. * @type int ['width'] Optional. Image width.
  174. * @type int ['height'] Optional. Image height.
  175. * @type bool ['crop'] Optional. Whether to crop the image. Default false.
  176. * }
  177. * }
  178. * @return array An array of resized images' metadata by size.
  179. */
  180. public function multi_resize( $sizes ) {
  181. $metadata = array();
  182. $orig_size = $this->size;
  183. foreach ( $sizes as $size => $size_data ) {
  184. if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
  185. continue;
  186. }
  187. if ( ! isset( $size_data['width'] ) ) {
  188. $size_data['width'] = null;
  189. }
  190. if ( ! isset( $size_data['height'] ) ) {
  191. $size_data['height'] = null;
  192. }
  193. if ( ! isset( $size_data['crop'] ) ) {
  194. $size_data['crop'] = false;
  195. }
  196. $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
  197. if( ! is_wp_error( $image ) ) {
  198. $resized = $this->_save( $image );
  199. imagedestroy( $image );
  200. if ( ! is_wp_error( $resized ) && $resized ) {
  201. unset( $resized['path'] );
  202. $metadata[$size] = $resized;
  203. }
  204. }
  205. $this->size = $orig_size;
  206. }
  207. return $metadata;
  208. }
  209. /**
  210. * Crops Image.
  211. *
  212. * @since 3.5.0
  213. * @access public
  214. *
  215. * @param string|int $src The source file or Attachment ID.
  216. * @param int $src_x The start x position to crop from.
  217. * @param int $src_y The start y position to crop from.
  218. * @param int $src_w The width to crop.
  219. * @param int $src_h The height to crop.
  220. * @param int $dst_w Optional. The destination width.
  221. * @param int $dst_h Optional. The destination height.
  222. * @param boolean $src_abs Optional. If the source crop points are absolute.
  223. * @return boolean|WP_Error
  224. */
  225. public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false ) {
  226. // If destination width/height isn't specified, use same as
  227. // width/height from source.
  228. if ( ! $dst_w )
  229. $dst_w = $src_w;
  230. if ( ! $dst_h )
  231. $dst_h = $src_h;
  232. $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
  233. if ( $src_abs ) {
  234. $src_w -= $src_x;
  235. $src_h -= $src_y;
  236. }
  237. if ( function_exists( 'imageantialias' ) )
  238. imageantialias( $dst, true );
  239. imagecopyresampled( $dst, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
  240. if ( is_resource( $dst ) ) {
  241. imagedestroy( $this->image );
  242. $this->image = $dst;
  243. $this->update_size();
  244. return true;
  245. }
  246. return new WP_Error( 'image_crop_error', __('Image crop failed.'), $this->file );
  247. }
  248. /**
  249. * Rotates current image counter-clockwise by $angle.
  250. * Ported from image-edit.php
  251. *
  252. * @since 3.5.0
  253. * @access public
  254. *
  255. * @param float $angle
  256. * @return boolean|WP_Error
  257. */
  258. public function rotate( $angle ) {
  259. if ( function_exists('imagerotate') ) {
  260. $rotated = imagerotate( $this->image, $angle, 0 );
  261. if ( is_resource( $rotated ) ) {
  262. imagedestroy( $this->image );
  263. $this->image = $rotated;
  264. $this->update_size();
  265. return true;
  266. }
  267. }
  268. return new WP_Error( 'image_rotate_error', __('Image rotate failed.'), $this->file );
  269. }
  270. /**
  271. * Flips current image.
  272. *
  273. * @since 3.5.0
  274. * @access public
  275. *
  276. * @param boolean $horz Flip along Horizontal Axis
  277. * @param boolean $vert Flip along Vertical Axis
  278. * @returns boolean|WP_Error
  279. */
  280. public function flip( $horz, $vert ) {
  281. $w = $this->size['width'];
  282. $h = $this->size['height'];
  283. $dst = wp_imagecreatetruecolor( $w, $h );
  284. if ( is_resource( $dst ) ) {
  285. $sx = $vert ? ($w - 1) : 0;
  286. $sy = $horz ? ($h - 1) : 0;
  287. $sw = $vert ? -$w : $w;
  288. $sh = $horz ? -$h : $h;
  289. if ( imagecopyresampled( $dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh ) ) {
  290. imagedestroy( $this->image );
  291. $this->image = $dst;
  292. return true;
  293. }
  294. }
  295. return new WP_Error( 'image_flip_error', __('Image flip failed.'), $this->file );
  296. }
  297. /**
  298. * Saves current in-memory image to file.
  299. *
  300. * @since 3.5.0
  301. * @access public
  302. *
  303. * @param string $destfilename
  304. * @param string $mime_type
  305. * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
  306. */
  307. public function save( $filename = null, $mime_type = null ) {
  308. $saved = $this->_save( $this->image, $filename, $mime_type );
  309. if ( ! is_wp_error( $saved ) ) {
  310. $this->file = $saved['path'];
  311. $this->mime_type = $saved['mime-type'];
  312. }
  313. return $saved;
  314. }
  315. protected function _save( $image, $filename = null, $mime_type = null ) {
  316. list( $filename, $extension, $mime_type ) = $this->get_output_format( $filename, $mime_type );
  317. if ( ! $filename )
  318. $filename = $this->generate_filename( null, null, $extension );
  319. if ( 'image/gif' == $mime_type ) {
  320. if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) )
  321. return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
  322. }
  323. elseif ( 'image/png' == $mime_type ) {
  324. // convert from full colors to index colors, like original PNG.
  325. if ( function_exists('imageistruecolor') && ! imageistruecolor( $image ) )
  326. imagetruecolortopalette( $image, false, imagecolorstotal( $image ) );
  327. if ( ! $this->make_image( $filename, 'imagepng', array( $image, $filename ) ) )
  328. return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
  329. }
  330. elseif ( 'image/jpeg' == $mime_type ) {
  331. if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->quality ) ) )
  332. return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
  333. }
  334. else {
  335. return new WP_Error( 'image_save_error', __('Image Editor Save Failed') );
  336. }
  337. // Set correct file permissions
  338. $stat = stat( dirname( $filename ) );
  339. $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
  340. @ chmod( $filename, $perms );
  341. /**
  342. * Filter the name of the saved image file.
  343. *
  344. * @since 2.6.0
  345. *
  346. * @param string $filename Name of the file.
  347. */
  348. return array(
  349. 'path' => $filename,
  350. 'file' => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
  351. 'width' => $this->size['width'],
  352. 'height' => $this->size['height'],
  353. 'mime-type' => $mime_type,
  354. );
  355. }
  356. /**
  357. * Returns stream of current image.
  358. *
  359. * @since 3.5.0
  360. * @access public
  361. *
  362. * @param string $mime_type
  363. */
  364. public function stream( $mime_type = null ) {
  365. list( $filename, $extension, $mime_type ) = $this->get_output_format( null, $mime_type );
  366. switch ( $mime_type ) {
  367. case 'image/png':
  368. header( 'Content-Type: image/png' );
  369. return imagepng( $this->image );
  370. case 'image/gif':
  371. header( 'Content-Type: image/gif' );
  372. return imagegif( $this->image );
  373. default:
  374. header( 'Content-Type: image/jpeg' );
  375. return imagejpeg( $this->image, null, $this->quality );
  376. }
  377. }
  378. /**
  379. * Either calls editor's save function or handles file as a stream.
  380. *
  381. * @since 3.5.0
  382. * @access protected
  383. *
  384. * @param string|stream $filename
  385. * @param callable $function
  386. * @param array $arguments
  387. * @return boolean
  388. */
  389. protected function make_image( $filename, $function, $arguments ) {
  390. if ( wp_is_stream( $filename ) )
  391. $arguments[1] = null;
  392. return parent::make_image( $filename, $function, $arguments );
  393. }
  394. }