259 |
259 |
260 if ( $exif_meta ) { |
260 if ( $exif_meta ) { |
261 $image_meta['image_meta'] = $exif_meta; |
261 $image_meta['image_meta'] = $exif_meta; |
262 } |
262 } |
263 |
263 |
264 // Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736. |
264 /** |
265 if ( 'image/png' !== $imagesize['mime'] ) { |
265 * Filters the "BIG image" threshold value. |
266 |
266 * |
267 /** |
267 * If the original image width or height is above the threshold, it will be scaled down. The threshold is |
268 * Filters the "BIG image" threshold value. |
268 * used as max width and max height. The scaled down image will be used as the largest available size, including |
269 * |
269 * the `_wp_attached_file` post meta value. |
270 * If the original image width or height is above the threshold, it will be scaled down. The threshold is |
270 * |
271 * used as max width and max height. The scaled down image will be used as the largest available size, including |
271 * Returning `false` from the filter callback will disable the scaling. |
272 * the `_wp_attached_file` post meta value. |
272 * |
273 * |
273 * @since 5.3.0 |
274 * Returning `false` from the filter callback will disable the scaling. |
274 * |
275 * |
275 * @param int $threshold The threshold value in pixels. Default 2560. |
276 * @since 5.3.0 |
276 * @param array $imagesize { |
277 * |
277 * Indexed array of the image width and height in pixels. |
278 * @param int $threshold The threshold value in pixels. Default 2560. |
278 * |
279 * @param array $imagesize { |
279 * @type int $0 The image width. |
280 * Indexed array of the image width and height in pixels. |
280 * @type int $1 The image height. |
281 * |
281 * } |
282 * @type int $0 The image width. |
282 * @param string $file Full path to the uploaded image file. |
283 * @type int $1 The image height. |
283 * @param int $attachment_id Attachment post ID. |
284 * } |
284 */ |
285 * @param string $file Full path to the uploaded image file. |
285 $threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id ); |
286 * @param int $attachment_id Attachment post ID. |
286 |
287 */ |
287 /* |
288 $threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id ); |
288 * If the original image's dimensions are over the threshold, |
289 |
289 * scale the image and use it as the "full" size. |
290 /* |
290 */ |
291 * If the original image's dimensions are over the threshold, |
291 $scale_down = false; |
292 * scale the image and use it as the "full" size. |
292 $convert = false; |
293 */ |
293 |
294 if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) { |
294 if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) { |
295 $editor = wp_get_image_editor( $file ); |
295 // The image will be converted if needed on saving. |
296 |
296 $scale_down = true; |
297 if ( is_wp_error( $editor ) ) { |
297 } else { |
298 // This image cannot be edited. |
298 // The image may need to be converted regardless of its dimensions. |
299 return $image_meta; |
299 $output_format = wp_get_image_editor_output_format( $file, $imagesize['mime'] ); |
300 } |
300 |
301 |
301 if ( |
302 // Resize the image. |
302 is_array( $output_format ) && |
|
303 array_key_exists( $imagesize['mime'], $output_format ) && |
|
304 $output_format[ $imagesize['mime'] ] !== $imagesize['mime'] |
|
305 ) { |
|
306 $convert = true; |
|
307 } |
|
308 } |
|
309 |
|
310 if ( $scale_down || $convert ) { |
|
311 $editor = wp_get_image_editor( $file ); |
|
312 |
|
313 if ( is_wp_error( $editor ) ) { |
|
314 // This image cannot be edited. |
|
315 return $image_meta; |
|
316 } |
|
317 |
|
318 if ( $scale_down ) { |
|
319 // Resize the image. This will also convet it if needed. |
303 $resized = $editor->resize( $threshold, $threshold ); |
320 $resized = $editor->resize( $threshold, $threshold ); |
304 $rotated = null; |
321 } elseif ( $convert ) { |
305 |
322 // The image will be converted (if possible) when saved. |
306 // If there is EXIF data, rotate according to EXIF Orientation. |
323 $resized = true; |
307 if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) { |
324 } |
308 $resized = $editor->maybe_exif_rotate(); |
325 |
309 $rotated = $resized; |
326 $rotated = null; |
310 } |
327 |
311 |
328 // If there is EXIF data, rotate according to EXIF Orientation. |
312 if ( ! is_wp_error( $resized ) ) { |
329 if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) { |
313 /* |
330 $resized = $editor->maybe_exif_rotate(); |
314 * Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg". |
331 $rotated = $resized; // bool true or WP_Error |
315 * This doesn't affect the sub-sizes names as they are generated from the original image (for best quality). |
332 } |
316 */ |
333 |
|
334 if ( ! is_wp_error( $resized ) ) { |
|
335 /* |
|
336 * Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg". |
|
337 * This doesn't affect the sub-sizes names as they are generated from the original image (for best quality). |
|
338 */ |
|
339 if ( $scale_down ) { |
317 $saved = $editor->save( $editor->generate_filename( 'scaled' ) ); |
340 $saved = $editor->save( $editor->generate_filename( 'scaled' ) ); |
318 |
341 } elseif ( $convert ) { |
319 if ( ! is_wp_error( $saved ) ) { |
342 // Pass an empty string to avoid adding a suffix to converted file names. |
320 $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); |
343 $saved = $editor->save( $editor->generate_filename( '' ) ); |
321 |
344 } else { |
322 // If the image was rotated update the stored EXIF data. |
345 $saved = $editor->save(); |
323 if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) { |
346 } |
324 $image_meta['image_meta']['orientation'] = 1; |
347 |
325 } |
348 if ( ! is_wp_error( $saved ) ) { |
326 } else { |
349 $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); |
327 // TODO: Log errors. |
350 |
|
351 // If the image was rotated update the stored EXIF data. |
|
352 if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) { |
|
353 $image_meta['image_meta']['orientation'] = 1; |
328 } |
354 } |
329 } else { |
355 } else { |
330 // TODO: Log errors. |
356 // TODO: Log errors. |
331 } |
357 } |
332 } elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) { |
358 } else { |
333 // Rotate the whole original image if there is EXIF data and "orientation" is not 1. |
359 // TODO: Log errors. |
334 |
360 } |
335 $editor = wp_get_image_editor( $file ); |
361 } elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) { |
336 |
362 // Rotate the whole original image if there is EXIF data and "orientation" is not 1. |
337 if ( is_wp_error( $editor ) ) { |
363 $editor = wp_get_image_editor( $file ); |
338 // This image cannot be edited. |
364 |
339 return $image_meta; |
365 if ( is_wp_error( $editor ) ) { |
340 } |
366 // This image cannot be edited. |
341 |
367 return $image_meta; |
342 // Rotate the image. |
368 } |
343 $rotated = $editor->maybe_exif_rotate(); |
369 |
344 |
370 // Rotate the image. |
345 if ( true === $rotated ) { |
371 $rotated = $editor->maybe_exif_rotate(); |
346 // Append `-rotated` to the image file name. |
372 |
347 $saved = $editor->save( $editor->generate_filename( 'rotated' ) ); |
373 if ( true === $rotated ) { |
348 |
374 // Append `-rotated` to the image file name. |
349 if ( ! is_wp_error( $saved ) ) { |
375 $saved = $editor->save( $editor->generate_filename( 'rotated' ) ); |
350 $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); |
376 |
351 |
377 if ( ! is_wp_error( $saved ) ) { |
352 // Update the stored EXIF data. |
378 $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); |
353 if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { |
379 |
354 $image_meta['image_meta']['orientation'] = 1; |
380 // Update the stored EXIF data. |
355 } |
381 if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { |
356 } else { |
382 $image_meta['image_meta']['orientation'] = 1; |
357 // TODO: Log errors. |
|
358 } |
383 } |
|
384 } else { |
|
385 // TODO: Log errors. |
359 } |
386 } |
360 } |
387 } |
361 } |
388 } |
362 |
389 |
363 /* |
390 /* |