Advertisement
Guest User

Untitled

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