/wp-admin/includes/image.php

  1. <?php 
  2. /** 
  3. * File contains all the administration image manipulation functions. 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Administration 
  7. */ 
  8.  
  9. /** 
  10. * Crop an Image to a given size. 
  11. * 
  12. * @since 2.1.0 
  13. * 
  14. * @param string|int $src The source file or Attachment ID. 
  15. * @param int $src_x The start x position to crop from. 
  16. * @param int $src_y The start y position to crop from. 
  17. * @param int $src_w The width to crop. 
  18. * @param int $src_h The height to crop. 
  19. * @param int $dst_w The destination width. 
  20. * @param int $dst_h The destination height. 
  21. * @param int $src_abs Optional. If the source crop points are absolute. 
  22. * @param string $dst_file Optional. The destination file to write to. 
  23. * @return string|WP_Error New filepath on success, WP_Error on failure. 
  24. */ 
  25. function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { 
  26. $src_file = $src; 
  27. if ( is_numeric( $src ) ) { // Handle int as attachment ID 
  28. $src_file = get_attached_file( $src ); 
  29.  
  30. if ( ! file_exists( $src_file ) ) { 
  31. // If the file doesn't exist, attempt a URL fopen on the src link. 
  32. // This can occur with certain file replication plugins. 
  33. $src = _load_image_to_edit_path( $src, 'full' ); 
  34. } else { 
  35. $src = $src_file; 
  36.  
  37. $editor = wp_get_image_editor( $src ); 
  38. if ( is_wp_error( $editor ) ) 
  39. return $editor; 
  40.  
  41. $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); 
  42. if ( is_wp_error( $src ) ) 
  43. return $src; 
  44.  
  45. if ( ! $dst_file ) 
  46. $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); 
  47.  
  48. /** 
  49. * The directory containing the original file may no longer exist when 
  50. * using a replication plugin. 
  51. */ 
  52. wp_mkdir_p( dirname( $dst_file ) ); 
  53.  
  54. $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 
  55.  
  56. $result = $editor->save( $dst_file ); 
  57. if ( is_wp_error( $result ) ) 
  58. return $result; 
  59.  
  60. return $dst_file; 
  61.  
  62. /** 
  63. * Generate post thumbnail attachment meta data. 
  64. * 
  65. * @since 2.1.0 
  66. * 
  67. * @param int $attachment_id Attachment Id to process. 
  68. * @param string $file Filepath of the Attached image. 
  69. * @return mixed Metadata for attachment. 
  70. */ 
  71. function wp_generate_attachment_metadata( $attachment_id, $file ) { 
  72. $attachment = get_post( $attachment_id ); 
  73.  
  74. $metadata = array(); 
  75. $support = false; 
  76. $mime_type = get_post_mime_type( $attachment ); 
  77.  
  78. if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) { 
  79. $imagesize = getimagesize( $file ); 
  80. $metadata['width'] = $imagesize[0]; 
  81. $metadata['height'] = $imagesize[1]; 
  82.  
  83. // Make the file path relative to the upload dir. 
  84. $metadata['file'] = _wp_relative_upload_path($file); 
  85.  
  86. // Make thumbnails and other intermediate sizes. 
  87. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  88.  
  89. $sizes = array(); 
  90. foreach ( get_intermediate_image_sizes() as $s ) { 
  91. $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false ); 
  92. if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) { 
  93. // For theme-added sizes 
  94. $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); 
  95. } else { 
  96. // For default sizes set in options 
  97. $sizes[$s]['width'] = get_option( "{$s}_size_w" ); 
  98.  
  99. if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) { 
  100. // For theme-added sizes 
  101. $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); 
  102. } else { 
  103. // For default sizes set in options 
  104. $sizes[$s]['height'] = get_option( "{$s}_size_h" ); 
  105.  
  106. if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) { 
  107. // For theme-added sizes 
  108. $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; 
  109. } else { 
  110. // For default sizes set in options 
  111. $sizes[$s]['crop'] = get_option( "{$s}_crop" ); 
  112.  
  113. /** 
  114. * Filters the image sizes automatically generated when uploading an image. 
  115. * 
  116. * @since 2.9.0 
  117. * @since 4.4.0 Added the `$metadata` argument. 
  118. * 
  119. * @param array $sizes An associative array of image sizes. 
  120. * @param array $metadata An associative array of image metadata: width, height, file. 
  121. */ 
  122. $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata ); 
  123.  
  124. if ( $sizes ) { 
  125. $editor = wp_get_image_editor( $file ); 
  126.  
  127. if ( ! is_wp_error( $editor ) ) 
  128. $metadata['sizes'] = $editor->multi_resize( $sizes ); 
  129. } else { 
  130. $metadata['sizes'] = array(); 
  131.  
  132. // Fetch additional metadata from EXIF/IPTC. 
  133. $image_meta = wp_read_image_metadata( $file ); 
  134. if ( $image_meta ) 
  135. $metadata['image_meta'] = $image_meta; 
  136.  
  137. } elseif ( wp_attachment_is( 'video', $attachment ) ) { 
  138. $metadata = wp_read_video_metadata( $file ); 
  139. $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); 
  140. } elseif ( wp_attachment_is( 'audio', $attachment ) ) { 
  141. $metadata = wp_read_audio_metadata( $file ); 
  142. $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); 
  143.  
  144. if ( $support && ! empty( $metadata['image']['data'] ) ) { 
  145. // Check for existing cover. 
  146. $hash = md5( $metadata['image']['data'] ); 
  147. $posts = get_posts( array( 
  148. 'fields' => 'ids',  
  149. 'post_type' => 'attachment',  
  150. 'post_mime_type' => $metadata['image']['mime'],  
  151. 'post_status' => 'inherit',  
  152. 'posts_per_page' => 1,  
  153. 'meta_key' => '_cover_hash',  
  154. 'meta_value' => $hash 
  155. ) ); 
  156. $exists = reset( $posts ); 
  157.  
  158. if ( ! empty( $exists ) ) { 
  159. update_post_meta( $attachment_id, '_thumbnail_id', $exists ); 
  160. } else { 
  161. $ext = '.jpg'; 
  162. switch ( $metadata['image']['mime'] ) { 
  163. case 'image/gif': 
  164. $ext = '.gif'; 
  165. break; 
  166. case 'image/png': 
  167. $ext = '.png'; 
  168. break; 
  169. $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; 
  170. $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); 
  171. if ( false === $uploaded['error'] ) { 
  172. $image_attachment = array( 
  173. 'post_mime_type' => $metadata['image']['mime'],  
  174. 'post_type' => 'attachment',  
  175. 'post_content' => '',  
  176. ); 
  177. /** 
  178. * Filters the parameters for the attachment thumbnail creation. 
  179. * 
  180. * @since 3.9.0 
  181. * 
  182. * @param array $image_attachment An array of parameters to create the thumbnail. 
  183. * @param array $metadata Current attachment metadata. 
  184. * @param array $uploaded An array containing the thumbnail path and url. 
  185. */ 
  186. $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded ); 
  187.  
  188. $sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] ); 
  189. add_post_meta( $sub_attachment_id, '_cover_hash', $hash ); 
  190. $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); 
  191. wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); 
  192. update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); 
  193. // Try to create image thumbnails for PDFs 
  194. else if ( 'application/pdf' === $mime_type ) { 
  195. $fallback_sizes = array( 
  196. 'thumbnail',  
  197. 'medium',  
  198. 'large',  
  199. ); 
  200.  
  201. /** 
  202. * Filters the image sizes generated for non-image mime types. 
  203. * 
  204. * @since 4.7.0 
  205. * 
  206. * @param array $fallback_sizes An array of image size names. 
  207. */ 
  208. $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); 
  209.  
  210. $sizes = array(); 
  211. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  212.  
  213. foreach ( $fallback_sizes as $s ) { 
  214. if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { 
  215. $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); 
  216. } else { 
  217. $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); 
  218.  
  219. if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { 
  220. $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); 
  221. } else { 
  222. $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); 
  223.  
  224. if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { 
  225. $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; 
  226. } else { 
  227. // Force thumbnails to be soft crops. 
  228. if ( ! 'thumbnail' === $s ) { 
  229. $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); 
  230.  
  231. // Only load PDFs in an image editor if we're processing sizes. 
  232. if ( ! empty( $sizes ) ) { 
  233. $editor = wp_get_image_editor( $file ); 
  234.  
  235. if ( ! is_wp_error( $editor ) ) { // No support for this type of file 
  236. /** 
  237. * PDFs may have the same file filename as JPEGs. 
  238. * Ensure the PDF preview image does not overwrite any JPEG images that already exist. 
  239. */ 
  240. $dirname = dirname( $file ) . '/'; 
  241. $ext = '.' . pathinfo( $file, PATHINFO_EXTENSION ); 
  242. $preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' ); 
  243.  
  244. $uploaded = $editor->save( $preview_file, 'image/jpeg' ); 
  245. unset( $editor ); 
  246.  
  247. // Resize based on the full size image, rather than the source. 
  248. if ( ! is_wp_error( $uploaded ) ) { 
  249. $editor = wp_get_image_editor( $uploaded['path'] ); 
  250. unset( $uploaded['path'] ); 
  251.  
  252. if ( ! is_wp_error( $editor ) ) { 
  253. $metadata['sizes'] = $editor->multi_resize( $sizes ); 
  254. $metadata['sizes']['full'] = $uploaded; 
  255.  
  256. // Remove the blob of binary data from the array. 
  257. if ( $metadata ) { 
  258. unset( $metadata['image']['data'] ); 
  259.  
  260. /** 
  261. * Filters the generated attachment meta data. 
  262. * 
  263. * @since 2.1.0 
  264. * 
  265. * @param array $metadata An array of attachment meta data. 
  266. * @param int $attachment_id Current attachment ID. 
  267. */ 
  268. return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); 
  269.  
  270. /** 
  271. * Convert a fraction string to a decimal. 
  272. * 
  273. * @since 2.5.0 
  274. * 
  275. * @param string $str 
  276. * @return int|float 
  277. */ 
  278. function wp_exif_frac2dec($str) { 
  279. @list( $n, $d ) = explode( '/', $str ); 
  280. if ( !empty($d) ) 
  281. return $n / $d; 
  282. return $str; 
  283.  
  284. /** 
  285. * Convert the exif date format to a unix timestamp. 
  286. * 
  287. * @since 2.5.0 
  288. * 
  289. * @param string $str 
  290. * @return int 
  291. */ 
  292. function wp_exif_date2ts($str) { 
  293. @list( $date, $time ) = explode( ' ', trim($str) ); 
  294. @list( $y, $m, $d ) = explode( ':', $date ); 
  295.  
  296. return strtotime( "{$y}-{$m}-{$d} {$time}" ); 
  297.  
  298. /** 
  299. * Get extended image metadata, exif or iptc as available. 
  300. * 
  301. * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso 
  302. * created_timestamp, focal_length, shutter_speed, and title. 
  303. * 
  304. * The IPTC metadata that is retrieved is APP13, credit, byline, created date 
  305. * and time, caption, copyright, and title. Also includes FNumber, Model,  
  306. * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. 
  307. * 
  308. * @todo Try other exif libraries if available. 
  309. * @since 2.5.0 
  310. * 
  311. * @param string $file 
  312. * @return bool|array False on failure. Image metadata array on success. 
  313. */ 
  314. function wp_read_image_metadata( $file ) { 
  315. if ( ! file_exists( $file ) ) 
  316. return false; 
  317.  
  318. list( , , $sourceImageType ) = getimagesize( $file ); 
  319.  
  320. /** 
  321. * EXIF contains a bunch of data we'll probably never need formatted in ways 
  322. * that are difficult to use. We'll normalize it and just extract the fields 
  323. * that are likely to be useful. Fractions and numbers are converted to 
  324. * floats, dates to unix timestamps, and everything else to strings. 
  325. */ 
  326. $meta = array( 
  327. 'aperture' => 0,  
  328. 'credit' => '',  
  329. 'camera' => '',  
  330. 'caption' => '',  
  331. 'created_timestamp' => 0,  
  332. 'copyright' => '',  
  333. 'focal_length' => 0,  
  334. 'iso' => 0,  
  335. 'shutter_speed' => 0,  
  336. 'title' => '',  
  337. 'orientation' => 0,  
  338. 'keywords' => array(),  
  339. ); 
  340.  
  341. $iptc = array(); 
  342. /** 
  343. * Read IPTC first, since it might contain data not available in exif such 
  344. * as caption, description etc. 
  345. */ 
  346. if ( is_callable( 'iptcparse' ) ) { 
  347. getimagesize( $file, $info ); 
  348.  
  349. if ( ! empty( $info['APP13'] ) ) { 
  350. $iptc = iptcparse( $info['APP13'] ); 
  351.  
  352. // Headline, "A brief synopsis of the caption." 
  353. if ( ! empty( $iptc['2#105'][0] ) ) { 
  354. $meta['title'] = trim( $iptc['2#105'][0] ); 
  355. /** 
  356. * Title, "Many use the Title field to store the filename of the image,  
  357. * though the field may be used in many ways." 
  358. */ 
  359. } elseif ( ! empty( $iptc['2#005'][0] ) ) { 
  360. $meta['title'] = trim( $iptc['2#005'][0] ); 
  361.  
  362. if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption 
  363. $caption = trim( $iptc['2#120'][0] ); 
  364.  
  365. mbstring_binary_safe_encoding(); 
  366. $caption_length = strlen( $caption ); 
  367. reset_mbstring_encoding(); 
  368.  
  369. if ( empty( $meta['title'] ) && $caption_length < 80 ) { 
  370. // Assume the title is stored in 2:120 if it's short. 
  371. $meta['title'] = $caption; 
  372.  
  373. $meta['caption'] = $caption; 
  374.  
  375. if ( ! empty( $iptc['2#110'][0] ) ) // credit 
  376. $meta['credit'] = trim( $iptc['2#110'][0] ); 
  377. elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline 
  378. $meta['credit'] = trim( $iptc['2#080'][0] ); 
  379.  
  380. if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time 
  381. $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); 
  382.  
  383. if ( ! empty( $iptc['2#116'][0] ) ) // copyright 
  384. $meta['copyright'] = trim( $iptc['2#116'][0] ); 
  385.  
  386. if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array 
  387. $meta['keywords'] = array_values( $iptc['2#025'] ); 
  388.  
  389. /** 
  390. * Filters the image types to check for exif data. 
  391. * 
  392. * @since 2.5.0 
  393. * 
  394. * @param array $image_types Image types to check for exif data. 
  395. */ 
  396. if ( is_callable( 'exif_read_data' ) && in_array( $sourceImageType, apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ) ) ) { 
  397. $exif = @exif_read_data( $file ); 
  398.  
  399. if ( ! empty( $exif['ImageDescription'] ) ) { 
  400. mbstring_binary_safe_encoding(); 
  401. $description_length = strlen( $exif['ImageDescription'] ); 
  402. reset_mbstring_encoding(); 
  403.  
  404. if ( empty( $meta['title'] ) && $description_length < 80 ) { 
  405. // Assume the title is stored in ImageDescription 
  406. $meta['title'] = trim( $exif['ImageDescription'] ); 
  407.  
  408. if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) { 
  409. $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); 
  410.  
  411. if ( empty( $meta['caption'] ) ) { 
  412. $meta['caption'] = trim( $exif['ImageDescription'] ); 
  413. } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { 
  414. $meta['caption'] = trim( $exif['Comments'] ); 
  415.  
  416. if ( empty( $meta['credit'] ) ) { 
  417. if ( ! empty( $exif['Artist'] ) ) { 
  418. $meta['credit'] = trim( $exif['Artist'] ); 
  419. } elseif ( ! empty($exif['Author'] ) ) { 
  420. $meta['credit'] = trim( $exif['Author'] ); 
  421.  
  422. if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) { 
  423. $meta['copyright'] = trim( $exif['Copyright'] ); 
  424. if ( ! empty( $exif['FNumber'] ) ) { 
  425. $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); 
  426. if ( ! empty( $exif['Model'] ) ) { 
  427. $meta['camera'] = trim( $exif['Model'] ); 
  428. if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) { 
  429. $meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] ); 
  430. if ( ! empty( $exif['FocalLength'] ) ) { 
  431. $meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); 
  432. if ( ! empty( $exif['ISOSpeedRatings'] ) ) { 
  433. $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; 
  434. $meta['iso'] = trim( $meta['iso'] ); 
  435. if ( ! empty( $exif['ExposureTime'] ) ) { 
  436. $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); 
  437. if ( ! empty( $exif['Orientation'] ) ) { 
  438. $meta['orientation'] = $exif['Orientation']; 
  439.  
  440. foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { 
  441. if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) { 
  442. $meta[ $key ] = utf8_encode( $meta[ $key ] ); 
  443.  
  444. foreach ( $meta['keywords'] as $key => $keyword ) { 
  445. if ( ! seems_utf8( $keyword ) ) { 
  446. $meta['keywords'][ $key ] = utf8_encode( $keyword ); 
  447.  
  448. $meta = wp_kses_post_deep( $meta ); 
  449.  
  450. /** 
  451. * Filters the array of meta data read from an image's exif data. 
  452. * 
  453. * @since 2.5.0 
  454. * @since 4.4.0 The `$iptc` parameter was added. 
  455. * 
  456. * @param array $meta Image meta data. 
  457. * @param string $file Path to image file. 
  458. * @param int $sourceImageType Type of image. 
  459. * @param array $iptc IPTC data. 
  460. */ 
  461. return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType, $iptc ); 
  462.  
  463.  
  464. /** 
  465. * Validate that file is an image. 
  466. * 
  467. * @since 2.5.0 
  468. * 
  469. * @param string $path File path to test if valid image. 
  470. * @return bool True if valid image, false if not valid image. 
  471. */ 
  472. function file_is_valid_image($path) { 
  473. $size = @getimagesize($path); 
  474. return !empty($size); 
  475.  
  476. /** 
  477. * Validate that file is suitable for displaying within a web page. 
  478. * 
  479. * @since 2.5.0 
  480. * 
  481. * @param string $path File path to test. 
  482. * @return bool True if suitable, false if not suitable. 
  483. */ 
  484. function file_is_displayable_image($path) { 
  485. $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP ); 
  486.  
  487. $info = @getimagesize( $path ); 
  488. if ( empty( $info ) ) { 
  489. $result = false; 
  490. } elseif ( ! in_array( $info[2], $displayable_image_types ) ) { 
  491. $result = false; 
  492. } else { 
  493. $result = true; 
  494.  
  495. /** 
  496. * Filters whether the current image is displayable in the browser. 
  497. * 
  498. * @since 2.5.0 
  499. * 
  500. * @param bool $result Whether the image can be displayed. Default true. 
  501. * @param string $path Path to the image. 
  502. */ 
  503. return apply_filters( 'file_is_displayable_image', $result, $path ); 
  504.  
  505. /** 
  506. * Load an image resource for editing. 
  507. * 
  508. * @since 2.9.0 
  509. * 
  510. * @param string $attachment_id Attachment ID. 
  511. * @param string $mime_type Image mime type. 
  512. * @param string $size Optional. Image size, defaults to 'full'. 
  513. * @return resource|false The resulting image resource on success, false on failure. 
  514. */ 
  515. function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { 
  516. $filepath = _load_image_to_edit_path( $attachment_id, $size ); 
  517. if ( empty( $filepath ) ) 
  518. return false; 
  519.  
  520. switch ( $mime_type ) { 
  521. case 'image/jpeg': 
  522. $image = imagecreatefromjpeg($filepath); 
  523. break; 
  524. case 'image/png': 
  525. $image = imagecreatefrompng($filepath); 
  526. break; 
  527. case 'image/gif': 
  528. $image = imagecreatefromgif($filepath); 
  529. break; 
  530. default: 
  531. $image = false; 
  532. break; 
  533. if ( is_resource($image) ) { 
  534. /** 
  535. * Filters the current image being loaded for editing. 
  536. * 
  537. * @since 2.9.0 
  538. * 
  539. * @param resource $image Current image. 
  540. * @param string $attachment_id Attachment ID. 
  541. * @param string $size Image size. 
  542. */ 
  543. $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); 
  544. if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) { 
  545. imagealphablending($image, false); 
  546. imagesavealpha($image, true); 
  547. return $image; 
  548.  
  549. /** 
  550. * Retrieve the path or url of an attachment's attached file. 
  551. * 
  552. * If the attached file is not present on the local filesystem (usually due to replication plugins),  
  553. * then the url of the file is returned if url fopen is supported. 
  554. * 
  555. * @since 3.4.0 
  556. * @access private 
  557. * 
  558. * @param string $attachment_id Attachment ID. 
  559. * @param string $size Optional. Image size, defaults to 'full'. 
  560. * @return string|false File path or url on success, false on failure. 
  561. */ 
  562. function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { 
  563. $filepath = get_attached_file( $attachment_id ); 
  564.  
  565. if ( $filepath && file_exists( $filepath ) ) { 
  566. if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { 
  567. /** 
  568. * Filters the path to the current image. 
  569. * 
  570. * The filter is evaluated for all image sizes except 'full'. 
  571. * 
  572. * @since 3.1.0 
  573. * 
  574. * @param string $path Path to the current image. 
  575. * @param string $attachment_id Attachment ID. 
  576. * @param string $size Size of the image. 
  577. */ 
  578. $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); 
  579. } elseif ( function_exists( 'fopen' ) && true == ini_get( 'allow_url_fopen' ) ) { 
  580. /** 
  581. * Filters the image URL if not in the local filesystem. 
  582. * 
  583. * The filter is only evaluated if fopen is enabled on the server. 
  584. * 
  585. * @since 3.1.0 
  586. * 
  587. * @param string $image_url Current image URL. 
  588. * @param string $attachment_id Attachment ID. 
  589. * @param string $size Size of the image. 
  590. */ 
  591. $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); 
  592.  
  593. /** 
  594. * Filters the returned path or URL of the current image. 
  595. * 
  596. * @since 2.9.0 
  597. * 
  598. * @param string|bool $filepath File path or URL to current image, or false. 
  599. * @param string $attachment_id Attachment ID. 
  600. * @param string $size Size of the image. 
  601. */ 
  602. return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); 
  603.  
  604. /** 
  605. * Copy an existing image file. 
  606. * 
  607. * @since 3.4.0 
  608. * @access private 
  609. * 
  610. * @param string $attachment_id Attachment ID. 
  611. * @return string|false New file path on success, false on failure. 
  612. */ 
  613. function _copy_image_file( $attachment_id ) { 
  614. $dst_file = $src_file = get_attached_file( $attachment_id ); 
  615. if ( ! file_exists( $src_file ) ) 
  616. $src_file = _load_image_to_edit_path( $attachment_id ); 
  617.  
  618. if ( $src_file ) { 
  619. $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); 
  620. $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 
  621.  
  622. /** 
  623. * The directory containing the original file may no longer 
  624. * exist when using a replication plugin. 
  625. */ 
  626. wp_mkdir_p( dirname( $dst_file ) ); 
  627.  
  628. if ( ! @copy( $src_file, $dst_file ) ) 
  629. $dst_file = false; 
  630. } else { 
  631. $dst_file = false; 
  632.  
  633. return $dst_file; 
.