/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. * @global array $_wp_additional_image_sizes 
  68. * 
  69. * @param int $attachment_id Attachment Id to process. 
  70. * @param string $file Filepath of the Attached image. 
  71. * @return mixed Metadata for attachment. 
  72. */ 
  73. function wp_generate_attachment_metadata( $attachment_id, $file ) { 
  74. $attachment = get_post( $attachment_id ); 
  75.  
  76. $metadata = array(); 
  77. $support = false; 
  78. if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && 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. global $_wp_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. $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes 
  94. else 
  95. $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options 
  96. if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) 
  97. $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes 
  98. else 
  99. $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options 
  100. if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) 
  101. $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; // For theme-added sizes 
  102. else 
  103. $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options 
  104.  
  105. /** 
  106. * Filters the image sizes automatically generated when uploading an image. 
  107. * 
  108. * @since 2.9.0 
  109. * @since 4.4.0 Added the `$metadata` argument. 
  110. * 
  111. * @param array $sizes An associative array of image sizes. 
  112. * @param array $metadata An associative array of image metadata: width, height, file. 
  113. */ 
  114. $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata ); 
  115.  
  116. if ( $sizes ) { 
  117. $editor = wp_get_image_editor( $file ); 
  118.  
  119. if ( ! is_wp_error( $editor ) ) 
  120. $metadata['sizes'] = $editor->multi_resize( $sizes ); 
  121. } else { 
  122. $metadata['sizes'] = array(); 
  123.  
  124. // Fetch additional metadata from EXIF/IPTC. 
  125. $image_meta = wp_read_image_metadata( $file ); 
  126. if ( $image_meta ) 
  127. $metadata['image_meta'] = $image_meta; 
  128.  
  129. } elseif ( wp_attachment_is( 'video', $attachment ) ) { 
  130. $metadata = wp_read_video_metadata( $file ); 
  131. $support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); 
  132. } elseif ( wp_attachment_is( 'audio', $attachment ) ) { 
  133. $metadata = wp_read_audio_metadata( $file ); 
  134. $support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); 
  135.  
  136. if ( $support && ! empty( $metadata['image']['data'] ) ) { 
  137. // Check for existing cover. 
  138. $hash = md5( $metadata['image']['data'] ); 
  139. $posts = get_posts( array( 
  140. 'fields' => 'ids',  
  141. 'post_type' => 'attachment',  
  142. 'post_mime_type' => $metadata['image']['mime'],  
  143. 'post_status' => 'inherit',  
  144. 'posts_per_page' => 1,  
  145. 'meta_key' => '_cover_hash',  
  146. 'meta_value' => $hash 
  147. ) ); 
  148. $exists = reset( $posts ); 
  149.  
  150. if ( ! empty( $exists ) ) { 
  151. update_post_meta( $attachment_id, '_thumbnail_id', $exists ); 
  152. } else { 
  153. $ext = '.jpg'; 
  154. switch ( $metadata['image']['mime'] ) { 
  155. case 'image/gif': 
  156. $ext = '.gif'; 
  157. break; 
  158. case 'image/png': 
  159. $ext = '.png'; 
  160. break; 
  161. $basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext; 
  162. $uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); 
  163. if ( false === $uploaded['error'] ) { 
  164. $image_attachment = array( 
  165. 'post_mime_type' => $metadata['image']['mime'],  
  166. 'post_type' => 'attachment',  
  167. 'post_content' => '',  
  168. ); 
  169. /** 
  170. * Filters the parameters for the attachment thumbnail creation. 
  171. * 
  172. * @since 3.9.0 
  173. * 
  174. * @param array $image_attachment An array of parameters to create the thumbnail. 
  175. * @param array $metadata Current attachment metadata. 
  176. * @param array $uploaded An array containing the thumbnail path and url. 
  177. */ 
  178. $image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded ); 
  179.  
  180. $sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] ); 
  181. add_post_meta( $sub_attachment_id, '_cover_hash', $hash ); 
  182. $attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); 
  183. wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); 
  184. update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); 
  185.  
  186. // Remove the blob of binary data from the array. 
  187. if ( $metadata ) { 
  188. unset( $metadata['image']['data'] ); 
  189.  
  190. /** 
  191. * Filters the generated attachment meta data. 
  192. * 
  193. * @since 2.1.0 
  194. * 
  195. * @param array $metadata An array of attachment meta data. 
  196. * @param int $attachment_id Current attachment ID. 
  197. */ 
  198. return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); 
  199.  
  200. /** 
  201. * Convert a fraction string to a decimal. 
  202. * 
  203. * @since 2.5.0 
  204. * 
  205. * @param string $str 
  206. * @return int|float 
  207. */ 
  208. function wp_exif_frac2dec($str) { 
  209. @list( $n, $d ) = explode( '/', $str ); 
  210. if ( !empty($d) ) 
  211. return $n / $d; 
  212. return $str; 
  213.  
  214. /** 
  215. * Convert the exif date format to a unix timestamp. 
  216. * 
  217. * @since 2.5.0 
  218. * 
  219. * @param string $str 
  220. * @return int 
  221. */ 
  222. function wp_exif_date2ts($str) { 
  223. @list( $date, $time ) = explode( ' ', trim($str) ); 
  224. @list( $y, $m, $d ) = explode( ':', $date ); 
  225.  
  226. return strtotime( "{$y}-{$m}-{$d} {$time}" ); 
  227.  
  228. /** 
  229. * Get extended image metadata, exif or iptc as available. 
  230. * 
  231. * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso 
  232. * created_timestamp, focal_length, shutter_speed, and title. 
  233. * 
  234. * The IPTC metadata that is retrieved is APP13, credit, byline, created date 
  235. * and time, caption, copyright, and title. Also includes FNumber, Model,  
  236. * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. 
  237. * 
  238. * @todo Try other exif libraries if available. 
  239. * @since 2.5.0 
  240. * 
  241. * @param string $file 
  242. * @return bool|array False on failure. Image metadata array on success. 
  243. */ 
  244. function wp_read_image_metadata( $file ) { 
  245. if ( ! file_exists( $file ) ) 
  246. return false; 
  247.  
  248. list( , , $sourceImageType ) = getimagesize( $file ); 
  249.  
  250. /** 
  251. * EXIF contains a bunch of data we'll probably never need formatted in ways 
  252. * that are difficult to use. We'll normalize it and just extract the fields 
  253. * that are likely to be useful. Fractions and numbers are converted to 
  254. * floats, dates to unix timestamps, and everything else to strings. 
  255. */ 
  256. $meta = array( 
  257. 'aperture' => 0,  
  258. 'credit' => '',  
  259. 'camera' => '',  
  260. 'caption' => '',  
  261. 'created_timestamp' => 0,  
  262. 'copyright' => '',  
  263. 'focal_length' => 0,  
  264. 'iso' => 0,  
  265. 'shutter_speed' => 0,  
  266. 'title' => '',  
  267. 'orientation' => 0,  
  268. 'keywords' => array(),  
  269. ); 
  270.  
  271. $iptc = array(); 
  272. /** 
  273. * Read IPTC first, since it might contain data not available in exif such 
  274. * as caption, description etc. 
  275. */ 
  276. if ( is_callable( 'iptcparse' ) ) { 
  277. getimagesize( $file, $info ); 
  278.  
  279. if ( ! empty( $info['APP13'] ) ) { 
  280. $iptc = iptcparse( $info['APP13'] ); 
  281.  
  282. // Headline, "A brief synopsis of the caption." 
  283. if ( ! empty( $iptc['2#105'][0] ) ) { 
  284. $meta['title'] = trim( $iptc['2#105'][0] ); 
  285. /** 
  286. * Title, "Many use the Title field to store the filename of the image,  
  287. * though the field may be used in many ways." 
  288. */ 
  289. } elseif ( ! empty( $iptc['2#005'][0] ) ) { 
  290. $meta['title'] = trim( $iptc['2#005'][0] ); 
  291.  
  292. if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption 
  293. $caption = trim( $iptc['2#120'][0] ); 
  294.  
  295. mbstring_binary_safe_encoding(); 
  296. $caption_length = strlen( $caption ); 
  297. reset_mbstring_encoding(); 
  298.  
  299. if ( empty( $meta['title'] ) && $caption_length < 80 ) { 
  300. // Assume the title is stored in 2:120 if it's short. 
  301. $meta['title'] = $caption; 
  302.  
  303. $meta['caption'] = $caption; 
  304.  
  305. if ( ! empty( $iptc['2#110'][0] ) ) // credit 
  306. $meta['credit'] = trim( $iptc['2#110'][0] ); 
  307. elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline 
  308. $meta['credit'] = trim( $iptc['2#080'][0] ); 
  309.  
  310. if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) // created date and time 
  311. $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); 
  312.  
  313. if ( ! empty( $iptc['2#116'][0] ) ) // copyright 
  314. $meta['copyright'] = trim( $iptc['2#116'][0] ); 
  315.  
  316. if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array 
  317. $meta['keywords'] = array_values( $iptc['2#025'] ); 
  318.  
  319. /** 
  320. * Filters the image types to check for exif data. 
  321. * 
  322. * @since 2.5.0 
  323. * 
  324. * @param array $image_types Image types to check for exif data. 
  325. */ 
  326. 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 ) ) ) ) { 
  327. $exif = @exif_read_data( $file ); 
  328.  
  329. if ( ! empty( $exif['ImageDescription'] ) ) { 
  330. mbstring_binary_safe_encoding(); 
  331. $description_length = strlen( $exif['ImageDescription'] ); 
  332. reset_mbstring_encoding(); 
  333.  
  334. if ( empty( $meta['title'] ) && $description_length < 80 ) { 
  335. // Assume the title is stored in ImageDescription 
  336. $meta['title'] = trim( $exif['ImageDescription'] ); 
  337.  
  338. if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) { 
  339. $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); 
  340.  
  341. if ( empty( $meta['caption'] ) ) { 
  342. $meta['caption'] = trim( $exif['ImageDescription'] ); 
  343. } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { 
  344. $meta['caption'] = trim( $exif['Comments'] ); 
  345.  
  346. if ( empty( $meta['credit'] ) ) { 
  347. if ( ! empty( $exif['Artist'] ) ) { 
  348. $meta['credit'] = trim( $exif['Artist'] ); 
  349. } elseif ( ! empty($exif['Author'] ) ) { 
  350. $meta['credit'] = trim( $exif['Author'] ); 
  351.  
  352. if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) { 
  353. $meta['copyright'] = trim( $exif['Copyright'] ); 
  354. if ( ! empty( $exif['FNumber'] ) ) { 
  355. $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); 
  356. if ( ! empty( $exif['Model'] ) ) { 
  357. $meta['camera'] = trim( $exif['Model'] ); 
  358. if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) { 
  359. $meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] ); 
  360. if ( ! empty( $exif['FocalLength'] ) ) { 
  361. $meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); 
  362. if ( ! empty( $exif['ISOSpeedRatings'] ) ) { 
  363. $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; 
  364. $meta['iso'] = trim( $meta['iso'] ); 
  365. if ( ! empty( $exif['ExposureTime'] ) ) { 
  366. $meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); 
  367. if ( ! empty( $exif['Orientation'] ) ) { 
  368. $meta['orientation'] = $exif['Orientation']; 
  369.  
  370. foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { 
  371. if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) { 
  372. $meta[ $key ] = utf8_encode( $meta[ $key ] ); 
  373.  
  374. foreach ( $meta['keywords'] as $key => $keyword ) { 
  375. if ( ! seems_utf8( $keyword ) ) { 
  376. $meta['keywords'][ $key ] = utf8_encode( $keyword ); 
  377.  
  378. $meta = wp_kses_post_deep( $meta ); 
  379.  
  380. /** 
  381. * Filters the array of meta data read from an image's exif data. 
  382. * 
  383. * @since 2.5.0 
  384. * @since 4.4.0 The `$iptc` parameter was added. 
  385. * 
  386. * @param array $meta Image meta data. 
  387. * @param string $file Path to image file. 
  388. * @param int $sourceImageType Type of image. 
  389. * @param array $iptc IPTC data. 
  390. */ 
  391. return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType, $iptc ); 
  392.  
  393.  
  394. /** 
  395. * Validate that file is an image. 
  396. * 
  397. * @since 2.5.0 
  398. * 
  399. * @param string $path File path to test if valid image. 
  400. * @return bool True if valid image, false if not valid image. 
  401. */ 
  402. function file_is_valid_image($path) { 
  403. $size = @getimagesize($path); 
  404. return !empty($size); 
  405.  
  406. /** 
  407. * Validate that file is suitable for displaying within a web page. 
  408. * 
  409. * @since 2.5.0 
  410. * 
  411. * @param string $path File path to test. 
  412. * @return bool True if suitable, false if not suitable. 
  413. */ 
  414. function file_is_displayable_image($path) { 
  415. $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP ); 
  416.  
  417. $info = @getimagesize( $path ); 
  418. if ( empty( $info ) ) { 
  419. $result = false; 
  420. } elseif ( ! in_array( $info[2], $displayable_image_types ) ) { 
  421. $result = false; 
  422. } else { 
  423. $result = true; 
  424.  
  425. /** 
  426. * Filters whether the current image is displayable in the browser. 
  427. * 
  428. * @since 2.5.0 
  429. * 
  430. * @param bool $result Whether the image can be displayed. Default true. 
  431. * @param string $path Path to the image. 
  432. */ 
  433. return apply_filters( 'file_is_displayable_image', $result, $path ); 
  434.  
  435. /** 
  436. * Load an image resource for editing. 
  437. * 
  438. * @since 2.9.0 
  439. * 
  440. * @param string $attachment_id Attachment ID. 
  441. * @param string $mime_type Image mime type. 
  442. * @param string $size Optional. Image size, defaults to 'full'. 
  443. * @return resource|false The resulting image resource on success, false on failure. 
  444. */ 
  445. function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { 
  446. $filepath = _load_image_to_edit_path( $attachment_id, $size ); 
  447. if ( empty( $filepath ) ) 
  448. return false; 
  449.  
  450. switch ( $mime_type ) { 
  451. case 'image/jpeg': 
  452. $image = imagecreatefromjpeg($filepath); 
  453. break; 
  454. case 'image/png': 
  455. $image = imagecreatefrompng($filepath); 
  456. break; 
  457. case 'image/gif': 
  458. $image = imagecreatefromgif($filepath); 
  459. break; 
  460. default: 
  461. $image = false; 
  462. break; 
  463. if ( is_resource($image) ) { 
  464. /** 
  465. * Filters the current image being loaded for editing. 
  466. * 
  467. * @since 2.9.0 
  468. * 
  469. * @param resource $image Current image. 
  470. * @param string $attachment_id Attachment ID. 
  471. * @param string $size Image size. 
  472. */ 
  473. $image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); 
  474. if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) { 
  475. imagealphablending($image, false); 
  476. imagesavealpha($image, true); 
  477. return $image; 
  478.  
  479. /** 
  480. * Retrieve the path or url of an attachment's attached file. 
  481. * 
  482. * If the attached file is not present on the local filesystem (usually due to replication plugins),  
  483. * then the url of the file is returned if url fopen is supported. 
  484. * 
  485. * @since 3.4.0 
  486. * @access private 
  487. * 
  488. * @param string $attachment_id Attachment ID. 
  489. * @param string $size Optional. Image size, defaults to 'full'. 
  490. * @return string|false File path or url on success, false on failure. 
  491. */ 
  492. function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { 
  493. $filepath = get_attached_file( $attachment_id ); 
  494.  
  495. if ( $filepath && file_exists( $filepath ) ) { 
  496. if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { 
  497. /** 
  498. * Filters the path to the current image. 
  499. * 
  500. * The filter is evaluated for all image sizes except 'full'. 
  501. * 
  502. * @since 3.1.0 
  503. * 
  504. * @param string $path Path to the current image. 
  505. * @param string $attachment_id Attachment ID. 
  506. * @param string $size Size of the image. 
  507. */ 
  508. $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); 
  509. } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) { 
  510. /** 
  511. * Filters the image URL if not in the local filesystem. 
  512. * 
  513. * The filter is only evaluated if fopen is enabled on the server. 
  514. * 
  515. * @since 3.1.0 
  516. * 
  517. * @param string $image_url Current image URL. 
  518. * @param string $attachment_id Attachment ID. 
  519. * @param string $size Size of the image. 
  520. */ 
  521. $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); 
  522.  
  523. /** 
  524. * Filters the returned path or URL of the current image. 
  525. * 
  526. * @since 2.9.0 
  527. * 
  528. * @param string|bool $filepath File path or URL to current image, or false. 
  529. * @param string $attachment_id Attachment ID. 
  530. * @param string $size Size of the image. 
  531. */ 
  532. return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); 
  533.  
  534. /** 
  535. * Copy an existing image file. 
  536. * 
  537. * @since 3.4.0 
  538. * @access private 
  539. * 
  540. * @param string $attachment_id Attachment ID. 
  541. * @return string|false New file path on success, false on failure. 
  542. */ 
  543. function _copy_image_file( $attachment_id ) { 
  544. $dst_file = $src_file = get_attached_file( $attachment_id ); 
  545. if ( ! file_exists( $src_file ) ) 
  546. $src_file = _load_image_to_edit_path( $attachment_id ); 
  547.  
  548. if ( $src_file ) { 
  549. $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); 
  550. $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 
  551.  
  552. /** 
  553. * The directory containing the original file may no longer 
  554. * exist when using a replication plugin. 
  555. */ 
  556. wp_mkdir_p( dirname( $dst_file ) ); 
  557.  
  558. if ( ! @copy( $src_file, $dst_file ) ) 
  559. $dst_file = false; 
  560. } else { 
  561. $dst_file = false; 
  562.  
  563. return $dst_file; 
.