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