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