Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

699 rader
22 KiB

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