PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/df_home/static/test/portalbkd/wp-includes/class-wp-image-editor-gd.php

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