/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. $uploaded = $editor->save( $file, 'image/jpeg' ); 
  237. unset( $editor ); 
  238.  
  239. // Resize based on the full size image, rather than the source. 
  240. if ( ! is_wp_error( $uploaded ) ) { 
  241. $editor = wp_get_image_editor( $uploaded['path'] ); 
  242. unset( $uploaded['path'] ); 
  243.  
  244. if ( ! is_wp_error( $editor ) ) { 
  245. $metadata['sizes'] = $editor->multi_resize( $sizes ); 
  246. $metadata['sizes']['full'] = $uploaded; 
  247.  
  248. // Remove the blob of binary data from the array. 
  249. if ( $metadata ) { 
  250. unset( $metadata['image']['data'] ); 
  251.  
  252. /** 
  253. * Filters the generated attachment meta data. 
  254. * 
  255. * @since 2.1.0 
  256. * 
  257. * @param array $metadata An array of attachment meta data. 
  258. * @param int $attachment_id Current attachment ID. 
  259. */ 
  260. return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); 
  261.  
  262. /** 
  263. * Convert a fraction string to a decimal. 
  264. * 
  265. * @since 2.5.0 
  266. * 
  267. * @param string $str 
  268. * @return int|float 
  269. */ 
  270. function wp_exif_frac2dec($str) { 
  271. @list( $n, $d ) = explode( '/', $str ); 
  272. if ( !empty($d) ) 
  273. return $n / $d; 
  274. return $str; 
  275.  
  276. /** 
  277. * Convert the exif date format to a unix timestamp. 
  278. * 
  279. * @since 2.5.0 
  280. * 
  281. * @param string $str 
  282. * @return int 
  283. */ 
  284. function wp_exif_date2ts($str) { 
  285. @list( $date, $time ) = explode( ' ', trim($str) ); 
  286. @list( $y, $m, $d ) = explode( ':', $date ); 
  287.  
  288. return strtotime( "{$y}-{$m}-{$d} {$time}" ); 
  289.  
  290. /** 
  291. * Get extended image metadata, exif or iptc as available. 
  292. * 
  293. * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso 
  294. * created_timestamp, focal_length, shutter_speed, and title. 
  295. * 
  296. * The IPTC metadata that is retrieved is APP13, credit, byline, created date 
  297. * and time, caption, copyright, and title. Also includes FNumber, Model,  
  298. * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. 
  299. * 
  300. * @todo Try other exif libraries if available. 
  301. * @since 2.5.0 
  302. * 
  303. * @param string $file 
  304. * @return bool|array False on failure. Image metadata array on success. 
  305. */ 
  306. function wp_read_image_metadata( $file ) { 
  307. if ( ! file_exists( $file ) ) 
  308. return false; 
  309.  
  310. list( , , $sourceImageType ) = getimagesize( $file ); 
  311.  
  312. /** 
  313. * EXIF contains a bunch of data we'll probably never need formatted in ways 
  314. * that are difficult to use. We'll normalize it and just extract the fields 
  315. * that are likely to be useful. Fractions and numbers are converted to 
  316. * floats, dates to unix timestamps, and everything else to strings. 
  317. */ 
  318. $meta = array( 
  319. 'aperture' => 0,  
  320. 'credit' => '',  
  321. 'camera' => '',  
  322. 'caption' => '',  
  323. 'created_timestamp' => 0,  
  324. 'copyright' => '',  
  325. 'focal_length' => 0,  
  326. 'iso' => 0,  
  327. 'shutter_speed' => 0,  
  328. 'title' => '',  
  329. 'orientation' => 0,  
  330. 'keywords' => array(),  
  331. ); 
  332.  
  333. $iptc = array(); 
  334. /** 
  335. * Read IPTC first, since it might contain data not available in exif such 
  336. * as caption, description etc. 
  337. */ 
  338. if ( is_callable( 'iptcparse' ) ) { 
  339. getimagesize( $file, $info ); 
  340.  
  341. if ( ! empty( $info['APP13'] ) ) { 
  342. $iptc = iptcparse( $info['APP13'] ); 
  343.  
  344. // Headline, "A brief synopsis of the caption." 
  345. if ( ! empty( $iptc['2#105'][0] ) ) { 
  346. $meta['title'] = trim( $iptc['2#105'][0] ); 
  347. /** 
  348. * Title, "Many use the Title field to store the filename of the image,  
  349. * though the field may be used in many ways." 
  350. */ 
  351. } elseif ( ! empty( $iptc['2#005'][0] ) ) { 
  352. $meta['title'] = trim( $iptc['2#005'][0] ); 
  353.  
  354. if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption 
  355. $caption = trim( $iptc['2#120'][0] ); 
  356.  
  357. mbstring_binary_safe_encoding(); 
  358. $caption_length = strlen( $caption ); 
  359. reset_mbstring_encoding(); 
  360.  
  361. if ( empty( $meta['title'] ) && $caption_length < 80 ) { 
  362. // Assume the title is stored in 2:120 if it's short. 
  363. $meta['title'] = $caption; 
  364.  
  365. $meta['caption'] = $caption; 
  366.  
  367. if ( ! empty( $iptc['2#110'][0] ) ) // credit 
  368. $meta['credit'] = trim( $iptc['2#110'][0] ); 
  369. elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline 
  370. $meta['credit'] = trim( $iptc['2#080'][0] ); 
  371.  
  372. if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time 
  373. $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); 
  374.  
  375. if ( ! empty( $iptc['2#116'][0] ) ) // copyright 
  376. $meta['copyright'] = trim( $iptc['2#116'][0] ); 
  377.  
  378. if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array 
  379. $meta['keywords'] = array_values( $iptc['2#025'] ); 
  380.  
  381. /** 
  382. * Filters the image types to check for exif data. 
  383. * 
  384. * @since 2.5.0 
  385. * 
  386. * @param array $image_types Image types to check for exif data. 
  387. */ 
  388. 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 ) ) ) ) { 
  389. $exif = @exif_read_data( $file ); 
  390.  
  391. if ( ! empty( $exif['ImageDescription'] ) ) { 
  392. mbstring_binary_safe_encoding(); 
  393. $description_length = strlen( $exif['ImageDescription'] ); 
  394. reset_mbstring_encoding(); 
  395.  
  396. if ( empty( $meta['title'] ) && $description_length < 80 ) { 
  397. // Assume the title is stored in ImageDescription 
  398. $meta['title'] = trim( $exif['ImageDescription'] ); 
  399.  
  400. if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) { 
  401. $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); 
  402.  
  403. if ( empty( $meta['caption'] ) ) { 
  404. $meta['caption'] = trim( $exif['ImageDescription'] ); 
  405. } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { 
  406. $meta['caption'] = trim( $exif['Comments'] ); 
  407.  
  408. if ( empty( $meta['credit'] ) ) { 
  409. if ( ! empty( $exif['Artist'] ) ) { 
  410. $meta['credit'] = trim( $exif['Artist'] ); 
  411. } elseif ( ! empty($exif['Author'] ) ) { 
  412. $meta['credit'] = trim( $exif['Author'] ); 
  413.  
  414. if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) { 
  415. $meta['copyright'] = trim( $exif['Copyright'] ); 
  416. if ( ! empty( $exif['FNumber'] ) ) { 
  417. $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); 
  418. if ( ! empty( $exif['Model'] ) ) { 
  419. $meta['camera'] = trim( $exif['Model'] ); 
  420. if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) { 
  421. $meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] ); 
  422. if ( ! empty( $exif['FocalLength'] ) ) { 
  423. $meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); 
  424. if ( ! empty( $exif['ISOSpeedRatings'] ) ) { 
  425. $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; 
  426. $meta['iso'] = trim( $meta['iso'] ); 
  427. if ( ! empty( $exif['ExposureTime'] ) ) { 
  428. $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); 
  429. if ( ! empty( $exif['Orientation'] ) ) { 
  430. $meta['orientation'] = $exif['Orientation']; 
  431.  
  432. foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { 
  433. if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) { 
  434. $meta[ $key ] = utf8_encode( $meta[ $key ] ); 
  435.  
  436. foreach ( $meta['keywords'] as $key => $keyword ) { 
  437. if ( ! seems_utf8( $keyword ) ) { 
  438. $meta['keywords'][ $key ] = utf8_encode( $keyword ); 
  439.  
  440. $meta = wp_kses_post_deep( $meta ); 
  441.  
  442. /** 
  443. * Filters the array of meta data read from an image's exif data. 
  444. * 
  445. * @since 2.5.0 
  446. * @since 4.4.0 The `$iptc` parameter was added. 
  447. * 
  448. * @param array $meta Image meta data. 
  449. * @param string $file Path to image file. 
  450. * @param int $sourceImageType Type of image. 
  451. * @param array $iptc IPTC data. 
  452. */ 
  453. return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType, $iptc ); 
  454.  
  455.  
  456. /** 
  457. * Validate that file is an image. 
  458. * 
  459. * @since 2.5.0 
  460. * 
  461. * @param string $path File path to test if valid image. 
  462. * @return bool True if valid image, false if not valid image. 
  463. */ 
  464. function file_is_valid_image($path) { 
  465. $size = @getimagesize($path); 
  466. return !empty($size); 
  467.  
  468. /** 
  469. * Validate that file is suitable for displaying within a web page. 
  470. * 
  471. * @since 2.5.0 
  472. * 
  473. * @param string $path File path to test. 
  474. * @return bool True if suitable, false if not suitable. 
  475. */ 
  476. function file_is_displayable_image($path) { 
  477. $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP ); 
  478.  
  479. $info = @getimagesize( $path ); 
  480. if ( empty( $info ) ) { 
  481. $result = false; 
  482. } elseif ( ! in_array( $info[2], $displayable_image_types ) ) { 
  483. $result = false; 
  484. } else { 
  485. $result = true; 
  486.  
  487. /** 
  488. * Filters whether the current image is displayable in the browser. 
  489. * 
  490. * @since 2.5.0 
  491. * 
  492. * @param bool $result Whether the image can be displayed. Default true. 
  493. * @param string $path Path to the image. 
  494. */ 
  495. return apply_filters( 'file_is_displayable_image', $result, $path ); 
  496.  
  497. /** 
  498. * Load an image resource for editing. 
  499. * 
  500. * @since 2.9.0 
  501. * 
  502. * @param string $attachment_id Attachment ID. 
  503. * @param string $mime_type Image mime type. 
  504. * @param string $size Optional. Image size, defaults to 'full'. 
  505. * @return resource|false The resulting image resource on success, false on failure. 
  506. */ 
  507. function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { 
  508. $filepath = _load_image_to_edit_path( $attachment_id, $size ); 
  509. if ( empty( $filepath ) ) 
  510. return false; 
  511.  
  512. switch ( $mime_type ) { 
  513. case 'image/jpeg': 
  514. $image = imagecreatefromjpeg($filepath); 
  515. break; 
  516. case 'image/png': 
  517. $image = imagecreatefrompng($filepath); 
  518. break; 
  519. case 'image/gif': 
  520. $image = imagecreatefromgif($filepath); 
  521. break; 
  522. default: 
  523. $image = false; 
  524. break; 
  525. if ( is_resource($image) ) { 
  526. /** 
  527. * Filters the current image being loaded for editing. 
  528. * 
  529. * @since 2.9.0 
  530. * 
  531. * @param resource $image Current image. 
  532. * @param string $attachment_id Attachment ID. 
  533. * @param string $size Image size. 
  534. */ 
  535. $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); 
  536. if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) { 
  537. imagealphablending($image, false); 
  538. imagesavealpha($image, true); 
  539. return $image; 
  540.  
  541. /** 
  542. * Retrieve the path or url of an attachment's attached file. 
  543. * 
  544. * If the attached file is not present on the local filesystem (usually due to replication plugins),  
  545. * then the url of the file is returned if url fopen is supported. 
  546. * 
  547. * @since 3.4.0 
  548. * @access private 
  549. * 
  550. * @param string $attachment_id Attachment ID. 
  551. * @param string $size Optional. Image size, defaults to 'full'. 
  552. * @return string|false File path or url on success, false on failure. 
  553. */ 
  554. function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { 
  555. $filepath = get_attached_file( $attachment_id ); 
  556.  
  557. if ( $filepath && file_exists( $filepath ) ) { 
  558. if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { 
  559. /** 
  560. * Filters the path to the current image. 
  561. * 
  562. * The filter is evaluated for all image sizes except 'full'. 
  563. * 
  564. * @since 3.1.0 
  565. * 
  566. * @param string $path Path to the current image. 
  567. * @param string $attachment_id Attachment ID. 
  568. * @param string $size Size of the image. 
  569. */ 
  570. $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); 
  571. } elseif ( function_exists( 'fopen' ) && true == ini_get( 'allow_url_fopen' ) ) { 
  572. /** 
  573. * Filters the image URL if not in the local filesystem. 
  574. * 
  575. * The filter is only evaluated if fopen is enabled on the server. 
  576. * 
  577. * @since 3.1.0 
  578. * 
  579. * @param string $image_url Current image URL. 
  580. * @param string $attachment_id Attachment ID. 
  581. * @param string $size Size of the image. 
  582. */ 
  583. $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); 
  584.  
  585. /** 
  586. * Filters the returned path or URL of the current image. 
  587. * 
  588. * @since 2.9.0 
  589. * 
  590. * @param string|bool $filepath File path or URL to current image, or false. 
  591. * @param string $attachment_id Attachment ID. 
  592. * @param string $size Size of the image. 
  593. */ 
  594. return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); 
  595.  
  596. /** 
  597. * Copy an existing image file. 
  598. * 
  599. * @since 3.4.0 
  600. * @access private 
  601. * 
  602. * @param string $attachment_id Attachment ID. 
  603. * @return string|false New file path on success, false on failure. 
  604. */ 
  605. function _copy_image_file( $attachment_id ) { 
  606. $dst_file = $src_file = get_attached_file( $attachment_id ); 
  607. if ( ! file_exists( $src_file ) ) 
  608. $src_file = _load_image_to_edit_path( $attachment_id ); 
  609.  
  610. if ( $src_file ) { 
  611. $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); 
  612. $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 
  613.  
  614. /** 
  615. * The directory containing the original file may no longer 
  616. * exist when using a replication plugin. 
  617. */ 
  618. wp_mkdir_p( dirname( $dst_file ) ); 
  619.  
  620. if ( ! @copy( $src_file, $dst_file ) ) 
  621. $dst_file = false; 
  622. } else { 
  623. $dst_file = false; 
  624.  
  625. return $dst_file; 
.