author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* File contains all the administration image manipulation functions. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
16 | 10 |
* Crops an image to a given size. |
0 | 11 |
* |
12 |
* @since 2.1.0 |
|
13 |
* |
|
18 | 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 bool|false $src_abs Optional. If the source crop points are absolute. |
|
22 |
* @param string|false $dst_file Optional. The destination file to write to. |
|
0 | 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; |
|
16 | 27 |
if ( is_numeric( $src ) ) { // Handle int as attachment ID. |
0 | 28 |
$src_file = get_attached_file( $src ); |
29 |
||
30 |
if ( ! file_exists( $src_file ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
31 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
32 |
* If the file doesn't exist, attempt a URL fopen on the src link. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
33 |
* This can occur with certain file replication plugins. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
34 |
*/ |
0 | 35 |
$src = _load_image_to_edit_path( $src, 'full' ); |
36 |
} else { |
|
37 |
$src = $src_file; |
|
38 |
} |
|
39 |
} |
|
40 |
||
41 |
$editor = wp_get_image_editor( $src ); |
|
9 | 42 |
if ( is_wp_error( $editor ) ) { |
0 | 43 |
return $editor; |
9 | 44 |
} |
0 | 45 |
|
46 |
$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); |
|
9 | 47 |
if ( is_wp_error( $src ) ) { |
0 | 48 |
return $src; |
9 | 49 |
} |
0 | 50 |
|
9 | 51 |
if ( ! $dst_file ) { |
52 |
$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file ); |
|
53 |
} |
|
0 | 54 |
|
5 | 55 |
/* |
56 |
* The directory containing the original file may no longer exist when |
|
57 |
* using a replication plugin. |
|
58 |
*/ |
|
0 | 59 |
wp_mkdir_p( dirname( $dst_file ) ); |
60 |
||
9 | 61 |
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) ); |
0 | 62 |
|
63 |
$result = $editor->save( $dst_file ); |
|
9 | 64 |
if ( is_wp_error( $result ) ) { |
0 | 65 |
return $result; |
9 | 66 |
} |
0 | 67 |
|
19 | 68 |
if ( ! empty( $result['path'] ) ) { |
69 |
return $result['path']; |
|
70 |
} |
|
71 |
||
0 | 72 |
return $dst_file; |
73 |
} |
|
74 |
||
75 |
/** |
|
16 | 76 |
* Compare the existing image sub-sizes (as saved in the attachment meta) |
77 |
* to the currently registered image sub-sizes, and return the difference. |
|
78 |
* |
|
79 |
* Registered sub-sizes that are larger than the image are skipped. |
|
80 |
* |
|
81 |
* @since 5.3.0 |
|
82 |
* |
|
83 |
* @param int $attachment_id The image attachment post ID. |
|
19 | 84 |
* @return array[] Associative array of arrays of image sub-size information for |
85 |
* missing image sizes, keyed by image size name. |
|
16 | 86 |
*/ |
87 |
function wp_get_missing_image_subsizes( $attachment_id ) { |
|
88 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
|
89 |
return array(); |
|
90 |
} |
|
91 |
||
92 |
$registered_sizes = wp_get_registered_image_subsizes(); |
|
93 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
94 |
||
95 |
// Meta error? |
|
96 |
if ( empty( $image_meta ) ) { |
|
97 |
return $registered_sizes; |
|
98 |
} |
|
99 |
||
100 |
// Use the originally uploaded image dimensions as full_width and full_height. |
|
101 |
if ( ! empty( $image_meta['original_image'] ) ) { |
|
102 |
$image_file = wp_get_original_image_path( $attachment_id ); |
|
18 | 103 |
$imagesize = wp_getimagesize( $image_file ); |
16 | 104 |
} |
105 |
||
106 |
if ( ! empty( $imagesize ) ) { |
|
107 |
$full_width = $imagesize[0]; |
|
108 |
$full_height = $imagesize[1]; |
|
109 |
} else { |
|
110 |
$full_width = (int) $image_meta['width']; |
|
111 |
$full_height = (int) $image_meta['height']; |
|
112 |
} |
|
113 |
||
114 |
$possible_sizes = array(); |
|
115 |
||
116 |
// Skip registered sizes that are too large for the uploaded image. |
|
117 |
foreach ( $registered_sizes as $size_name => $size_data ) { |
|
118 |
if ( image_resize_dimensions( $full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'] ) ) { |
|
119 |
$possible_sizes[ $size_name ] = $size_data; |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
if ( empty( $image_meta['sizes'] ) ) { |
|
124 |
$image_meta['sizes'] = array(); |
|
125 |
} |
|
126 |
||
127 |
/* |
|
128 |
* Remove sizes that already exist. Only checks for matching "size names". |
|
129 |
* It is possible that the dimensions for a particular size name have changed. |
|
130 |
* For example the user has changed the values on the Settings -> Media screen. |
|
131 |
* However we keep the old sub-sizes with the previous dimensions |
|
132 |
* as the image may have been used in an older post. |
|
133 |
*/ |
|
134 |
$missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] ); |
|
135 |
||
136 |
/** |
|
137 |
* Filters the array of missing image sub-sizes for an uploaded image. |
|
138 |
* |
|
139 |
* @since 5.3.0 |
|
140 |
* |
|
19 | 141 |
* @param array[] $missing_sizes Associative array of arrays of image sub-size information for |
142 |
* missing image sizes, keyed by image size name. |
|
143 |
* @param array $image_meta The image meta data. |
|
144 |
* @param int $attachment_id The image attachment post ID. |
|
16 | 145 |
*/ |
146 |
return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id ); |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* If any of the currently registered image sub-sizes are missing, |
|
151 |
* create them and update the image meta data. |
|
152 |
* |
|
153 |
* @since 5.3.0 |
|
154 |
* |
|
155 |
* @param int $attachment_id The image attachment post ID. |
|
156 |
* @return array|WP_Error The updated image meta data array or WP_Error object |
|
157 |
* if both the image meta and the attached file are missing. |
|
158 |
*/ |
|
159 |
function wp_update_image_subsizes( $attachment_id ) { |
|
160 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
161 |
$image_file = wp_get_original_image_path( $attachment_id ); |
|
162 |
||
163 |
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
164 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
* Previously failed upload? |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
166 |
* If there is an uploaded file, make all sub-sizes and generate all of the attachment meta. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
*/ |
16 | 168 |
if ( ! empty( $image_file ) ) { |
169 |
$image_meta = wp_create_image_subsizes( $image_file, $attachment_id ); |
|
170 |
} else { |
|
171 |
return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) ); |
|
172 |
} |
|
173 |
} else { |
|
174 |
$missing_sizes = wp_get_missing_image_subsizes( $attachment_id ); |
|
175 |
||
176 |
if ( empty( $missing_sizes ) ) { |
|
177 |
return $image_meta; |
|
178 |
} |
|
179 |
||
180 |
// This also updates the image meta. |
|
181 |
$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id ); |
|
182 |
} |
|
183 |
||
184 |
/** This filter is documented in wp-admin/includes/image.php */ |
|
185 |
$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' ); |
|
186 |
||
187 |
// Save the updated metadata. |
|
188 |
wp_update_attachment_metadata( $attachment_id, $image_meta ); |
|
189 |
||
190 |
return $image_meta; |
|
191 |
} |
|
192 |
||
193 |
/** |
|
194 |
* Updates the attached file and image meta data when the original image was edited. |
|
195 |
* |
|
196 |
* @since 5.3.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
197 |
* @since 6.0.0 The `$filesize` value was added to the returned array. |
16 | 198 |
* @access private |
199 |
* |
|
200 |
* @param array $saved_data The data returned from WP_Image_Editor after successfully saving an image. |
|
201 |
* @param string $original_file Path to the original file. |
|
202 |
* @param array $image_meta The image meta data. |
|
203 |
* @param int $attachment_id The attachment post ID. |
|
204 |
* @return array The updated image meta data. |
|
205 |
*/ |
|
206 |
function _wp_image_meta_replace_original( $saved_data, $original_file, $image_meta, $attachment_id ) { |
|
207 |
$new_file = $saved_data['path']; |
|
208 |
||
209 |
// Update the attached file meta. |
|
210 |
update_attached_file( $attachment_id, $new_file ); |
|
211 |
||
212 |
// Width and height of the new image. |
|
213 |
$image_meta['width'] = $saved_data['width']; |
|
214 |
$image_meta['height'] = $saved_data['height']; |
|
215 |
||
216 |
// Make the file path relative to the upload dir. |
|
217 |
$image_meta['file'] = _wp_relative_upload_path( $new_file ); |
|
218 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
219 |
// Add image file size. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
220 |
$image_meta['filesize'] = wp_filesize( $new_file ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
221 |
|
16 | 222 |
// Store the original image file name in image_meta. |
223 |
$image_meta['original_image'] = wp_basename( $original_file ); |
|
224 |
||
225 |
return $image_meta; |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata. |
|
230 |
* |
|
231 |
* Intended for use after an image is uploaded. Saves/updates the image metadata after each |
|
232 |
* sub-size is created. If there was an error, it is added to the returned image metadata array. |
|
233 |
* |
|
234 |
* @since 5.3.0 |
|
235 |
* |
|
236 |
* @param string $file Full path to the image file. |
|
19 | 237 |
* @param int $attachment_id Attachment ID to process. |
16 | 238 |
* @return array The image attachment meta data. |
239 |
*/ |
|
240 |
function wp_create_image_subsizes( $file, $attachment_id ) { |
|
18 | 241 |
$imagesize = wp_getimagesize( $file ); |
16 | 242 |
|
243 |
if ( empty( $imagesize ) ) { |
|
244 |
// File is not an image. |
|
245 |
return array(); |
|
246 |
} |
|
247 |
||
248 |
// Default image meta. |
|
249 |
$image_meta = array( |
|
19 | 250 |
'width' => $imagesize[0], |
251 |
'height' => $imagesize[1], |
|
252 |
'file' => _wp_relative_upload_path( $file ), |
|
253 |
'filesize' => wp_filesize( $file ), |
|
254 |
'sizes' => array(), |
|
16 | 255 |
); |
256 |
||
257 |
// Fetch additional metadata from EXIF/IPTC. |
|
258 |
$exif_meta = wp_read_image_metadata( $file ); |
|
259 |
||
260 |
if ( $exif_meta ) { |
|
261 |
$image_meta['image_meta'] = $exif_meta; |
|
262 |
} |
|
263 |
||
264 |
// Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736. |
|
265 |
if ( 'image/png' !== $imagesize['mime'] ) { |
|
266 |
||
267 |
/** |
|
268 |
* Filters the "BIG image" threshold value. |
|
269 |
* |
|
270 |
* If the original image width or height is above the threshold, it will be scaled down. The threshold is |
|
271 |
* used as max width and max height. The scaled down image will be used as the largest available size, including |
|
272 |
* the `_wp_attached_file` post meta value. |
|
273 |
* |
|
274 |
* Returning `false` from the filter callback will disable the scaling. |
|
275 |
* |
|
276 |
* @since 5.3.0 |
|
277 |
* |
|
278 |
* @param int $threshold The threshold value in pixels. Default 2560. |
|
279 |
* @param array $imagesize { |
|
280 |
* Indexed array of the image width and height in pixels. |
|
281 |
* |
|
282 |
* @type int $0 The image width. |
|
283 |
* @type int $1 The image height. |
|
284 |
* } |
|
285 |
* @param string $file Full path to the uploaded image file. |
|
286 |
* @param int $attachment_id Attachment post ID. |
|
287 |
*/ |
|
288 |
$threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id ); |
|
289 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
290 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
291 |
* If the original image's dimensions are over the threshold, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
292 |
* scale the image and use it as the "full" size. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
293 |
*/ |
16 | 294 |
if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) { |
295 |
$editor = wp_get_image_editor( $file ); |
|
296 |
||
297 |
if ( is_wp_error( $editor ) ) { |
|
298 |
// This image cannot be edited. |
|
299 |
return $image_meta; |
|
300 |
} |
|
301 |
||
302 |
// Resize the image. |
|
303 |
$resized = $editor->resize( $threshold, $threshold ); |
|
304 |
$rotated = null; |
|
305 |
||
306 |
// If there is EXIF data, rotate according to EXIF Orientation. |
|
307 |
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) { |
|
308 |
$resized = $editor->maybe_exif_rotate(); |
|
309 |
$rotated = $resized; |
|
310 |
} |
|
311 |
||
312 |
if ( ! is_wp_error( $resized ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
313 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
314 |
* Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg". |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
315 |
* This doesn't affect the sub-sizes names as they are generated from the original image (for best quality). |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
316 |
*/ |
16 | 317 |
$saved = $editor->save( $editor->generate_filename( 'scaled' ) ); |
318 |
||
319 |
if ( ! is_wp_error( $saved ) ) { |
|
320 |
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); |
|
321 |
||
322 |
// If the image was rotated update the stored EXIF data. |
|
323 |
if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) { |
|
324 |
$image_meta['image_meta']['orientation'] = 1; |
|
325 |
} |
|
326 |
} else { |
|
327 |
// TODO: Log errors. |
|
328 |
} |
|
329 |
} else { |
|
330 |
// TODO: Log errors. |
|
331 |
} |
|
332 |
} elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) { |
|
333 |
// Rotate the whole original image if there is EXIF data and "orientation" is not 1. |
|
334 |
||
335 |
$editor = wp_get_image_editor( $file ); |
|
336 |
||
337 |
if ( is_wp_error( $editor ) ) { |
|
338 |
// This image cannot be edited. |
|
339 |
return $image_meta; |
|
340 |
} |
|
341 |
||
342 |
// Rotate the image. |
|
343 |
$rotated = $editor->maybe_exif_rotate(); |
|
344 |
||
345 |
if ( true === $rotated ) { |
|
346 |
// Append `-rotated` to the image file name. |
|
347 |
$saved = $editor->save( $editor->generate_filename( 'rotated' ) ); |
|
348 |
||
349 |
if ( ! is_wp_error( $saved ) ) { |
|
350 |
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); |
|
351 |
||
352 |
// Update the stored EXIF data. |
|
353 |
if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { |
|
354 |
$image_meta['image_meta']['orientation'] = 1; |
|
355 |
} |
|
356 |
} else { |
|
357 |
// TODO: Log errors. |
|
358 |
} |
|
359 |
} |
|
360 |
} |
|
361 |
} |
|
362 |
||
363 |
/* |
|
364 |
* Initial save of the new metadata. |
|
365 |
* At this point the file was uploaded and moved to the uploads directory |
|
366 |
* but the image sub-sizes haven't been created yet and the `sizes` array is empty. |
|
367 |
*/ |
|
368 |
wp_update_attachment_metadata( $attachment_id, $image_meta ); |
|
369 |
||
370 |
$new_sizes = wp_get_registered_image_subsizes(); |
|
371 |
||
372 |
/** |
|
373 |
* Filters the image sizes automatically generated when uploading an image. |
|
374 |
* |
|
375 |
* @since 2.9.0 |
|
376 |
* @since 4.4.0 Added the `$image_meta` argument. |
|
377 |
* @since 5.3.0 Added the `$attachment_id` argument. |
|
378 |
* |
|
379 |
* @param array $new_sizes Associative array of image sizes to be created. |
|
380 |
* @param array $image_meta The image meta data: width, height, file, sizes, etc. |
|
381 |
* @param int $attachment_id The attachment post ID for the image. |
|
382 |
*/ |
|
383 |
$new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id ); |
|
384 |
||
385 |
return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ); |
|
386 |
} |
|
387 |
||
388 |
/** |
|
389 |
* Low-level function to create image sub-sizes. |
|
390 |
* |
|
391 |
* Updates the image meta after each sub-size is created. |
|
392 |
* Errors are stored in the returned image metadata array. |
|
393 |
* |
|
394 |
* @since 5.3.0 |
|
395 |
* @access private |
|
396 |
* |
|
397 |
* @param array $new_sizes Array defining what sizes to create. |
|
398 |
* @param string $file Full path to the image file. |
|
399 |
* @param array $image_meta The attachment meta data array. |
|
19 | 400 |
* @param int $attachment_id Attachment ID to process. |
16 | 401 |
* @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing. |
402 |
*/ |
|
403 |
function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) { |
|
404 |
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) { |
|
405 |
// Not an image attachment. |
|
406 |
return array(); |
|
407 |
} |
|
408 |
||
409 |
// Check if any of the new sizes already exist. |
|
410 |
if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) { |
|
411 |
foreach ( $image_meta['sizes'] as $size_name => $size_meta ) { |
|
412 |
/* |
|
413 |
* Only checks "size name" so we don't override existing images even if the dimensions |
|
414 |
* don't match the currently defined size with the same name. |
|
415 |
* To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta. |
|
416 |
*/ |
|
417 |
if ( array_key_exists( $size_name, $new_sizes ) ) { |
|
418 |
unset( $new_sizes[ $size_name ] ); |
|
419 |
} |
|
420 |
} |
|
421 |
} else { |
|
422 |
$image_meta['sizes'] = array(); |
|
423 |
} |
|
424 |
||
425 |
if ( empty( $new_sizes ) ) { |
|
426 |
// Nothing to do... |
|
427 |
return $image_meta; |
|
428 |
} |
|
429 |
||
430 |
/* |
|
431 |
* Sort the image sub-sizes in order of priority when creating them. |
|
432 |
* This ensures there is an appropriate sub-size the user can access immediately |
|
433 |
* even when there was an error and not all sub-sizes were created. |
|
434 |
*/ |
|
435 |
$priority = array( |
|
436 |
'medium' => null, |
|
437 |
'large' => null, |
|
438 |
'thumbnail' => null, |
|
439 |
'medium_large' => null, |
|
440 |
); |
|
441 |
||
442 |
$new_sizes = array_filter( array_merge( $priority, $new_sizes ) ); |
|
443 |
||
444 |
$editor = wp_get_image_editor( $file ); |
|
445 |
||
446 |
if ( is_wp_error( $editor ) ) { |
|
447 |
// The image cannot be edited. |
|
448 |
return $image_meta; |
|
449 |
} |
|
450 |
||
451 |
// If stored EXIF data exists, rotate the source image before creating sub-sizes. |
|
452 |
if ( ! empty( $image_meta['image_meta'] ) ) { |
|
453 |
$rotated = $editor->maybe_exif_rotate(); |
|
454 |
||
455 |
if ( is_wp_error( $rotated ) ) { |
|
456 |
// TODO: Log errors. |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
if ( method_exists( $editor, 'make_subsize' ) ) { |
|
461 |
foreach ( $new_sizes as $new_size_name => $new_size_data ) { |
|
462 |
$new_size_meta = $editor->make_subsize( $new_size_data ); |
|
463 |
||
464 |
if ( is_wp_error( $new_size_meta ) ) { |
|
465 |
// TODO: Log errors. |
|
466 |
} else { |
|
467 |
// Save the size meta value. |
|
468 |
$image_meta['sizes'][ $new_size_name ] = $new_size_meta; |
|
469 |
wp_update_attachment_metadata( $attachment_id, $image_meta ); |
|
470 |
} |
|
471 |
} |
|
472 |
} else { |
|
473 |
// Fall back to `$editor->multi_resize()`. |
|
474 |
$created_sizes = $editor->multi_resize( $new_sizes ); |
|
475 |
||
476 |
if ( ! empty( $created_sizes ) ) { |
|
477 |
$image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes ); |
|
478 |
wp_update_attachment_metadata( $attachment_id, $image_meta ); |
|
479 |
} |
|
480 |
} |
|
481 |
||
482 |
return $image_meta; |
|
483 |
} |
|
484 |
||
485 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
486 |
* Copy parent attachment properties to newly cropped image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
487 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
488 |
* @since 6.5.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
489 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
490 |
* @param string $cropped Path to the cropped image file. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
491 |
* @param int $parent_attachment_id Parent file Attachment ID. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
492 |
* @param string $context Control calling the function. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
493 |
* @return array Properties of attachment. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
494 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
495 |
function wp_copy_parent_attachment_properties( $cropped, $parent_attachment_id, $context = '' ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
496 |
$parent = get_post( $parent_attachment_id ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
497 |
$parent_url = wp_get_attachment_url( $parent->ID ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
498 |
$parent_basename = wp_basename( $parent_url ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
499 |
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
500 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
501 |
$size = wp_getimagesize( $cropped ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
502 |
$image_type = $size ? $size['mime'] : 'image/jpeg'; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
503 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
504 |
$sanitized_post_title = sanitize_file_name( $parent->post_title ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
505 |
$use_original_title = ( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
506 |
( '' !== trim( $parent->post_title ) ) && |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
507 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
508 |
* Check if the original image has a title other than the "filename" default, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
509 |
* meaning the image had a title when originally uploaded or its title was edited. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
510 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
511 |
( $parent_basename !== $sanitized_post_title ) && |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
512 |
( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
513 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
514 |
$use_original_description = ( '' !== trim( $parent->post_content ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
515 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
516 |
$attachment = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
517 |
'post_title' => $use_original_title ? $parent->post_title : wp_basename( $cropped ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
518 |
'post_content' => $use_original_description ? $parent->post_content : $url, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
519 |
'post_mime_type' => $image_type, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
520 |
'guid' => $url, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
521 |
'context' => $context, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
522 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
523 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
524 |
// Copy the image caption attribute (post_excerpt field) from the original image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
525 |
if ( '' !== trim( $parent->post_excerpt ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
526 |
$attachment['post_excerpt'] = $parent->post_excerpt; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
527 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
528 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
529 |
// Copy the image alt text attribute from the original image. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
530 |
if ( '' !== trim( $parent->_wp_attachment_image_alt ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
531 |
$attachment['meta_input'] = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
532 |
'_wp_attachment_image_alt' => wp_slash( $parent->_wp_attachment_image_alt ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
533 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
534 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
535 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
536 |
$attachment['post_parent'] = $parent_attachment_id; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
537 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
538 |
return $attachment; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
539 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
540 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
541 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
542 |
* Generates attachment meta data and create image sub-sizes for images. |
0 | 543 |
* |
544 |
* @since 2.1.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
545 |
* @since 6.0.0 The `$filesize` value was added to the returned array. |
0 | 546 |
* |
19 | 547 |
* @param int $attachment_id Attachment ID to process. |
548 |
* @param string $file Filepath of the attached image. |
|
18 | 549 |
* @return array Metadata for attachment. |
0 | 550 |
*/ |
551 |
function wp_generate_attachment_metadata( $attachment_id, $file ) { |
|
552 |
$attachment = get_post( $attachment_id ); |
|
553 |
||
9 | 554 |
$metadata = array(); |
555 |
$support = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
556 |
$mime_type = get_post_mime_type( $attachment ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
558 |
if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) { |
5 | 559 |
// Make thumbnails and other intermediate sizes. |
16 | 560 |
$metadata = wp_create_image_subsizes( $file, $attachment_id ); |
5 | 561 |
} elseif ( wp_attachment_is( 'video', $attachment ) ) { |
0 | 562 |
$metadata = wp_read_video_metadata( $file ); |
9 | 563 |
$support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); |
5 | 564 |
} elseif ( wp_attachment_is( 'audio', $attachment ) ) { |
0 | 565 |
$metadata = wp_read_audio_metadata( $file ); |
9 | 566 |
$support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); |
0 | 567 |
} |
568 |
||
18 | 569 |
/* |
570 |
* wp_read_video_metadata() and wp_read_audio_metadata() return `false` |
|
571 |
* if the attachment does not exist in the local filesystem, |
|
572 |
* so make sure to convert the value to an array. |
|
573 |
*/ |
|
574 |
if ( ! is_array( $metadata ) ) { |
|
575 |
$metadata = array(); |
|
576 |
} |
|
577 |
||
0 | 578 |
if ( $support && ! empty( $metadata['image']['data'] ) ) { |
5 | 579 |
// Check for existing cover. |
9 | 580 |
$hash = md5( $metadata['image']['data'] ); |
581 |
$posts = get_posts( |
|
582 |
array( |
|
583 |
'fields' => 'ids', |
|
584 |
'post_type' => 'attachment', |
|
585 |
'post_mime_type' => $metadata['image']['mime'], |
|
586 |
'post_status' => 'inherit', |
|
587 |
'posts_per_page' => 1, |
|
588 |
'meta_key' => '_cover_hash', |
|
589 |
'meta_value' => $hash, |
|
590 |
) |
|
591 |
); |
|
5 | 592 |
$exists = reset( $posts ); |
593 |
||
594 |
if ( ! empty( $exists ) ) { |
|
595 |
update_post_meta( $attachment_id, '_thumbnail_id', $exists ); |
|
596 |
} else { |
|
597 |
$ext = '.jpg'; |
|
598 |
switch ( $metadata['image']['mime'] ) { |
|
9 | 599 |
case 'image/gif': |
600 |
$ext = '.gif'; |
|
601 |
break; |
|
602 |
case 'image/png': |
|
603 |
$ext = '.png'; |
|
604 |
break; |
|
18 | 605 |
case 'image/webp': |
606 |
$ext = '.webp'; |
|
607 |
break; |
|
5 | 608 |
} |
9 | 609 |
$basename = str_replace( '.', '-', wp_basename( $file ) ) . '-image' . $ext; |
5 | 610 |
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); |
611 |
if ( false === $uploaded['error'] ) { |
|
612 |
$image_attachment = array( |
|
613 |
'post_mime_type' => $metadata['image']['mime'], |
|
9 | 614 |
'post_type' => 'attachment', |
615 |
'post_content' => '', |
|
5 | 616 |
); |
617 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
618 |
* Filters the parameters for the attachment thumbnail creation. |
5 | 619 |
* |
620 |
* @since 3.9.0 |
|
621 |
* |
|
622 |
* @param array $image_attachment An array of parameters to create the thumbnail. |
|
623 |
* @param array $metadata Current attachment metadata. |
|
16 | 624 |
* @param array $uploaded { |
625 |
* Information about the newly-uploaded file. |
|
626 |
* |
|
627 |
* @type string $file Filename of the newly-uploaded file. |
|
628 |
* @type string $url URL of the uploaded file. |
|
629 |
* @type string $type File type. |
|
630 |
* } |
|
5 | 631 |
*/ |
632 |
$image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded ); |
|
633 |
||
634 |
$sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] ); |
|
635 |
add_post_meta( $sub_attachment_id, '_cover_hash', $hash ); |
|
636 |
$attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); |
|
637 |
wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); |
|
638 |
update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); |
|
639 |
} |
|
0 | 640 |
} |
9 | 641 |
} elseif ( 'application/pdf' === $mime_type ) { |
642 |
// Try to create image thumbnails for PDFs. |
|
643 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
$fallback_sizes = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
645 |
'thumbnail', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
'medium', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
647 |
'large', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
648 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
649 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* Filters the image sizes generated for non-image mime types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* |
16 | 655 |
* @param string[] $fallback_sizes An array of image size names. |
656 |
* @param array $metadata Current attachment metadata. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
$fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
|
16 | 660 |
$registered_sizes = wp_get_registered_image_subsizes(); |
661 |
$merged_sizes = array_intersect_key( $registered_sizes, array_flip( $fallback_sizes ) ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
|
16 | 663 |
// Force thumbnails to be soft crops. |
664 |
if ( isset( $merged_sizes['thumbnail'] ) && is_array( $merged_sizes['thumbnail'] ) ) { |
|
665 |
$merged_sizes['thumbnail']['crop'] = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
666 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
// Only load PDFs in an image editor if we're processing sizes. |
16 | 669 |
if ( ! empty( $merged_sizes ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
$editor = wp_get_image_editor( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
|
16 | 672 |
if ( ! is_wp_error( $editor ) ) { // No support for this type of file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
* PDFs may have the same file filename as JPEGs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
* Ensure the PDF preview image does not overwrite any JPEG images that already exist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
676 |
*/ |
9 | 677 |
$dirname = dirname( $file ) . '/'; |
678 |
$ext = '.' . pathinfo( $file, PATHINFO_EXTENSION ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
$preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
680 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
$uploaded = $editor->save( $preview_file, 'image/jpeg' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
unset( $editor ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
684 |
// Resize based on the full size image, rather than the source. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
if ( ! is_wp_error( $uploaded ) ) { |
16 | 686 |
$image_file = $uploaded['path']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
unset( $uploaded['path'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
|
16 | 689 |
$metadata['sizes'] = array( |
690 |
'full' => $uploaded, |
|
691 |
); |
|
692 |
||
693 |
// Save the meta data before any image post-processing errors could happen. |
|
694 |
wp_update_attachment_metadata( $attachment_id, $metadata ); |
|
695 |
||
696 |
// Create sub-sizes saving the image meta after each. |
|
697 |
$metadata = _wp_make_subsizes( $merged_sizes, $image_file, $metadata, $attachment_id ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
698 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
699 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
} |
5 | 702 |
|
703 |
// Remove the blob of binary data from the array. |
|
18 | 704 |
unset( $metadata['image']['data'] ); |
0 | 705 |
|
19 | 706 |
// Capture file size for cases where it has not been captured yet, such as PDFs. |
707 |
if ( ! isset( $metadata['filesize'] ) && file_exists( $file ) ) { |
|
708 |
$metadata['filesize'] = wp_filesize( $file ); |
|
709 |
} |
|
710 |
||
5 | 711 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
712 |
* Filters the generated attachment meta data. |
5 | 713 |
* |
714 |
* @since 2.1.0 |
|
16 | 715 |
* @since 5.3.0 The `$context` parameter was added. |
5 | 716 |
* |
16 | 717 |
* @param array $metadata An array of attachment meta data. |
718 |
* @param int $attachment_id Current attachment ID. |
|
719 |
* @param string $context Additional context. Can be 'create' when metadata was initially created for new attachment |
|
720 |
* or 'update' when the metadata was updated. |
|
5 | 721 |
*/ |
16 | 722 |
return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' ); |
0 | 723 |
} |
724 |
||
725 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
726 |
* Converts a fraction string to a decimal. |
0 | 727 |
* |
728 |
* @since 2.5.0 |
|
729 |
* |
|
19 | 730 |
* @param string $str Fraction string. |
731 |
* @return int|float Returns calculated fraction or integer 0 on invalid input. |
|
0 | 732 |
*/ |
9 | 733 |
function wp_exif_frac2dec( $str ) { |
19 | 734 |
if ( ! is_scalar( $str ) || is_bool( $str ) ) { |
735 |
return 0; |
|
736 |
} |
|
737 |
||
738 |
if ( ! is_string( $str ) ) { |
|
739 |
return $str; // This can only be an integer or float, so this is fine. |
|
740 |
} |
|
741 |
||
742 |
// Fractions passed as a string must contain a single `/`. |
|
743 |
if ( substr_count( $str, '/' ) !== 1 ) { |
|
744 |
if ( is_numeric( $str ) ) { |
|
745 |
return (float) $str; |
|
746 |
} |
|
747 |
||
748 |
return 0; |
|
16 | 749 |
} |
750 |
||
751 |
list( $numerator, $denominator ) = explode( '/', $str ); |
|
19 | 752 |
|
753 |
// Both the numerator and the denominator must be numbers. |
|
754 |
if ( ! is_numeric( $numerator ) || ! is_numeric( $denominator ) ) { |
|
755 |
return 0; |
|
9 | 756 |
} |
19 | 757 |
|
758 |
// The denominator must not be zero. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
759 |
if ( 0 == $denominator ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- Deliberate loose comparison. |
19 | 760 |
return 0; |
761 |
} |
|
762 |
||
763 |
return $numerator / $denominator; |
|
0 | 764 |
} |
765 |
||
766 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
767 |
* Converts the exif date format to a unix timestamp. |
0 | 768 |
* |
769 |
* @since 2.5.0 |
|
770 |
* |
|
19 | 771 |
* @param string $str A date string expected to be in Exif format (Y:m:d H:i:s). |
772 |
* @return int|false The unix timestamp, or false on failure. |
|
0 | 773 |
*/ |
9 | 774 |
function wp_exif_date2ts( $str ) { |
16 | 775 |
list( $date, $time ) = explode( ' ', trim( $str ) ); |
776 |
list( $y, $m, $d ) = explode( ':', $date ); |
|
0 | 777 |
|
778 |
return strtotime( "{$y}-{$m}-{$d} {$time}" ); |
|
779 |
} |
|
780 |
||
781 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
782 |
* Gets extended image metadata, exif or iptc as available. |
0 | 783 |
* |
784 |
* Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso |
|
785 |
* created_timestamp, focal_length, shutter_speed, and title. |
|
786 |
* |
|
787 |
* The IPTC metadata that is retrieved is APP13, credit, byline, created date |
|
788 |
* and time, caption, copyright, and title. Also includes FNumber, Model, |
|
789 |
* DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. |
|
790 |
* |
|
791 |
* @todo Try other exif libraries if available. |
|
792 |
* @since 2.5.0 |
|
793 |
* |
|
794 |
* @param string $file |
|
18 | 795 |
* @return array|false Image metadata array on success, false on failure. |
0 | 796 |
*/ |
797 |
function wp_read_image_metadata( $file ) { |
|
9 | 798 |
if ( ! file_exists( $file ) ) { |
0 | 799 |
return false; |
9 | 800 |
} |
0 | 801 |
|
18 | 802 |
list( , , $image_type ) = wp_getimagesize( $file ); |
0 | 803 |
|
5 | 804 |
/* |
805 |
* EXIF contains a bunch of data we'll probably never need formatted in ways |
|
806 |
* that are difficult to use. We'll normalize it and just extract the fields |
|
807 |
* that are likely to be useful. Fractions and numbers are converted to |
|
808 |
* floats, dates to unix timestamps, and everything else to strings. |
|
809 |
*/ |
|
0 | 810 |
$meta = array( |
9 | 811 |
'aperture' => 0, |
812 |
'credit' => '', |
|
813 |
'camera' => '', |
|
814 |
'caption' => '', |
|
0 | 815 |
'created_timestamp' => 0, |
9 | 816 |
'copyright' => '', |
817 |
'focal_length' => 0, |
|
818 |
'iso' => 0, |
|
819 |
'shutter_speed' => 0, |
|
820 |
'title' => '', |
|
821 |
'orientation' => 0, |
|
822 |
'keywords' => array(), |
|
0 | 823 |
); |
824 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
$iptc = array(); |
18 | 826 |
$info = array(); |
5 | 827 |
/* |
828 |
* Read IPTC first, since it might contain data not available in exif such |
|
829 |
* as caption, description etc. |
|
830 |
*/ |
|
0 | 831 |
if ( is_callable( 'iptcparse' ) ) { |
18 | 832 |
wp_getimagesize( $file, $info ); |
0 | 833 |
|
834 |
if ( ! empty( $info['APP13'] ) ) { |
|
18 | 835 |
// Don't silence errors when in debug mode, unless running unit tests. |
836 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG |
|
837 |
&& ! defined( 'WP_RUN_CORE_TESTS' ) |
|
838 |
) { |
|
839 |
$iptc = iptcparse( $info['APP13'] ); |
|
840 |
} else { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
841 |
// Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480 |
18 | 842 |
$iptc = @iptcparse( $info['APP13'] ); |
843 |
} |
|
0 | 844 |
|
19 | 845 |
if ( ! is_array( $iptc ) ) { |
846 |
$iptc = array(); |
|
847 |
} |
|
848 |
||
16 | 849 |
// Headline, "A brief synopsis of the caption". |
5 | 850 |
if ( ! empty( $iptc['2#105'][0] ) ) { |
0 | 851 |
$meta['title'] = trim( $iptc['2#105'][0] ); |
9 | 852 |
/* |
853 |
* Title, "Many use the Title field to store the filename of the image, |
|
16 | 854 |
* though the field may be used in many ways". |
9 | 855 |
*/ |
5 | 856 |
} elseif ( ! empty( $iptc['2#005'][0] ) ) { |
0 | 857 |
$meta['title'] = trim( $iptc['2#005'][0] ); |
5 | 858 |
} |
0 | 859 |
|
16 | 860 |
if ( ! empty( $iptc['2#120'][0] ) ) { // Description / legacy caption. |
0 | 861 |
$caption = trim( $iptc['2#120'][0] ); |
5 | 862 |
|
863 |
mbstring_binary_safe_encoding(); |
|
864 |
$caption_length = strlen( $caption ); |
|
865 |
reset_mbstring_encoding(); |
|
866 |
||
867 |
if ( empty( $meta['title'] ) && $caption_length < 80 ) { |
|
0 | 868 |
// Assume the title is stored in 2:120 if it's short. |
5 | 869 |
$meta['title'] = $caption; |
0 | 870 |
} |
5 | 871 |
|
872 |
$meta['caption'] = $caption; |
|
0 | 873 |
} |
874 |
||
16 | 875 |
if ( ! empty( $iptc['2#110'][0] ) ) { // Credit. |
0 | 876 |
$meta['credit'] = trim( $iptc['2#110'][0] ); |
16 | 877 |
} elseif ( ! empty( $iptc['2#080'][0] ) ) { // Creator / legacy byline. |
0 | 878 |
$meta['credit'] = trim( $iptc['2#080'][0] ); |
9 | 879 |
} |
0 | 880 |
|
16 | 881 |
if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) { // Created date and time. |
0 | 882 |
$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); |
9 | 883 |
} |
0 | 884 |
|
16 | 885 |
if ( ! empty( $iptc['2#116'][0] ) ) { // Copyright. |
0 | 886 |
$meta['copyright'] = trim( $iptc['2#116'][0] ); |
9 | 887 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
888 |
|
16 | 889 |
if ( ! empty( $iptc['2#025'][0] ) ) { // Keywords array. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
890 |
$meta['keywords'] = array_values( $iptc['2#025'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
891 |
} |
9 | 892 |
} |
0 | 893 |
} |
894 |
||
9 | 895 |
$exif = array(); |
896 |
||
5 | 897 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
898 |
* Filters the image types to check for exif data. |
5 | 899 |
* |
900 |
* @since 2.5.0 |
|
901 |
* |
|
19 | 902 |
* @param int[] $image_types Array of image types to check for exif data. Each value |
903 |
* is usually one of the `IMAGETYPE_*` constants. |
|
5 | 904 |
*/ |
9 | 905 |
$exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ); |
906 |
||
16 | 907 |
if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) { |
18 | 908 |
// Don't silence errors when in debug mode, unless running unit tests. |
909 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG |
|
910 |
&& ! defined( 'WP_RUN_CORE_TESTS' ) |
|
911 |
) { |
|
912 |
$exif = exif_read_data( $file ); |
|
913 |
} else { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
914 |
// Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480 |
18 | 915 |
$exif = @exif_read_data( $file ); |
916 |
} |
|
0 | 917 |
|
19 | 918 |
if ( ! is_array( $exif ) ) { |
919 |
$exif = array(); |
|
920 |
} |
|
921 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
922 |
$exif_description = ''; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
923 |
$exif_usercomment = ''; |
5 | 924 |
if ( ! empty( $exif['ImageDescription'] ) ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
925 |
$exif_description = trim( $exif['ImageDescription'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
926 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
927 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
928 |
if ( ! empty( $exif['COMPUTED']['UserComment'] ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
929 |
$exif_usercomment = trim( $exif['COMPUTED']['UserComment'] ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
930 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
931 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
932 |
if ( $exif_description ) { |
5 | 933 |
mbstring_binary_safe_encoding(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
934 |
$description_length = strlen( $exif_description ); |
5 | 935 |
reset_mbstring_encoding(); |
936 |
if ( empty( $meta['title'] ) && $description_length < 80 ) { |
|
16 | 937 |
// Assume the title is stored in ImageDescription. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
938 |
$meta['title'] = $exif_description; |
5 | 939 |
} |
940 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
941 |
// If both user comments and description are present. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
942 |
if ( empty( $meta['caption'] ) && $exif_description && $exif_usercomment ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
943 |
if ( ! empty( $meta['title'] ) && $exif_description === $meta['title'] ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
944 |
$caption = $exif_usercomment; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
945 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
946 |
if ( $exif_description === $exif_usercomment ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
947 |
$caption = $exif_description; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
948 |
} else { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
949 |
$caption = trim( $exif_description . ' ' . $exif_usercomment ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
950 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
951 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
952 |
$meta['caption'] = $caption; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
953 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
954 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
955 |
if ( empty( $meta['caption'] ) && $exif_usercomment ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
956 |
$meta['caption'] = $exif_usercomment; |
5 | 957 |
} |
958 |
||
959 |
if ( empty( $meta['caption'] ) ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
960 |
$meta['caption'] = $exif_description; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
961 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
962 |
} elseif ( empty( $meta['caption'] ) && $exif_usercomment ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
963 |
$meta['caption'] = $exif_usercomment; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
964 |
$description_length = strlen( $exif_usercomment ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
965 |
if ( empty( $meta['title'] ) && $description_length < 80 ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
966 |
$meta['title'] = trim( $exif_usercomment ); |
0 | 967 |
} |
5 | 968 |
} elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { |
0 | 969 |
$meta['caption'] = trim( $exif['Comments'] ); |
970 |
} |
|
971 |
||
5 | 972 |
if ( empty( $meta['credit'] ) ) { |
973 |
if ( ! empty( $exif['Artist'] ) ) { |
|
974 |
$meta['credit'] = trim( $exif['Artist'] ); |
|
9 | 975 |
} elseif ( ! empty( $exif['Author'] ) ) { |
5 | 976 |
$meta['credit'] = trim( $exif['Author'] ); |
977 |
} |
|
978 |
} |
|
0 | 979 |
|
5 | 980 |
if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) { |
0 | 981 |
$meta['copyright'] = trim( $exif['Copyright'] ); |
5 | 982 |
} |
19 | 983 |
if ( ! empty( $exif['FNumber'] ) && is_scalar( $exif['FNumber'] ) ) { |
0 | 984 |
$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
5 | 985 |
} |
986 |
if ( ! empty( $exif['Model'] ) ) { |
|
0 | 987 |
$meta['camera'] = trim( $exif['Model'] ); |
5 | 988 |
} |
989 |
if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) { |
|
990 |
$meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] ); |
|
991 |
} |
|
992 |
if ( ! empty( $exif['FocalLength'] ) ) { |
|
19 | 993 |
$meta['focal_length'] = (string) $exif['FocalLength']; |
994 |
if ( is_scalar( $exif['FocalLength'] ) ) { |
|
995 |
$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); |
|
996 |
} |
|
5 | 997 |
} |
998 |
if ( ! empty( $exif['ISOSpeedRatings'] ) ) { |
|
0 | 999 |
$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; |
1000 |
$meta['iso'] = trim( $meta['iso'] ); |
|
1001 |
} |
|
5 | 1002 |
if ( ! empty( $exif['ExposureTime'] ) ) { |
19 | 1003 |
$meta['shutter_speed'] = (string) $exif['ExposureTime']; |
1004 |
if ( is_scalar( $exif['ExposureTime'] ) ) { |
|
1005 |
$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); |
|
1006 |
} |
|
5 | 1007 |
} |
1008 |
if ( ! empty( $exif['Orientation'] ) ) { |
|
1009 |
$meta['orientation'] = $exif['Orientation']; |
|
1010 |
} |
|
0 | 1011 |
} |
1012 |
||
1013 |
foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { |
|
5 | 1014 |
if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) { |
0 | 1015 |
$meta[ $key ] = utf8_encode( $meta[ $key ] ); |
5 | 1016 |
} |
0 | 1017 |
} |
1018 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1019 |
foreach ( $meta['keywords'] as $key => $keyword ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1020 |
if ( ! seems_utf8( $keyword ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1021 |
$meta['keywords'][ $key ] = utf8_encode( $keyword ); |
5 | 1022 |
} |
1023 |
} |
|
1024 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1025 |
$meta = wp_kses_post_deep( $meta ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1026 |
|
5 | 1027 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1028 |
* Filters the array of meta data read from an image's exif data. |
5 | 1029 |
* |
1030 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1031 |
* @since 4.4.0 The `$iptc` parameter was added. |
9 | 1032 |
* @since 5.0.0 The `$exif` parameter was added. |
5 | 1033 |
* |
9 | 1034 |
* @param array $meta Image meta data. |
1035 |
* @param string $file Path to image file. |
|
1036 |
* @param int $image_type Type of image, one of the `IMAGETYPE_XXX` constants. |
|
1037 |
* @param array $iptc IPTC data. |
|
1038 |
* @param array $exif EXIF data. |
|
5 | 1039 |
*/ |
9 | 1040 |
return apply_filters( 'wp_read_image_metadata', $meta, $file, $image_type, $iptc, $exif ); |
0 | 1041 |
} |
1042 |
||
1043 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1044 |
* Validates that file is an image. |
0 | 1045 |
* |
1046 |
* @since 2.5.0 |
|
1047 |
* |
|
1048 |
* @param string $path File path to test if valid image. |
|
1049 |
* @return bool True if valid image, false if not valid image. |
|
1050 |
*/ |
|
9 | 1051 |
function file_is_valid_image( $path ) { |
18 | 1052 |
$size = wp_getimagesize( $path ); |
9 | 1053 |
return ! empty( $size ); |
0 | 1054 |
} |
1055 |
||
1056 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1057 |
* Validates that file is suitable for displaying within a web page. |
0 | 1058 |
* |
1059 |
* @since 2.5.0 |
|
1060 |
* |
|
1061 |
* @param string $path File path to test. |
|
1062 |
* @return bool True if suitable, false if not suitable. |
|
1063 |
*/ |
|
9 | 1064 |
function file_is_displayable_image( $path ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1065 |
$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO, IMAGETYPE_WEBP, IMAGETYPE_AVIF ); |
9 | 1066 |
|
18 | 1067 |
$info = wp_getimagesize( $path ); |
5 | 1068 |
if ( empty( $info ) ) { |
0 | 1069 |
$result = false; |
16 | 1070 |
} elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) { |
0 | 1071 |
$result = false; |
5 | 1072 |
} else { |
0 | 1073 |
$result = true; |
5 | 1074 |
} |
0 | 1075 |
|
5 | 1076 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1077 |
* Filters whether the current image is displayable in the browser. |
5 | 1078 |
* |
1079 |
* @since 2.5.0 |
|
1080 |
* |
|
1081 |
* @param bool $result Whether the image can be displayed. Default true. |
|
1082 |
* @param string $path Path to the image. |
|
1083 |
*/ |
|
1084 |
return apply_filters( 'file_is_displayable_image', $result, $path ); |
|
0 | 1085 |
} |
1086 |
||
1087 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1088 |
* Loads an image resource for editing. |
0 | 1089 |
* |
1090 |
* @since 2.9.0 |
|
1091 |
* |
|
18 | 1092 |
* @param int $attachment_id Attachment ID. |
1093 |
* @param string $mime_type Image mime type. |
|
1094 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
1095 |
* of width and height values in pixels (in that order). Default 'full'. |
|
1096 |
* @return resource|GdImage|false The resulting image resource or GdImage instance on success, |
|
1097 |
* false on failure. |
|
0 | 1098 |
*/ |
1099 |
function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { |
|
1100 |
$filepath = _load_image_to_edit_path( $attachment_id, $size ); |
|
9 | 1101 |
if ( empty( $filepath ) ) { |
0 | 1102 |
return false; |
9 | 1103 |
} |
0 | 1104 |
|
1105 |
switch ( $mime_type ) { |
|
1106 |
case 'image/jpeg': |
|
9 | 1107 |
$image = imagecreatefromjpeg( $filepath ); |
0 | 1108 |
break; |
1109 |
case 'image/png': |
|
9 | 1110 |
$image = imagecreatefrompng( $filepath ); |
0 | 1111 |
break; |
1112 |
case 'image/gif': |
|
9 | 1113 |
$image = imagecreatefromgif( $filepath ); |
0 | 1114 |
break; |
18 | 1115 |
case 'image/webp': |
1116 |
$image = false; |
|
1117 |
if ( function_exists( 'imagecreatefromwebp' ) ) { |
|
1118 |
$image = imagecreatefromwebp( $filepath ); |
|
1119 |
} |
|
1120 |
break; |
|
0 | 1121 |
default: |
1122 |
$image = false; |
|
1123 |
break; |
|
1124 |
} |
|
18 | 1125 |
|
1126 |
if ( is_gd_image( $image ) ) { |
|
5 | 1127 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1128 |
* Filters the current image being loaded for editing. |
5 | 1129 |
* |
1130 |
* @since 2.9.0 |
|
1131 |
* |
|
18 | 1132 |
* @param resource|GdImage $image Current image. |
1133 |
* @param int $attachment_id Attachment ID. |
|
1134 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
1135 |
* an array of width and height values in pixels (in that order). |
|
5 | 1136 |
*/ |
1137 |
$image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); |
|
18 | 1138 |
|
9 | 1139 |
if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
1140 |
imagealphablending( $image, false ); |
|
1141 |
imagesavealpha( $image, true ); |
|
0 | 1142 |
} |
1143 |
} |
|
18 | 1144 |
|
0 | 1145 |
return $image; |
1146 |
} |
|
1147 |
||
1148 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1149 |
* Retrieves the path or URL of an attachment's attached file. |
0 | 1150 |
* |
1151 |
* If the attached file is not present on the local filesystem (usually due to replication plugins), |
|
18 | 1152 |
* then the URL of the file is returned if `allow_url_fopen` is supported. |
0 | 1153 |
* |
1154 |
* @since 3.4.0 |
|
1155 |
* @access private |
|
1156 |
* |
|
18 | 1157 |
* @param int $attachment_id Attachment ID. |
1158 |
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array |
|
1159 |
* of width and height values in pixels (in that order). Default 'full'. |
|
1160 |
* @return string|false File path or URL on success, false on failure. |
|
0 | 1161 |
*/ |
1162 |
function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { |
|
1163 |
$filepath = get_attached_file( $attachment_id ); |
|
1164 |
||
1165 |
if ( $filepath && file_exists( $filepath ) ) { |
|
16 | 1166 |
if ( 'full' !== $size ) { |
1167 |
$data = image_get_intermediate_size( $attachment_id, $size ); |
|
1168 |
||
1169 |
if ( $data ) { |
|
1170 |
$filepath = path_join( dirname( $filepath ), $data['file'] ); |
|
1171 |
||
1172 |
/** |
|
18 | 1173 |
* Filters the path to an attachment's file when editing the image. |
16 | 1174 |
* |
1175 |
* The filter is evaluated for all image sizes except 'full'. |
|
1176 |
* |
|
1177 |
* @since 3.1.0 |
|
1178 |
* |
|
18 | 1179 |
* @param string $path Path to the current image. |
1180 |
* @param int $attachment_id Attachment ID. |
|
1181 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
1182 |
* an array of width and height values in pixels (in that order). |
|
16 | 1183 |
*/ |
1184 |
$filepath = apply_filters( 'load_image_to_edit_filesystempath', $filepath, $attachment_id, $size ); |
|
1185 |
} |
|
0 | 1186 |
} |
16 | 1187 |
} elseif ( function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) ) { |
5 | 1188 |
/** |
18 | 1189 |
* Filters the path to an attachment's URL when editing the image. |
5 | 1190 |
* |
18 | 1191 |
* The filter is only evaluated if the file isn't stored locally and `allow_url_fopen` is enabled on the server. |
5 | 1192 |
* |
1193 |
* @since 3.1.0 |
|
1194 |
* |
|
18 | 1195 |
* @param string|false $image_url Current image URL. |
1196 |
* @param int $attachment_id Attachment ID. |
|
1197 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
1198 |
* an array of width and height values in pixels (in that order). |
|
5 | 1199 |
*/ |
0 | 1200 |
$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); |
1201 |
} |
|
1202 |
||
5 | 1203 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1204 |
* Filters the returned path or URL of the current image. |
5 | 1205 |
* |
1206 |
* @since 2.9.0 |
|
1207 |
* |
|
18 | 1208 |
* @param string|false $filepath File path or URL to current image, or false. |
1209 |
* @param int $attachment_id Attachment ID. |
|
1210 |
* @param string|int[] $size Requested image size. Can be any registered image size name, or |
|
1211 |
* an array of width and height values in pixels (in that order). |
|
5 | 1212 |
*/ |
0 | 1213 |
return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); |
1214 |
} |
|
1215 |
||
1216 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1217 |
* Copies an existing image file. |
0 | 1218 |
* |
1219 |
* @since 3.4.0 |
|
1220 |
* @access private |
|
1221 |
* |
|
18 | 1222 |
* @param int $attachment_id Attachment ID. |
0 | 1223 |
* @return string|false New file path on success, false on failure. |
1224 |
*/ |
|
1225 |
function _copy_image_file( $attachment_id ) { |
|
16 | 1226 |
$dst_file = get_attached_file( $attachment_id ); |
1227 |
$src_file = $dst_file; |
|
1228 |
||
9 | 1229 |
if ( ! file_exists( $src_file ) ) { |
0 | 1230 |
$src_file = _load_image_to_edit_path( $attachment_id ); |
9 | 1231 |
} |
0 | 1232 |
|
1233 |
if ( $src_file ) { |
|
9 | 1234 |
$dst_file = str_replace( wp_basename( $dst_file ), 'copy-' . wp_basename( $dst_file ), $dst_file ); |
1235 |
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) ); |
|
0 | 1236 |
|
5 | 1237 |
/* |
1238 |
* The directory containing the original file may no longer |
|
1239 |
* exist when using a replication plugin. |
|
1240 |
*/ |
|
0 | 1241 |
wp_mkdir_p( dirname( $dst_file ) ); |
1242 |
||
16 | 1243 |
if ( ! copy( $src_file, $dst_file ) ) { |
0 | 1244 |
$dst_file = false; |
9 | 1245 |
} |
0 | 1246 |
} else { |
1247 |
$dst_file = false; |
|
1248 |
} |
|
1249 |
||
1250 |
return $dst_file; |
|
1251 |
} |