PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/blog.old/wp-includes/class-wp-image-editor.php

https://github.com/chopsuei3/oscc
PHP | 423 lines | 126 code | 48 blank | 249 comment | 22 complexity | 2390d5e80e381595ce5fb10664fdb2ad MD5 | raw file
  1. <?php
  2. /**
  3. * Base WordPress Image Editor
  4. *
  5. * @package WordPress
  6. * @subpackage Image_Editor
  7. */
  8. /**
  9. * Base image editor class from which implementations extend
  10. *
  11. * @since 3.5.0
  12. */
  13. abstract class WP_Image_Editor {
  14. protected $file = null;
  15. protected $size = null;
  16. protected $mime_type = null;
  17. protected $default_mime_type = 'image/jpeg';
  18. protected $quality = 90;
  19. /**
  20. * Each instance handles a single file.
  21. */
  22. public function __construct( $file ) {
  23. $this->file = $file;
  24. }
  25. /**
  26. * Checks to see if current environment supports the editor chosen.
  27. * Must be overridden in a sub-class.
  28. *
  29. * @since 3.5.0
  30. * @access public
  31. * @abstract
  32. *
  33. * @param array $args
  34. * @return boolean
  35. */
  36. public static function test( $args = array() ) {
  37. return false;
  38. }
  39. /**
  40. * Checks to see if editor supports the mime-type specified.
  41. * Must be overridden in a sub-class.
  42. *
  43. * @since 3.5.0
  44. * @access public
  45. * @abstract
  46. *
  47. * @param string $mime_type
  48. * @return boolean
  49. */
  50. public static function supports_mime_type( $mime_type ) {
  51. return false;
  52. }
  53. /**
  54. * Loads image from $this->file into editor.
  55. *
  56. * @since 3.5.0
  57. * @access protected
  58. * @abstract
  59. *
  60. * @return boolean|WP_Error True if loaded; WP_Error on failure.
  61. */
  62. abstract public function load();
  63. /**
  64. * Saves current image to file.
  65. *
  66. * @since 3.5.0
  67. * @access public
  68. * @abstract
  69. *
  70. * @param string $destfilename
  71. * @param string $mime_type
  72. * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
  73. */
  74. abstract public function save( $destfilename = null, $mime_type = null );
  75. /**
  76. * Resizes current image.
  77. *
  78. * @since 3.5.0
  79. * @access public
  80. * @abstract
  81. *
  82. * @param int $max_w
  83. * @param int $max_h
  84. * @param boolean $crop
  85. * @return boolean|WP_Error
  86. */
  87. abstract public function resize( $max_w, $max_h, $crop = false );
  88. /**
  89. * Resize multiple images from a single source.
  90. *
  91. * @since 3.5.0
  92. * @access public
  93. * @abstract
  94. *
  95. * @param array $sizes {
  96. * An array of image size arrays. Default sizes are 'small', 'medium', 'large'.
  97. *
  98. * @type array $size {
  99. * @type int $width Image width.
  100. * @type int $height Image height.
  101. * @type bool $crop Optional. Whether to crop the image. Default false.
  102. * }
  103. * }
  104. * @return array An array of resized images metadata by size.
  105. */
  106. abstract public function multi_resize( $sizes );
  107. /**
  108. * Crops Image.
  109. *
  110. * @since 3.5.0
  111. * @access public
  112. * @abstract
  113. *
  114. * @param string|int $src The source file or Attachment ID.
  115. * @param int $src_x The start x position to crop from.
  116. * @param int $src_y The start y position to crop from.
  117. * @param int $src_w The width to crop.
  118. * @param int $src_h The height to crop.
  119. * @param int $dst_w Optional. The destination width.
  120. * @param int $dst_h Optional. The destination height.
  121. * @param boolean $src_abs Optional. If the source crop points are absolute.
  122. * @return boolean|WP_Error
  123. */
  124. abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
  125. /**
  126. * Rotates current image counter-clockwise by $angle.
  127. *
  128. * @since 3.5.0
  129. * @access public
  130. * @abstract
  131. *
  132. * @param float $angle
  133. * @return boolean|WP_Error
  134. */
  135. abstract public function rotate( $angle );
  136. /**
  137. * Flips current image.
  138. *
  139. * @since 3.5.0
  140. * @access public
  141. * @abstract
  142. *
  143. * @param boolean $horz Flip along Horizontal Axis
  144. * @param boolean $vert Flip along Vertical Axis
  145. * @return boolean|WP_Error
  146. */
  147. abstract public function flip( $horz, $vert );
  148. /**
  149. * Streams current image to browser.
  150. *
  151. * @since 3.5.0
  152. * @access public
  153. * @abstract
  154. *
  155. * @param string $mime_type
  156. * @return boolean|WP_Error
  157. */
  158. abstract public function stream( $mime_type = null );
  159. /**
  160. * Gets dimensions of image.
  161. *
  162. * @since 3.5.0
  163. * @access public
  164. *
  165. * @return array {'width'=>int, 'height'=>int}
  166. */
  167. public function get_size() {
  168. return $this->size;
  169. }
  170. /**
  171. * Sets current image size.
  172. *
  173. * @since 3.5.0
  174. * @access protected
  175. *
  176. * @param int $width
  177. * @param int $height
  178. */
  179. protected function update_size( $width = null, $height = null ) {
  180. $this->size = array(
  181. 'width' => (int) $width,
  182. 'height' => (int) $height
  183. );
  184. return true;
  185. }
  186. /**
  187. * Sets Image Compression quality on a 1-100% scale.
  188. *
  189. * @since 3.5.0
  190. * @access public
  191. *
  192. * @param int $quality Compression Quality. Range: [1,100]
  193. * @return boolean
  194. */
  195. public function set_quality( $quality ) {
  196. /**
  197. * Filter the default quality setting.
  198. *
  199. * @since 3.5.0
  200. *
  201. * @param int $quality Quality level between 0 (low) and 100 (high).
  202. */
  203. $this->quality = apply_filters( 'wp_editor_set_quality', $quality );
  204. return ( (bool) $this->quality );
  205. }
  206. /**
  207. * Returns preferred mime-type and extension based on provided
  208. * file's extension and mime, or current file's extension and mime.
  209. *
  210. * Will default to $this->default_mime_type if requested is not supported.
  211. *
  212. * Provides corrected filename only if filename is provided.
  213. *
  214. * @since 3.5.0
  215. * @access protected
  216. *
  217. * @param string $filename
  218. * @param string $mime_type
  219. * @return array { filename|null, extension, mime-type }
  220. */
  221. protected function get_output_format( $filename = null, $mime_type = null ) {
  222. $new_ext = $file_ext = null;
  223. $file_mime = null;
  224. // By default, assume specified type takes priority
  225. if ( $mime_type ) {
  226. $new_ext = $this->get_extension( $mime_type );
  227. }
  228. if ( $filename ) {
  229. $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
  230. $file_mime = $this->get_mime_type( $file_ext );
  231. }
  232. else {
  233. // If no file specified, grab editor's current extension and mime-type.
  234. $file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
  235. $file_mime = $this->mime_type;
  236. }
  237. // Check to see if specified mime-type is the same as type implied by
  238. // file extension. If so, prefer extension from file.
  239. if ( ! $mime_type || ( $file_mime == $mime_type ) ) {
  240. $mime_type = $file_mime;
  241. $new_ext = $file_ext;
  242. }
  243. // Double-check that the mime-type selected is supported by the editor.
  244. // If not, choose a default instead.
  245. if ( ! $this->supports_mime_type( $mime_type ) ) {
  246. /**
  247. * Filter default mime type prior to getting the file extension.
  248. *
  249. * @see wp_get_mime_types()
  250. *
  251. * @since 3.5.0
  252. *
  253. * @param string $mime_type Mime type string.
  254. */
  255. $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type );
  256. $new_ext = $this->get_extension( $mime_type );
  257. }
  258. if ( $filename ) {
  259. $ext = '';
  260. $info = pathinfo( $filename );
  261. $dir = $info['dirname'];
  262. if( isset( $info['extension'] ) )
  263. $ext = $info['extension'];
  264. $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
  265. }
  266. return array( $filename, $new_ext, $mime_type );
  267. }
  268. /**
  269. * Builds an output filename based on current file, and adding proper suffix
  270. *
  271. * @since 3.5.0
  272. * @access public
  273. *
  274. * @param string $suffix
  275. * @param string $dest_path
  276. * @param string $extension
  277. * @return string filename
  278. */
  279. public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
  280. // $suffix will be appended to the destination filename, just before the extension
  281. if ( ! $suffix )
  282. $suffix = $this->get_suffix();
  283. $info = pathinfo( $this->file );
  284. $dir = $info['dirname'];
  285. $ext = $info['extension'];
  286. $name = wp_basename( $this->file, ".$ext" );
  287. $new_ext = strtolower( $extension ? $extension : $ext );
  288. if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) )
  289. $dir = $_dest_path;
  290. return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
  291. }
  292. /**
  293. * Builds and returns proper suffix for file based on height and width.
  294. *
  295. * @since 3.5.0
  296. * @access public
  297. *
  298. * @return string suffix
  299. */
  300. public function get_suffix() {
  301. if ( ! $this->get_size() )
  302. return false;
  303. return "{$this->size['width']}x{$this->size['height']}";
  304. }
  305. /**
  306. * Either calls editor's save function or handles file as a stream.
  307. *
  308. * @since 3.5.0
  309. * @access protected
  310. *
  311. * @param string|stream $filename
  312. * @param callable $function
  313. * @param array $arguments
  314. * @return boolean
  315. */
  316. protected function make_image( $filename, $function, $arguments ) {
  317. if ( $stream = wp_is_stream( $filename ) ) {
  318. ob_start();
  319. } else {
  320. // The directory containing the original file may no longer exist when using a replication plugin.
  321. wp_mkdir_p( dirname( $filename ) );
  322. }
  323. $result = call_user_func_array( $function, $arguments );
  324. if ( $result && $stream ) {
  325. $contents = ob_get_contents();
  326. $fp = fopen( $filename, 'w' );
  327. if ( ! $fp )
  328. return false;
  329. fwrite( $fp, $contents );
  330. fclose( $fp );
  331. }
  332. if ( $stream ) {
  333. ob_end_clean();
  334. }
  335. return $result;
  336. }
  337. /**
  338. * Returns first matched mime-type from extension,
  339. * as mapped from wp_get_mime_types()
  340. *
  341. * @since 3.5.0
  342. * @access protected
  343. *
  344. * @param string $extension
  345. * @return string|boolean
  346. */
  347. protected static function get_mime_type( $extension = null ) {
  348. if ( ! $extension )
  349. return false;
  350. $mime_types = wp_get_mime_types();
  351. $extensions = array_keys( $mime_types );
  352. foreach( $extensions as $_extension ) {
  353. if ( preg_match( "/{$extension}/i", $_extension ) ) {
  354. return $mime_types[$_extension];
  355. }
  356. }
  357. return false;
  358. }
  359. /**
  360. * Returns first matched extension from Mime-type,
  361. * as mapped from wp_get_mime_types()
  362. *
  363. * @since 3.5.0
  364. * @access protected
  365. *
  366. * @param string $mime_type
  367. * @return string|boolean
  368. */
  369. protected static function get_extension( $mime_type = null ) {
  370. $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) );
  371. if ( empty( $extensions[0] ) )
  372. return false;
  373. return $extensions[0];
  374. }
  375. }