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