15 * |
15 * |
16 * @since 1.2.0 |
16 * @since 1.2.0 |
17 * |
17 * |
18 * @param mixed $file Filename of the original image, Or attachment id. |
18 * @param mixed $file Filename of the original image, Or attachment id. |
19 * @param int $max_side Maximum length of a single side for the thumbnail. |
19 * @param int $max_side Maximum length of a single side for the thumbnail. |
|
20 * @param mixed $deprecated Never used. |
20 * @return string Thumbnail path on success, Error string on failure. |
21 * @return string Thumbnail path on success, Error string on failure. |
21 */ |
22 */ |
22 function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) { |
23 function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) { |
|
24 if ( !empty( $deprecated ) ) |
|
25 _deprecated_argument( __FUNCTION__, '1.2' ); |
23 $thumbpath = image_resize( $file, $max_side, $max_side ); |
26 $thumbpath = image_resize( $file, $max_side, $max_side ); |
24 return apply_filters( 'wp_create_thumbnail', $thumbpath ); |
27 return apply_filters( 'wp_create_thumbnail', $thumbpath ); |
25 } |
28 } |
26 |
29 |
27 /** |
30 /** |
28 * Crop an Image to a given size. |
31 * Crop an Image to a given size. |
29 * |
32 * |
30 * @since 2.1.0 |
33 * @since 2.1.0 |
31 * |
34 * |
32 * @param string|int $src_file The source file or Attachment ID. |
35 * @param string|int $src The source file or Attachment ID. |
33 * @param int $src_x The start x position to crop from. |
36 * @param int $src_x The start x position to crop from. |
34 * @param int $src_y The start y position to crop from. |
37 * @param int $src_y The start y position to crop from. |
35 * @param int $src_w The width to crop. |
38 * @param int $src_w The width to crop. |
36 * @param int $src_h The height to crop. |
39 * @param int $src_h The height to crop. |
37 * @param int $dst_w The destination width. |
40 * @param int $dst_w The destination width. |
38 * @param int $dst_h The destination height. |
41 * @param int $dst_h The destination height. |
39 * @param int $src_abs Optional. If the source crop points are absolute. |
42 * @param int $src_abs Optional. If the source crop points are absolute. |
40 * @param string $dst_file Optional. The destination file to write to. |
43 * @param string $dst_file Optional. The destination file to write to. |
41 * @return string New filepath on success, String error message on failure. |
44 * @return string|WP_Error|false New filepath on success, WP_Error or false on failure. |
42 */ |
45 */ |
43 function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { |
46 function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { |
44 if ( is_numeric( $src_file ) ) // Handle int as attachment ID |
47 if ( is_numeric( $src ) ) { // Handle int as attachment ID |
45 $src_file = get_attached_file( $src_file ); |
48 $src_file = get_attached_file( $src ); |
46 |
49 if ( ! file_exists( $src_file ) ) { |
47 $src = wp_load_image( $src_file ); |
50 // If the file doesn't exist, attempt a url fopen on the src link. |
48 |
51 // This can occur with certain file replication plugins. |
49 if ( !is_resource( $src )) |
52 $post = get_post( $src ); |
50 return $src; |
53 $image_type = $post->post_mime_type; |
|
54 $src = load_image_to_edit( $src, $post->post_mime_type, 'full' ); |
|
55 } else { |
|
56 $size = @getimagesize( $src_file ); |
|
57 $image_type = ( $size ) ? $size['mime'] : ''; |
|
58 $src = wp_load_image( $src_file ); |
|
59 } |
|
60 } else { |
|
61 $size = @getimagesize( $src ); |
|
62 $image_type = ( $size ) ? $size['mime'] : ''; |
|
63 $src = wp_load_image( $src ); |
|
64 } |
|
65 |
|
66 if ( ! is_resource( $src ) ) |
|
67 return new WP_Error( 'error_loading_image', $src, $src_file ); |
51 |
68 |
52 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
69 $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
53 |
70 |
54 if ( $src_abs ) { |
71 if ( $src_abs ) { |
55 $src_w -= $src_x; |
72 $src_w -= $src_x; |
56 $src_h -= $src_y; |
73 $src_h -= $src_y; |
57 } |
74 } |
58 |
75 |
59 if (function_exists('imageantialias')) |
76 if ( function_exists( 'imageantialias' ) ) |
60 imageantialias( $dst, true ); |
77 imageantialias( $dst, true ); |
61 |
78 |
62 imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
79 imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
63 |
80 |
64 imagedestroy( $src ); // Free up memory |
81 imagedestroy( $src ); // Free up memory |
65 |
82 |
66 if ( ! $dst_file ) |
83 if ( ! $dst_file ) |
67 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); |
84 $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); |
68 |
85 |
69 $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); |
86 if ( 'image/png' != $image_type ) |
70 |
87 $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); |
71 if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) |
88 |
|
89 // The directory containing the original file may no longer exist when |
|
90 // using a replication plugin. |
|
91 wp_mkdir_p( dirname( $dst_file ) ); |
|
92 |
|
93 $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); |
|
94 |
|
95 if ( 'image/png' == $image_type && imagepng( $dst, $dst_file ) ) |
|
96 return $dst_file; |
|
97 elseif ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) |
72 return $dst_file; |
98 return $dst_file; |
73 else |
99 else |
74 return false; |
100 return false; |
75 } |
101 } |
76 |
102 |
89 $metadata = array(); |
115 $metadata = array(); |
90 if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) { |
116 if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) { |
91 $imagesize = getimagesize( $file ); |
117 $imagesize = getimagesize( $file ); |
92 $metadata['width'] = $imagesize[0]; |
118 $metadata['width'] = $imagesize[0]; |
93 $metadata['height'] = $imagesize[1]; |
119 $metadata['height'] = $imagesize[1]; |
94 list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']); |
120 list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96); |
95 $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'"; |
121 $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'"; |
96 |
122 |
97 // Make the file path relative to the upload dir |
123 // Make the file path relative to the upload dir |
98 $metadata['file'] = _wp_relative_upload_path($file); |
124 $metadata['file'] = _wp_relative_upload_path($file); |
99 |
125 |
100 // make thumbnails and other intermediate sizes |
126 // make thumbnails and other intermediate sizes |
101 global $_wp_additional_image_sizes; |
127 global $_wp_additional_image_sizes; |
102 $temp_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes |
128 |
103 if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) |
129 foreach ( get_intermediate_image_sizes() as $s ) { |
104 $temp_sizes = array_merge( $temp_sizes, array_keys( $_wp_additional_image_sizes ) ); |
130 $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false ); |
105 |
|
106 $temp_sizes = apply_filters( 'intermediate_image_sizes', $temp_sizes ); |
|
107 |
|
108 foreach ( $temp_sizes as $s ) { |
|
109 $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE ); |
|
110 if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) |
131 if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) |
111 $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes |
132 $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes |
112 else |
133 else |
113 $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options |
134 $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options |
114 if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) |
135 if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) |
138 |
159 |
139 return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); |
160 return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); |
140 } |
161 } |
141 |
162 |
142 /** |
163 /** |
143 * Load an image from a string, if PHP supports it. |
164 * Calculated the new dimensions for a downsampled image. |
144 * |
|
145 * @since 2.1.0 |
|
146 * |
|
147 * @param string $file Filename of the image to load. |
|
148 * @return resource The resulting image resource on success, Error string on failure. |
|
149 */ |
|
150 function wp_load_image( $file ) { |
|
151 if ( is_numeric( $file ) ) |
|
152 $file = get_attached_file( $file ); |
|
153 |
|
154 if ( ! file_exists( $file ) ) |
|
155 return sprintf(__('File “%s” doesn’t exist?'), $file); |
|
156 |
|
157 if ( ! function_exists('imagecreatefromstring') ) |
|
158 return __('The GD image library is not installed.'); |
|
159 |
|
160 // Set artificially high because GD uses uncompressed images in memory |
|
161 @ini_set('memory_limit', '256M'); |
|
162 $image = imagecreatefromstring( file_get_contents( $file ) ); |
|
163 |
|
164 if ( !is_resource( $image ) ) |
|
165 return sprintf(__('File “%s” is not an image.'), $file); |
|
166 |
|
167 return $image; |
|
168 } |
|
169 |
|
170 /** |
|
171 * Calculated the new dimentions for a downsampled image. |
|
172 * |
165 * |
173 * @since 2.0.0 |
166 * @since 2.0.0 |
174 * @see wp_shrink_dimensions() |
167 * @see wp_constrain_dimensions() |
175 * |
168 * |
176 * @param int $width Current width of the image |
169 * @param int $width Current width of the image |
177 * @param int $height Current height of the image |
170 * @param int $height Current height of the image |
178 * @return mixed Array(height,width) of shrunk dimensions. |
171 * @return mixed Array(height,width) of shrunk dimensions. |
179 */ |
172 */ |
180 function get_udims( $width, $height) { |
173 function get_udims( $width, $height) { |
181 return wp_shrink_dimensions( $width, $height ); |
174 return wp_constrain_dimensions( $width, $height, 128, 96 ); |
182 } |
|
183 |
|
184 /** |
|
185 * Calculates the new dimentions for a downsampled image. |
|
186 * |
|
187 * @since 2.0.0 |
|
188 * @see wp_constrain_dimensions() |
|
189 * |
|
190 * @param int $width Current width of the image |
|
191 * @param int $height Current height of the image |
|
192 * @param int $wmax Maximum wanted width |
|
193 * @param int $hmax Maximum wanted height |
|
194 * @return mixed Array(height,width) of shrunk dimensions. |
|
195 */ |
|
196 function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) { |
|
197 return wp_constrain_dimensions( $width, $height, $wmax, $hmax ); |
|
198 } |
175 } |
199 |
176 |
200 /** |
177 /** |
201 * Convert a fraction string to a decimal. |
178 * Convert a fraction string to a decimal. |
202 * |
179 * |
266 'title' => '', |
243 'title' => '', |
267 ); |
244 ); |
268 |
245 |
269 // read iptc first, since it might contain data not available in exif such |
246 // read iptc first, since it might contain data not available in exif such |
270 // as caption, description etc |
247 // as caption, description etc |
271 if ( is_callable('iptcparse') ) { |
248 if ( is_callable( 'iptcparse' ) ) { |
272 getimagesize($file, $info); |
249 getimagesize( $file, $info ); |
273 if ( !empty($info['APP13']) ) { |
250 |
274 $iptc = iptcparse($info['APP13']); |
251 if ( ! empty( $info['APP13'] ) ) { |
275 if ( !empty($iptc['2#110'][0]) ) // credit |
252 $iptc = iptcparse( $info['APP13'] ); |
|
253 |
|
254 // headline, "A brief synopsis of the caption." |
|
255 if ( ! empty( $iptc['2#105'][0] ) ) |
|
256 $meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) ); |
|
257 // title, "Many use the Title field to store the filename of the image, though the field may be used in many ways." |
|
258 elseif ( ! empty( $iptc['2#005'][0] ) ) |
|
259 $meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) ); |
|
260 |
|
261 if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption |
|
262 $caption = utf8_encode( trim( $iptc['2#120'][0] ) ); |
|
263 if ( empty( $meta['title'] ) ) { |
|
264 // Assume the title is stored in 2:120 if it's short. |
|
265 if ( strlen( $caption ) < 80 ) |
|
266 $meta['title'] = $caption; |
|
267 else |
|
268 $meta['caption'] = $caption; |
|
269 } elseif ( $caption != $meta['title'] ) { |
|
270 $meta['caption'] = $caption; |
|
271 } |
|
272 } |
|
273 |
|
274 if ( ! empty( $iptc['2#110'][0] ) ) // credit |
276 $meta['credit'] = utf8_encode(trim($iptc['2#110'][0])); |
275 $meta['credit'] = utf8_encode(trim($iptc['2#110'][0])); |
277 elseif ( !empty($iptc['2#080'][0]) ) // byline |
276 elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline |
278 $meta['credit'] = utf8_encode(trim($iptc['2#080'][0])); |
277 $meta['credit'] = utf8_encode(trim($iptc['2#080'][0])); |
279 if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created date and time |
278 |
280 $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]); |
279 if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time |
281 if ( !empty($iptc['2#120'][0]) ) // caption |
280 $meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); |
282 $meta['caption'] = utf8_encode(trim($iptc['2#120'][0])); |
281 |
283 if ( !empty($iptc['2#116'][0]) ) // copyright |
282 if ( ! empty( $iptc['2#116'][0] ) ) // copyright |
284 $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0])); |
283 $meta['copyright'] = utf8_encode( trim( $iptc['2#116'][0] ) ); |
285 if ( !empty($iptc['2#005'][0]) ) // title |
|
286 $meta['title'] = utf8_encode(trim($iptc['2#005'][0])); |
|
287 } |
284 } |
288 } |
285 } |
289 |
286 |
290 // fetch additional info from exif if available |
287 // fetch additional info from exif if available |
291 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)) ) ) { |
288 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 ) ) ) ) { |
292 $exif = @exif_read_data( $file ); |
289 $exif = @exif_read_data( $file ); |
293 if (!empty($exif['FNumber'])) |
290 |
|
291 if ( !empty( $exif['Title'] ) ) |
|
292 $meta['title'] = utf8_encode( trim( $exif['Title'] ) ); |
|
293 |
|
294 if ( ! empty( $exif['ImageDescription'] ) ) { |
|
295 if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) { |
|
296 // Assume the title is stored in ImageDescription |
|
297 $meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) ); |
|
298 if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) |
|
299 $meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) ); |
|
300 } elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) { |
|
301 $meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) ); |
|
302 } |
|
303 } elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) { |
|
304 $meta['caption'] = utf8_encode( trim( $exif['Comments'] ) ); |
|
305 } |
|
306 |
|
307 if ( ! empty( $exif['Artist'] ) ) |
|
308 $meta['credit'] = utf8_encode( trim( $exif['Artist'] ) ); |
|
309 elseif ( ! empty($exif['Author'] ) ) |
|
310 $meta['credit'] = utf8_encode( trim( $exif['Author'] ) ); |
|
311 |
|
312 if ( ! empty( $exif['Copyright'] ) ) |
|
313 $meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) ); |
|
314 if ( ! empty($exif['FNumber'] ) ) |
294 $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
315 $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
295 if (!empty($exif['Model'])) |
316 if ( ! empty($exif['Model'] ) ) |
296 $meta['camera'] = trim( $exif['Model'] ); |
317 $meta['camera'] = utf8_encode( trim( $exif['Model'] ) ); |
297 if (!empty($exif['DateTimeDigitized'])) |
318 if ( ! empty($exif['DateTimeDigitized'] ) ) |
298 $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']); |
319 $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] ); |
299 if (!empty($exif['FocalLength'])) |
320 if ( ! empty($exif['FocalLength'] ) ) |
300 $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] ); |
321 $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] ); |
301 if (!empty($exif['ISOSpeedRatings'])) |
322 if ( ! empty($exif['ISOSpeedRatings'] ) ) { |
302 $meta['iso'] = $exif['ISOSpeedRatings']; |
323 $meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; |
303 if (!empty($exif['ExposureTime'])) |
324 $meta['iso'] = utf8_encode( trim( $meta['iso'] ) ); |
|
325 } |
|
326 if ( ! empty($exif['ExposureTime'] ) ) |
304 $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] ); |
327 $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] ); |
305 } |
328 } |
306 |
329 |
307 return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); |
330 return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); |
308 |
331 |
339 else |
362 else |
340 $result = true; |
363 $result = true; |
341 |
364 |
342 return apply_filters('file_is_displayable_image', $result, $path); |
365 return apply_filters('file_is_displayable_image', $result, $path); |
343 } |
366 } |
|
367 |
|
368 /** |
|
369 * Load an image resource for editing. |
|
370 * |
|
371 * @since 2.9.0 |
|
372 * |
|
373 * @param string $attachment_id Attachment ID. |
|
374 * @param string $mime_type Image mime type. |
|
375 * @param string $size Optional. Image size, defaults to 'full'. |
|
376 * @return resource|false The resulting image resource on success, false on failure. |
|
377 */ |
|
378 function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { |
|
379 $filepath = _load_image_to_edit_path( $attachment_id, $size ); |
|
380 if ( empty( $filepath ) ) |
|
381 return false; |
|
382 |
|
383 switch ( $mime_type ) { |
|
384 case 'image/jpeg': |
|
385 $image = imagecreatefromjpeg($filepath); |
|
386 break; |
|
387 case 'image/png': |
|
388 $image = imagecreatefrompng($filepath); |
|
389 break; |
|
390 case 'image/gif': |
|
391 $image = imagecreatefromgif($filepath); |
|
392 break; |
|
393 default: |
|
394 $image = false; |
|
395 break; |
|
396 } |
|
397 if ( is_resource($image) ) { |
|
398 $image = apply_filters('load_image_to_edit', $image, $attachment_id, $size); |
|
399 if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) { |
|
400 imagealphablending($image, false); |
|
401 imagesavealpha($image, true); |
|
402 } |
|
403 } |
|
404 return $image; |
|
405 } |
|
406 |
|
407 /** |
|
408 * Retrieve the path or url of an attachment's attached file. |
|
409 * |
|
410 * If the attached file is not present on the local filesystem (usually due to replication plugins), |
|
411 * then the url of the file is returned if url fopen is supported. |
|
412 * |
|
413 * @since 3.4.0 |
|
414 * @access private |
|
415 * |
|
416 * @param string $attachment_id Attachment ID. |
|
417 * @param string $size Optional. Image size, defaults to 'full'. |
|
418 * @return string|false File path or url on success, false on failure. |
|
419 */ |
|
420 function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { |
|
421 $filepath = get_attached_file( $attachment_id ); |
|
422 |
|
423 if ( $filepath && file_exists( $filepath ) ) { |
|
424 if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { |
|
425 $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); |
|
426 } |
|
427 } elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) { |
|
428 $filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); |
|
429 } |
|
430 |
|
431 return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); |
|
432 } |
|
433 |
|
434 /** |
|
435 * Copy an existing image file. |
|
436 * |
|
437 * @since 3.4.0 |
|
438 * @access private |
|
439 * |
|
440 * @param string $attachment_id Attachment ID. |
|
441 * @return string|false New file path on success, false on failure. |
|
442 */ |
|
443 function _copy_image_file( $attachment_id ) { |
|
444 $dst_file = $src_file = get_attached_file( $attachment_id ); |
|
445 if ( ! file_exists( $src_file ) ) |
|
446 $src_file = _load_image_to_edit_path( $attachment_id ); |
|
447 |
|
448 if ( $src_file ) { |
|
449 $dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); |
|
450 $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); |
|
451 |
|
452 // The directory containing the original file may no longer exist when |
|
453 // using a replication plugin. |
|
454 wp_mkdir_p( dirname( $dst_file ) ); |
|
455 |
|
456 if ( ! @copy( $src_file, $dst_file ) ) |
|
457 $dst_file = false; |
|
458 } else { |
|
459 $dst_file = false; |
|
460 } |
|
461 |
|
462 return $dst_file; |
|
463 } |