2007-05-25 09:16:21 +02:00
|
|
|
<?php
|
2007-11-03 19:33:19 +01:00
|
|
|
/**
|
|
|
|
* File contains all the administration image manipulation functions.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
2008-09-17 02:40:10 +02:00
|
|
|
* @subpackage Administration
|
2007-11-03 19:33:19 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-07-23 02:52:05 +02:00
|
|
|
* Crops an image to a given size.
|
2007-11-03 19:33:19 +01:00
|
|
|
*
|
2008-10-10 20:21:16 +02:00
|
|
|
* @since 2.1.0
|
2007-11-03 19:33:19 +01:00
|
|
|
*
|
2021-01-05 18:16:11 +01:00
|
|
|
* @param string|int $src The source file or Attachment ID.
|
|
|
|
* @param int $src_x The start x position to crop from.
|
|
|
|
* @param int $src_y The start y position to crop from.
|
|
|
|
* @param int $src_w The width to crop.
|
|
|
|
* @param int $src_h The height to crop.
|
|
|
|
* @param int $dst_w The destination width.
|
|
|
|
* @param int $dst_h The destination height.
|
|
|
|
* @param bool|false $src_abs Optional. If the source crop points are absolute.
|
|
|
|
* @param string|false $dst_file Optional. The destination file to write to.
|
2013-02-02 03:01:59 +01:00
|
|
|
* @return string|WP_Error New filepath on success, WP_Error on failure.
|
2007-11-03 19:33:19 +01:00
|
|
|
*/
|
2012-11-10 21:42:27 +01:00
|
|
|
function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
|
|
|
|
$src_file = $src;
|
2020-01-18 01:54:04 +01:00
|
|
|
if ( is_numeric( $src ) ) { // Handle int as attachment ID.
|
2012-11-10 21:42:27 +01:00
|
|
|
$src_file = get_attached_file( $src );
|
|
|
|
|
2012-04-06 22:47:24 +02:00
|
|
|
if ( ! file_exists( $src_file ) ) {
|
2023-07-09 21:52:24 +02:00
|
|
|
/*
|
|
|
|
* If the file doesn't exist, attempt a URL fopen on the src link.
|
|
|
|
* This can occur with certain file replication plugins.
|
|
|
|
*/
|
2012-11-13 01:03:55 +01:00
|
|
|
$src = _load_image_to_edit_path( $src, 'full' );
|
|
|
|
} else {
|
|
|
|
$src = $src_file;
|
2012-04-06 22:47:24 +02:00
|
|
|
}
|
|
|
|
}
|
2007-05-25 09:16:21 +02:00
|
|
|
|
2012-11-22 10:52:16 +01:00
|
|
|
$editor = wp_get_image_editor( $src );
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( is_wp_error( $editor ) ) {
|
2012-10-11 20:59:41 +02:00
|
|
|
return $editor;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2007-05-25 09:16:21 +02:00
|
|
|
|
2012-10-11 20:59:41 +02:00
|
|
|
$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( is_wp_error( $src ) ) {
|
2012-10-01 22:59:06 +02:00
|
|
|
return $src;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2007-05-25 09:16:21 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $dst_file ) {
|
2019-03-01 21:58:52 +01:00
|
|
|
$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2007-05-25 09:16:21 +02:00
|
|
|
|
2014-07-17 11:14:16 +02:00
|
|
|
/*
|
|
|
|
* The directory containing the original file may no longer exist when
|
|
|
|
* using a replication plugin.
|
|
|
|
*/
|
2012-04-06 22:47:24 +02:00
|
|
|
wp_mkdir_p( dirname( $dst_file ) );
|
|
|
|
|
2019-03-01 21:58:52 +01:00
|
|
|
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) );
|
2012-05-14 19:53:03 +02:00
|
|
|
|
2012-10-01 22:59:06 +02:00
|
|
|
$result = $editor->save( $dst_file );
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( is_wp_error( $result ) ) {
|
2013-02-02 03:01:59 +01:00
|
|
|
return $result;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2013-02-02 03:01:59 +01:00
|
|
|
|
2022-04-27 07:21:08 +02:00
|
|
|
if ( ! empty( $result['path'] ) ) {
|
|
|
|
return $result['path'];
|
|
|
|
}
|
|
|
|
|
2012-10-01 22:59:06 +02:00
|
|
|
return $dst_file;
|
2007-05-25 09:16:21 +02:00
|
|
|
}
|
|
|
|
|
2007-11-03 19:33:19 +01:00
|
|
|
/**
|
2022-09-06 23:13:12 +02:00
|
|
|
* Compare the existing image sub-sizes (as saved in the attachment meta)
|
|
|
|
* to the currently registered image sub-sizes, and return the difference.
|
2007-11-03 19:33:19 +01:00
|
|
|
*
|
2019-06-15 03:02:52 +02:00
|
|
|
* Registered sub-sizes that are larger than the image are skipped.
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
2019-06-15 03:02:52 +02:00
|
|
|
* @since 5.3.0
|
|
|
|
*
|
2022-09-06 23:13:12 +02:00
|
|
|
* @param int $attachment_id The image attachment post ID.
|
2021-12-07 13:20:02 +01:00
|
|
|
* @return array[] Associative array of arrays of image sub-size information for
|
|
|
|
* missing image sizes, keyed by image size name.
|
2007-11-03 19:33:19 +01:00
|
|
|
*/
|
2022-09-06 23:13:12 +02:00
|
|
|
function wp_get_missing_image_subsizes( $attachment_id ) {
|
2019-06-15 03:02:52 +02:00
|
|
|
if ( ! wp_attachment_is_image( $attachment_id ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
2007-05-25 09:16:21 +02:00
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
$registered_sizes = wp_get_registered_image_subsizes();
|
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id );
|
Media: Add support for rendering PDF thumbnails.
When support for PDFs is available, on upload,
render 'Thumbnail', 'Medium', 'Large', and 'Full' sizes of
the first page, and save them in attachment meta.
Use these renders within Add Media, Media Gallery and List views,
Attachment Details, Post/Attachment Edit screens, and Attachment pages.
Support available by default via Imagick -> ImageMagick -> Ghostscript,
but can be provided by any `WP_Image_Editor` that supports PDFs.
Props adamsilverstein, azaozz, celloexpressions, desrosj, dglingren, ericlewis, ipstenu, joemcgill, joyously, markoheijnen, melchoyce, mikeschroder, tomauger.
Fixes #31050.
Built from https://develop.svn.wordpress.org/trunk@38949
git-svn-id: http://core.svn.wordpress.org/trunk@38892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-26 09:28:32 +02:00
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
// Meta error?
|
|
|
|
if ( empty( $image_meta ) ) {
|
2019-06-15 03:32:53 +02:00
|
|
|
return $registered_sizes;
|
2019-06-15 03:02:52 +02:00
|
|
|
}
|
|
|
|
|
2019-11-07 19:51:02 +01:00
|
|
|
// Use the originally uploaded image dimensions as full_width and full_height.
|
|
|
|
if ( ! empty( $image_meta['original_image'] ) ) {
|
|
|
|
$image_file = wp_get_original_image_path( $attachment_id );
|
2021-02-02 17:53:04 +01:00
|
|
|
$imagesize = wp_getimagesize( $image_file );
|
2019-11-07 19:51:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $imagesize ) ) {
|
|
|
|
$full_width = $imagesize[0];
|
|
|
|
$full_height = $imagesize[1];
|
|
|
|
} else {
|
|
|
|
$full_width = (int) $image_meta['width'];
|
|
|
|
$full_height = (int) $image_meta['height'];
|
|
|
|
}
|
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
$possible_sizes = array();
|
|
|
|
|
|
|
|
// Skip registered sizes that are too large for the uploaded image.
|
|
|
|
foreach ( $registered_sizes as $size_name => $size_data ) {
|
|
|
|
if ( image_resize_dimensions( $full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'] ) ) {
|
|
|
|
$possible_sizes[ $size_name ] = $size_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $image_meta['sizes'] ) ) {
|
|
|
|
$image_meta['sizes'] = array();
|
|
|
|
}
|
|
|
|
|
2020-01-18 01:54:04 +01:00
|
|
|
/*
|
|
|
|
* Remove sizes that already exist. Only checks for matching "size names".
|
|
|
|
* It is possible that the dimensions for a particular size name have changed.
|
|
|
|
* For example the user has changed the values on the Settings -> Media screen.
|
|
|
|
* However we keep the old sub-sizes with the previous dimensions
|
|
|
|
* as the image may have been used in an older post.
|
|
|
|
*/
|
2022-09-06 23:13:12 +02:00
|
|
|
$missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] );
|
2019-06-15 03:02:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the array of missing image sub-sizes for an uploaded image.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
2021-12-07 13:20:02 +01:00
|
|
|
* @param array[] $missing_sizes Associative array of arrays of image sub-size information for
|
|
|
|
* missing image sizes, keyed by image size name.
|
|
|
|
* @param array $image_meta The image meta data.
|
|
|
|
* @param int $attachment_id The image attachment post ID.
|
2019-06-15 03:02:52 +02:00
|
|
|
*/
|
2022-09-06 23:13:12 +02:00
|
|
|
return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id );
|
2019-06-15 03:02:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If any of the currently registered image sub-sizes are missing,
|
|
|
|
* create them and update the image meta data.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param int $attachment_id The image attachment post ID.
|
2019-09-04 03:11:54 +02:00
|
|
|
* @return array|WP_Error The updated image meta data array or WP_Error object
|
|
|
|
* if both the image meta and the attached file are missing.
|
2019-06-15 03:02:52 +02:00
|
|
|
*/
|
|
|
|
function wp_update_image_subsizes( $attachment_id ) {
|
2019-09-04 03:11:54 +02:00
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id );
|
2019-09-07 03:07:55 +02:00
|
|
|
$image_file = wp_get_original_image_path( $attachment_id );
|
2019-09-04 03:11:54 +02:00
|
|
|
|
|
|
|
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
|
2023-07-09 21:52:24 +02:00
|
|
|
/*
|
|
|
|
* Previously failed upload?
|
|
|
|
* If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
|
|
|
|
*/
|
2019-09-04 03:11:54 +02:00
|
|
|
if ( ! empty( $image_file ) ) {
|
2019-10-30 22:10:04 +01:00
|
|
|
$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
|
2019-09-04 03:11:54 +02:00
|
|
|
} else {
|
|
|
|
return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
|
|
|
|
}
|
2019-10-30 22:10:04 +01:00
|
|
|
} else {
|
2022-09-06 23:13:12 +02:00
|
|
|
$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
|
2019-09-04 03:11:54 +02:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
if ( empty( $missing_sizes ) ) {
|
|
|
|
return $image_meta;
|
2019-10-30 22:10:04 +01:00
|
|
|
}
|
2019-06-15 03:02:52 +02:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
// This also updates the image meta.
|
|
|
|
$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
|
2019-06-15 03:02:52 +02:00
|
|
|
}
|
|
|
|
|
2019-10-30 22:10:04 +01:00
|
|
|
/** This filter is documented in wp-admin/includes/image.php */
|
|
|
|
$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );
|
|
|
|
|
|
|
|
// Save the updated metadata.
|
|
|
|
wp_update_attachment_metadata( $attachment_id, $image_meta );
|
|
|
|
|
|
|
|
return $image_meta;
|
2019-06-15 03:02:52 +02:00
|
|
|
}
|
|
|
|
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
/**
|
|
|
|
* Updates the attached file and image meta data when the original image was edited.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
2022-06-21 13:18:17 +02:00
|
|
|
* @since 6.0.0 The `$filesize` value was added to the returned array.
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
* @access private
|
|
|
|
*
|
2019-10-01 03:19:57 +02:00
|
|
|
* @param array $saved_data The data returned from WP_Image_Editor after successfully saving an image.
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
* @param string $original_file Path to the original file.
|
|
|
|
* @param array $image_meta The image meta data.
|
|
|
|
* @param int $attachment_id The attachment post ID.
|
|
|
|
* @return array The updated image meta data.
|
|
|
|
*/
|
|
|
|
function _wp_image_meta_replace_original( $saved_data, $original_file, $image_meta, $attachment_id ) {
|
|
|
|
$new_file = $saved_data['path'];
|
|
|
|
|
|
|
|
// Update the attached file meta.
|
|
|
|
update_attached_file( $attachment_id, $new_file );
|
|
|
|
|
|
|
|
// Width and height of the new image.
|
|
|
|
$image_meta['width'] = $saved_data['width'];
|
|
|
|
$image_meta['height'] = $saved_data['height'];
|
|
|
|
|
|
|
|
// Make the file path relative to the upload dir.
|
|
|
|
$image_meta['file'] = _wp_relative_upload_path( $new_file );
|
|
|
|
|
Media: Store attachment’s file size in metadata.
Store the file size of all newly uploaded attachments, as part of the metadata stored in post meta. Storing file size means, developers will not have to resort to doing `filesize` function calls, that can be time consuming on assets on offloaded to services like Amazon’s S3.
This change also introduces a new helper function called, `wp_filesize`. This is a wrapper around the `filesize` php function, that adds some helpful filters and ensures the return value is an integer.
Props Cybr, Spacedmonkey, SergeyBiryukov, johnwatkins0, swissspidy, desrosj, joemcgill, azaozz, antpb, adamsilverstein, uday17035.
Fixes #49412.
Built from https://develop.svn.wordpress.org/trunk@52837
git-svn-id: http://core.svn.wordpress.org/trunk@52426 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-10 14:10:02 +01:00
|
|
|
// Add image file size.
|
|
|
|
$image_meta['filesize'] = wp_filesize( $new_file );
|
|
|
|
|
2022-06-21 13:18:17 +02:00
|
|
|
// Store the original image file name in image_meta.
|
|
|
|
$image_meta['original_image'] = wp_basename( $original_file );
|
|
|
|
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
return $image_meta;
|
|
|
|
}
|
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
/**
|
2022-09-06 23:13:12 +02:00
|
|
|
* Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
|
2019-06-15 03:02:52 +02:00
|
|
|
*
|
|
|
|
* Intended for use after an image is uploaded. Saves/updates the image metadata after each
|
|
|
|
* sub-size is created. If there was an error, it is added to the returned image metadata array.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param string $file Full path to the image file.
|
2021-12-12 15:54:00 +01:00
|
|
|
* @param int $attachment_id Attachment ID to process.
|
2019-09-07 03:07:55 +02:00
|
|
|
* @return array The image attachment meta data.
|
2019-06-15 03:02:52 +02:00
|
|
|
*/
|
2019-09-07 03:07:55 +02:00
|
|
|
function wp_create_image_subsizes( $file, $attachment_id ) {
|
2021-02-02 17:53:04 +01:00
|
|
|
$imagesize = wp_getimagesize( $file );
|
2019-09-07 03:07:55 +02:00
|
|
|
|
|
|
|
if ( empty( $imagesize ) ) {
|
|
|
|
// File is not an image.
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2020-01-18 01:54:04 +01:00
|
|
|
// Default image meta.
|
2019-09-07 03:07:55 +02:00
|
|
|
$image_meta = array(
|
Media: Store attachment’s file size in metadata.
Store the file size of all newly uploaded attachments, as part of the metadata stored in post meta. Storing file size means, developers will not have to resort to doing `filesize` function calls, that can be time consuming on assets on offloaded to services like Amazon’s S3.
This change also introduces a new helper function called, `wp_filesize`. This is a wrapper around the `filesize` php function, that adds some helpful filters and ensures the return value is an integer.
Props Cybr, Spacedmonkey, SergeyBiryukov, johnwatkins0, swissspidy, desrosj, joemcgill, azaozz, antpb, adamsilverstein, uday17035.
Fixes #49412.
Built from https://develop.svn.wordpress.org/trunk@52837
git-svn-id: http://core.svn.wordpress.org/trunk@52426 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-10 14:10:02 +01:00
|
|
|
'width' => $imagesize[0],
|
|
|
|
'height' => $imagesize[1],
|
|
|
|
'file' => _wp_relative_upload_path( $file ),
|
|
|
|
'filesize' => wp_filesize( $file ),
|
|
|
|
'sizes' => array(),
|
2019-09-07 03:07:55 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Fetch additional metadata from EXIF/IPTC.
|
|
|
|
$exif_meta = wp_read_image_metadata( $file );
|
|
|
|
|
|
|
|
if ( $exif_meta ) {
|
|
|
|
$image_meta['image_meta'] = $exif_meta;
|
|
|
|
}
|
2008-12-09 19:03:31 +01:00
|
|
|
|
Media: enable generating multiple mime types for image uploads; specifically WebP versions for JPEG images by default.
This changeset adds the capability for core media uploads to generate sub sized images in more than a single mime type. The output formats for each mime type can be controlled through a filter. WebP is used as an additional output format for JPEG images by default to improve front end performance.
When generating additional mime types, only images which are smaller than the respective original are retained. By default, additional mime type images are only generated for the built-in core image sizes and any custom sizes that have opted in.
Image meta is updated with a new 'sources' array containing file details for each mime type. Each image size in the 'sizes' array also gets a new 'sources' array that contains the image file details for each mime type.
This change also increases image upload retries to accommodate additional image sizes. It also adds a `$mime_type` parameter to the `wp_get_missing_image_subsizes` function and filter.
This change adds three new filters to enable full control of secondary mime image generation and output:
* A new filter `wp_image_sizes_with_additional_mime_type_support` that filters the sizes that support secondary mime type output. Developers can use this to control the output of additional mime type sub-sized images on a per size basis.
* A new filter `wp_upload_image_mime_transforms` that filters the output mime types for a given input mime type. Developers can use this to control generation of additional mime types for a given input mime type or even override the original mime type.
* A new filter `wp_content_image_mimes` which controls image mime type output selection and order for frontend content. Developers can use this to control the mime type output preference order for content images. Content images inserted from the media library will use the available image versions based on the order from this filter.
Thanks to the many contributors who helped develop, test and give feedback on this feature.
A haiku to summarize:
Upload a JPEG
Images of all sizes
Output as WebPs
Props flixos90, MatthiasReinholz, studiolxv, markhowellsmead, eatingrules, pbiron, mukesh27, joegrainger, mehulkaklotar, tweetythierry, akshitsethi, peterwilsoncc, eugenemanuilov, mitogh, shetheliving, clarkeemily, codekraft, mikeschroder, clorith, kasparsd, spacedmonkey, trevorpfromsandee, jb510, scofennellgmailcom, seedsca, cagsmith, karinclimber, dainemawer, baxbridge, grapplerulrich, sobatkras, chynnabenton, tonylocalword, barneydavey, kwillmorth, garymatthews919, olliejones, imarkinteractive, jeffpaul, feastdesignco, webbeetle, masteradhoc.
See #55443.
Built from https://develop.svn.wordpress.org/trunk@53751
git-svn-id: http://core.svn.wordpress.org/trunk@53310 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-07-21 20:03:13 +02:00
|
|
|
// Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736.
|
2022-09-06 23:13:12 +02:00
|
|
|
if ( 'image/png' !== $imagesize['mime'] ) {
|
|
|
|
|
2019-12-01 19:26:05 +01:00
|
|
|
/**
|
|
|
|
* Filters the "BIG image" threshold value.
|
|
|
|
*
|
|
|
|
* If the original image width or height is above the threshold, it will be scaled down. The threshold is
|
|
|
|
* used as max width and max height. The scaled down image will be used as the largest available size, including
|
|
|
|
* the `_wp_attached_file` post meta value.
|
|
|
|
*
|
|
|
|
* Returning `false` from the filter callback will disable the scaling.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
|
|
|
* @param int $threshold The threshold value in pixels. Default 2560.
|
|
|
|
* @param array $imagesize {
|
|
|
|
* Indexed array of the image width and height in pixels.
|
|
|
|
*
|
|
|
|
* @type int $0 The image width.
|
|
|
|
* @type int $1 The image height.
|
|
|
|
* }
|
|
|
|
* @param string $file Full path to the uploaded image file.
|
|
|
|
* @param int $attachment_id Attachment post ID.
|
|
|
|
*/
|
|
|
|
$threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id );
|
|
|
|
|
2023-07-09 21:52:24 +02:00
|
|
|
/*
|
|
|
|
* If the original image's dimensions are over the threshold,
|
|
|
|
* scale the image and use it as the "full" size.
|
|
|
|
*/
|
2022-09-06 23:13:12 +02:00
|
|
|
if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
|
|
|
|
$editor = wp_get_image_editor( $file );
|
|
|
|
|
|
|
|
if ( is_wp_error( $editor ) ) {
|
|
|
|
// This image cannot be edited.
|
|
|
|
return $image_meta;
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Resize the image.
|
2019-12-01 19:26:05 +01:00
|
|
|
$resized = $editor->resize( $threshold, $threshold );
|
2022-09-06 23:13:12 +02:00
|
|
|
$rotated = null;
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
|
2019-12-01 19:26:05 +01:00
|
|
|
// If there is EXIF data, rotate according to EXIF Orientation.
|
|
|
|
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
|
2022-09-06 23:13:12 +02:00
|
|
|
$resized = $editor->maybe_exif_rotate();
|
|
|
|
$rotated = $resized;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $resized ) ) {
|
2023-07-09 21:52:24 +02:00
|
|
|
/*
|
|
|
|
* Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
|
|
|
|
* This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
|
|
|
|
*/
|
2022-09-06 23:13:12 +02:00
|
|
|
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $saved ) ) {
|
|
|
|
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
|
|
|
|
|
|
|
|
// If the image was rotated update the stored EXIF data.
|
|
|
|
if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) {
|
|
|
|
$image_meta['image_meta']['orientation'] = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: Log errors.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: Log errors.
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
}
|
2020-02-09 17:55:09 +01:00
|
|
|
} elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
|
2019-12-01 19:26:05 +01:00
|
|
|
// Rotate the whole original image if there is EXIF data and "orientation" is not 1.
|
2022-09-06 23:13:12 +02:00
|
|
|
|
|
|
|
$editor = wp_get_image_editor( $file );
|
|
|
|
|
|
|
|
if ( is_wp_error( $editor ) ) {
|
|
|
|
// This image cannot be edited.
|
|
|
|
return $image_meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate the image.
|
2019-12-01 19:26:05 +01:00
|
|
|
$rotated = $editor->maybe_exif_rotate();
|
2016-08-22 06:37:28 +02:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
if ( true === $rotated ) {
|
|
|
|
// Append `-rotated` to the image file name.
|
|
|
|
$saved = $editor->save( $editor->generate_filename( 'rotated' ) );
|
2019-11-04 18:07:03 +01:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
if ( ! is_wp_error( $saved ) ) {
|
|
|
|
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
|
2019-06-15 03:02:52 +02:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
// Update the stored EXIF data.
|
|
|
|
if ( ! empty( $image_meta['image_meta']['orientation'] ) ) {
|
|
|
|
$image_meta['image_meta']['orientation'] = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: Log errors.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Media: enable generating multiple mime types for image uploads; specifically WebP versions for JPEG images by default.
This changeset adds the capability for core media uploads to generate sub sized images in more than a single mime type. The output formats for each mime type can be controlled through a filter. WebP is used as an additional output format for JPEG images by default to improve front end performance.
When generating additional mime types, only images which are smaller than the respective original are retained. By default, additional mime type images are only generated for the built-in core image sizes and any custom sizes that have opted in.
Image meta is updated with a new 'sources' array containing file details for each mime type. Each image size in the 'sizes' array also gets a new 'sources' array that contains the image file details for each mime type.
This change also increases image upload retries to accommodate additional image sizes. It also adds a `$mime_type` parameter to the `wp_get_missing_image_subsizes` function and filter.
This change adds three new filters to enable full control of secondary mime image generation and output:
* A new filter `wp_image_sizes_with_additional_mime_type_support` that filters the sizes that support secondary mime type output. Developers can use this to control the output of additional mime type sub-sized images on a per size basis.
* A new filter `wp_upload_image_mime_transforms` that filters the output mime types for a given input mime type. Developers can use this to control generation of additional mime types for a given input mime type or even override the original mime type.
* A new filter `wp_content_image_mimes` which controls image mime type output selection and order for frontend content. Developers can use this to control the mime type output preference order for content images. Content images inserted from the media library will use the available image versions based on the order from this filter.
Thanks to the many contributors who helped develop, test and give feedback on this feature.
A haiku to summarize:
Upload a JPEG
Images of all sizes
Output as WebPs
Props flixos90, MatthiasReinholz, studiolxv, markhowellsmead, eatingrules, pbiron, mukesh27, joegrainger, mehulkaklotar, tweetythierry, akshitsethi, peterwilsoncc, eugenemanuilov, mitogh, shetheliving, clarkeemily, codekraft, mikeschroder, clorith, kasparsd, spacedmonkey, trevorpfromsandee, jb510, scofennellgmailcom, seedsca, cagsmith, karinclimber, dainemawer, baxbridge, grapplerulrich, sobatkras, chynnabenton, tonylocalword, barneydavey, kwillmorth, garymatthews919, olliejones, imarkinteractive, jeffpaul, feastdesignco, webbeetle, masteradhoc.
See #55443.
Built from https://develop.svn.wordpress.org/trunk@53751
git-svn-id: http://core.svn.wordpress.org/trunk@53310 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-07-21 20:03:13 +02:00
|
|
|
}
|
2019-06-15 03:02:52 +02:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
/*
|
|
|
|
* Initial save of the new metadata.
|
|
|
|
* At this point the file was uploaded and moved to the uploads directory
|
|
|
|
* but the image sub-sizes haven't been created yet and the `sizes` array is empty.
|
|
|
|
*/
|
|
|
|
wp_update_attachment_metadata( $attachment_id, $image_meta );
|
Media: enable generating multiple mime types for image uploads; specifically WebP versions for JPEG images by default.
This changeset adds the capability for core media uploads to generate sub sized images in more than a single mime type. The output formats for each mime type can be controlled through a filter. WebP is used as an additional output format for JPEG images by default to improve front end performance.
When generating additional mime types, only images which are smaller than the respective original are retained. By default, additional mime type images are only generated for the built-in core image sizes and any custom sizes that have opted in.
Image meta is updated with a new 'sources' array containing file details for each mime type. Each image size in the 'sizes' array also gets a new 'sources' array that contains the image file details for each mime type.
This change also increases image upload retries to accommodate additional image sizes. It also adds a `$mime_type` parameter to the `wp_get_missing_image_subsizes` function and filter.
This change adds three new filters to enable full control of secondary mime image generation and output:
* A new filter `wp_image_sizes_with_additional_mime_type_support` that filters the sizes that support secondary mime type output. Developers can use this to control the output of additional mime type sub-sized images on a per size basis.
* A new filter `wp_upload_image_mime_transforms` that filters the output mime types for a given input mime type. Developers can use this to control generation of additional mime types for a given input mime type or even override the original mime type.
* A new filter `wp_content_image_mimes` which controls image mime type output selection and order for frontend content. Developers can use this to control the mime type output preference order for content images. Content images inserted from the media library will use the available image versions based on the order from this filter.
Thanks to the many contributors who helped develop, test and give feedback on this feature.
A haiku to summarize:
Upload a JPEG
Images of all sizes
Output as WebPs
Props flixos90, MatthiasReinholz, studiolxv, markhowellsmead, eatingrules, pbiron, mukesh27, joegrainger, mehulkaklotar, tweetythierry, akshitsethi, peterwilsoncc, eugenemanuilov, mitogh, shetheliving, clarkeemily, codekraft, mikeschroder, clorith, kasparsd, spacedmonkey, trevorpfromsandee, jb510, scofennellgmailcom, seedsca, cagsmith, karinclimber, dainemawer, baxbridge, grapplerulrich, sobatkras, chynnabenton, tonylocalword, barneydavey, kwillmorth, garymatthews919, olliejones, imarkinteractive, jeffpaul, feastdesignco, webbeetle, masteradhoc.
See #55443.
Built from https://develop.svn.wordpress.org/trunk@53751
git-svn-id: http://core.svn.wordpress.org/trunk@53310 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-07-21 20:03:13 +02:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
$new_sizes = wp_get_registered_image_subsizes();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the image sizes automatically generated when uploading an image.
|
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
* @since 4.4.0 Added the `$image_meta` argument.
|
|
|
|
* @since 5.3.0 Added the `$attachment_id` argument.
|
|
|
|
*
|
|
|
|
* @param array $new_sizes Associative array of image sizes to be created.
|
|
|
|
* @param array $image_meta The image meta data: width, height, file, sizes, etc.
|
|
|
|
* @param int $attachment_id The attachment post ID for the image.
|
|
|
|
*/
|
|
|
|
$new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id );
|
|
|
|
|
|
|
|
return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id );
|
2019-06-15 03:02:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Low-level function to create image sub-sizes.
|
|
|
|
*
|
|
|
|
* Updates the image meta after each sub-size is created.
|
|
|
|
* Errors are stored in the returned image metadata array.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
* @access private
|
|
|
|
*
|
2022-09-06 23:13:12 +02:00
|
|
|
* @param array $new_sizes Array defining what sizes to create.
|
|
|
|
* @param string $file Full path to the image file.
|
|
|
|
* @param array $image_meta The attachment meta data array.
|
|
|
|
* @param int $attachment_id Attachment ID to process.
|
2019-06-15 03:02:52 +02:00
|
|
|
* @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
|
|
|
|
*/
|
2022-09-06 23:13:12 +02:00
|
|
|
function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
|
2019-09-07 03:07:55 +02:00
|
|
|
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
|
|
|
|
// Not an image attachment.
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
// Check if any of the new sizes already exist.
|
|
|
|
if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) {
|
|
|
|
foreach ( $image_meta['sizes'] as $size_name => $size_meta ) {
|
2020-01-18 01:54:04 +01:00
|
|
|
/*
|
|
|
|
* Only checks "size name" so we don't override existing images even if the dimensions
|
|
|
|
* don't match the currently defined size with the same name.
|
|
|
|
* To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta.
|
|
|
|
*/
|
2019-06-15 03:02:52 +02:00
|
|
|
if ( array_key_exists( $size_name, $new_sizes ) ) {
|
2022-09-06 23:13:12 +02:00
|
|
|
unset( $new_sizes[ $size_name ] );
|
2016-08-22 06:37:28 +02:00
|
|
|
}
|
2009-12-08 22:08:19 +01:00
|
|
|
}
|
2019-06-15 03:02:52 +02:00
|
|
|
} else {
|
|
|
|
$image_meta['sizes'] = array();
|
|
|
|
}
|
2009-12-08 22:08:19 +01:00
|
|
|
|
2019-09-07 03:07:55 +02:00
|
|
|
if ( empty( $new_sizes ) ) {
|
|
|
|
// Nothing to do...
|
|
|
|
return $image_meta;
|
|
|
|
}
|
2019-07-16 23:48:56 +02:00
|
|
|
|
2020-01-18 01:54:04 +01:00
|
|
|
/*
|
|
|
|
* Sort the image sub-sizes in order of priority when creating them.
|
|
|
|
* This ensures there is an appropriate sub-size the user can access immediately
|
|
|
|
* even when there was an error and not all sub-sizes were created.
|
|
|
|
*/
|
2019-09-07 03:07:55 +02:00
|
|
|
$priority = array(
|
|
|
|
'medium' => null,
|
|
|
|
'large' => null,
|
|
|
|
'thumbnail' => null,
|
|
|
|
'medium_large' => null,
|
|
|
|
);
|
2019-07-16 23:48:56 +02:00
|
|
|
|
2019-09-07 03:07:55 +02:00
|
|
|
$new_sizes = array_filter( array_merge( $priority, $new_sizes ) );
|
2019-06-15 03:02:52 +02:00
|
|
|
|
2022-09-06 23:13:12 +02:00
|
|
|
$editor = wp_get_image_editor( $file );
|
2008-08-09 07:36:14 +02:00
|
|
|
|
2019-09-07 03:07:55 +02:00
|
|
|
if ( is_wp_error( $editor ) ) {
|
|
|
|
// The image cannot be edited.
|
|
|
|
return $image_meta;
|
|
|
|
}
|
|
|
|
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
// If stored EXIF data exists, rotate the source image before creating sub-sizes.
|
|
|
|
if ( ! empty( $image_meta['image_meta'] ) ) {
|
|
|
|
$rotated = $editor->maybe_exif_rotate();
|
|
|
|
|
|
|
|
if ( is_wp_error( $rotated ) ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// TODO: Log errors.
|
Media/Upload: rotate images on upload according to EXIF Orientation.
Props msaggiorato, wpdavis, markoheijnen, dhuyvetter, msaggiorato, n7studios, triplejumper12, pbiron, mikeschroder, joemcgill, azaozz.
Fixes #14459.
Built from https://develop.svn.wordpress.org/trunk@46202
git-svn-id: http://core.svn.wordpress.org/trunk@46014 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2019-09-20 20:21:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:07:55 +02:00
|
|
|
if ( method_exists( $editor, 'make_subsize' ) ) {
|
|
|
|
foreach ( $new_sizes as $new_size_name => $new_size_data ) {
|
|
|
|
$new_size_meta = $editor->make_subsize( $new_size_data );
|
|
|
|
|
|
|
|
if ( is_wp_error( $new_size_meta ) ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// TODO: Log errors.
|
2019-06-15 03:02:52 +02:00
|
|
|
} else {
|
2019-09-07 03:07:55 +02:00
|
|
|
// Save the size meta value.
|
2022-09-06 23:13:12 +02:00
|
|
|
$image_meta['sizes'][ $new_size_name ] = $new_size_meta;
|
2019-10-14 22:05:01 +02:00
|
|
|
wp_update_attachment_metadata( $attachment_id, $image_meta );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2019-09-07 03:07:55 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Fall back to `$editor->multi_resize()`.
|
|
|
|
$created_sizes = $editor->multi_resize( $new_sizes );
|
|
|
|
|
|
|
|
if ( ! empty( $created_sizes ) ) {
|
2022-09-06 23:13:12 +02:00
|
|
|
$image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
|
2019-09-07 03:07:55 +02:00
|
|
|
wp_update_attachment_metadata( $attachment_id, $image_meta );
|
2012-11-14 15:26:52 +01:00
|
|
|
}
|
2019-06-15 03:02:52 +02:00
|
|
|
}
|
2008-08-09 07:36:14 +02:00
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
return $image_meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Generates attachment meta data and create image sub-sizes for images.
|
2019-06-15 03:02:52 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2022-06-21 13:18:17 +02:00
|
|
|
* @since 6.0.0 The `$filesize` value was added to the returned array.
|
2019-06-15 03:02:52 +02:00
|
|
|
*
|
2021-12-12 15:54:00 +01:00
|
|
|
* @param int $attachment_id Attachment ID to process.
|
|
|
|
* @param string $file Filepath of the attached image.
|
2021-06-15 18:52:56 +02:00
|
|
|
* @return array Metadata for attachment.
|
2019-06-15 03:02:52 +02:00
|
|
|
*/
|
|
|
|
function wp_generate_attachment_metadata( $attachment_id, $file ) {
|
|
|
|
$attachment = get_post( $attachment_id );
|
|
|
|
|
|
|
|
$metadata = array();
|
|
|
|
$support = false;
|
|
|
|
$mime_type = get_post_mime_type( $attachment );
|
|
|
|
|
|
|
|
if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) {
|
|
|
|
// Make thumbnails and other intermediate sizes.
|
2019-09-07 03:07:55 +02:00
|
|
|
$metadata = wp_create_image_subsizes( $file, $attachment_id );
|
2015-03-06 21:26:26 +01:00
|
|
|
} elseif ( wp_attachment_is( 'video', $attachment ) ) {
|
2013-03-21 05:55:42 +01:00
|
|
|
$metadata = wp_read_video_metadata( $file );
|
2017-12-01 00:11:00 +01:00
|
|
|
$support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
|
2015-03-06 21:26:26 +01:00
|
|
|
} elseif ( wp_attachment_is( 'audio', $attachment ) ) {
|
2013-03-21 05:55:42 +01:00
|
|
|
$metadata = wp_read_audio_metadata( $file );
|
2017-12-01 00:11:00 +01:00
|
|
|
$support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
|
2007-05-25 09:16:21 +02:00
|
|
|
}
|
2008-09-17 02:40:10 +02:00
|
|
|
|
2021-06-15 21:10:58 +02:00
|
|
|
/*
|
|
|
|
* wp_read_video_metadata() and wp_read_audio_metadata() return `false`
|
|
|
|
* if the attachment does not exist in the local filesystem,
|
|
|
|
* so make sure to convert the value to an array.
|
|
|
|
*/
|
2021-06-15 18:52:56 +02:00
|
|
|
if ( ! is_array( $metadata ) ) {
|
|
|
|
$metadata = array();
|
|
|
|
}
|
|
|
|
|
2013-03-21 05:55:42 +01:00
|
|
|
if ( $support && ! empty( $metadata['image']['data'] ) ) {
|
2014-07-17 11:14:16 +02:00
|
|
|
// Check for existing cover.
|
2017-12-01 00:11:00 +01:00
|
|
|
$hash = md5( $metadata['image']['data'] );
|
|
|
|
$posts = get_posts(
|
|
|
|
array(
|
|
|
|
'fields' => 'ids',
|
|
|
|
'post_type' => 'attachment',
|
|
|
|
'post_mime_type' => $metadata['image']['mime'],
|
|
|
|
'post_status' => 'inherit',
|
|
|
|
'posts_per_page' => 1,
|
|
|
|
'meta_key' => '_cover_hash',
|
|
|
|
'meta_value' => $hash,
|
|
|
|
)
|
|
|
|
);
|
2014-03-30 22:54:15 +02:00
|
|
|
$exists = reset( $posts );
|
|
|
|
|
|
|
|
if ( ! empty( $exists ) ) {
|
|
|
|
update_post_meta( $attachment_id, '_thumbnail_id', $exists );
|
|
|
|
} else {
|
|
|
|
$ext = '.jpg';
|
|
|
|
switch ( $metadata['image']['mime'] ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
case 'image/gif':
|
|
|
|
$ext = '.gif';
|
|
|
|
break;
|
|
|
|
case 'image/png':
|
|
|
|
$ext = '.png';
|
|
|
|
break;
|
Images: enable WebP support.
Add support for uploading, editing and saving WebP images when supported by the server.
Add 'image/webp' to supported mime types. Correctly identify WebP images and sizes even when PHP doesn't support WebP. Resize uploaded WebP files (when supported) and use for front end markup.
Props markoheijne, blobfolio, Clorith, joemcgill, atjn, desrosj, spacedmonkey, marylauc, mikeschroder, hellofromtonya, flixos90.
Fixes #35725.
Built from https://develop.svn.wordpress.org/trunk@50810
git-svn-id: http://core.svn.wordpress.org/trunk@50419 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-05-04 16:44:58 +02:00
|
|
|
case 'image/webp':
|
|
|
|
$ext = '.webp';
|
|
|
|
break;
|
2014-03-30 22:54:15 +02:00
|
|
|
}
|
2019-03-01 21:58:52 +01:00
|
|
|
$basename = str_replace( '.', '-', wp_basename( $file ) ) . '-image' . $ext;
|
2014-03-30 22:54:15 +02:00
|
|
|
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
|
|
|
|
if ( false === $uploaded['error'] ) {
|
|
|
|
$image_attachment = array(
|
|
|
|
'post_mime_type' => $metadata['image']['mime'],
|
2017-12-01 00:11:00 +01:00
|
|
|
'post_type' => 'attachment',
|
|
|
|
'post_content' => '',
|
2014-03-30 22:54:15 +02:00
|
|
|
);
|
|
|
|
/**
|
2016-05-22 20:01:30 +02:00
|
|
|
* Filters the parameters for the attachment thumbnail creation.
|
2014-03-30 22:54:15 +02:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param array $image_attachment An array of parameters to create the thumbnail.
|
2014-04-07 22:10:16 +02:00
|
|
|
* @param array $metadata Current attachment metadata.
|
2020-03-16 19:40:07 +01:00
|
|
|
* @param array $uploaded {
|
|
|
|
* Information about the newly-uploaded file.
|
|
|
|
*
|
2022-09-06 23:13:12 +02:00
|
|
|
* @type string $file Filename of the newly-uploaded file.
|
|
|
|
* @type string $url URL of the uploaded file.
|
|
|
|
* @type string $type File type.
|
2020-03-16 19:40:07 +01:00
|
|
|
* }
|
2014-03-30 22:54:15 +02:00
|
|
|
*/
|
|
|
|
$image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded );
|
|
|
|
|
|
|
|
$sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] );
|
|
|
|
add_post_meta( $sub_attachment_id, '_cover_hash', $hash );
|
|
|
|
$attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] );
|
|
|
|
wp_update_attachment_metadata( $sub_attachment_id, $attach_data );
|
|
|
|
update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id );
|
|
|
|
}
|
2013-03-21 05:55:42 +01:00
|
|
|
}
|
2019-01-11 07:40:50 +01:00
|
|
|
} elseif ( 'application/pdf' === $mime_type ) {
|
|
|
|
// Try to create image thumbnails for PDFs.
|
|
|
|
|
Media: Add support for rendering PDF thumbnails.
When support for PDFs is available, on upload,
render 'Thumbnail', 'Medium', 'Large', and 'Full' sizes of
the first page, and save them in attachment meta.
Use these renders within Add Media, Media Gallery and List views,
Attachment Details, Post/Attachment Edit screens, and Attachment pages.
Support available by default via Imagick -> ImageMagick -> Ghostscript,
but can be provided by any `WP_Image_Editor` that supports PDFs.
Props adamsilverstein, azaozz, celloexpressions, desrosj, dglingren, ericlewis, ipstenu, joemcgill, joyously, markoheijnen, melchoyce, mikeschroder, tomauger.
Fixes #31050.
Built from https://develop.svn.wordpress.org/trunk@38949
git-svn-id: http://core.svn.wordpress.org/trunk@38892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-26 09:28:32 +02:00
|
|
|
$fallback_sizes = array(
|
|
|
|
'thumbnail',
|
|
|
|
'medium',
|
|
|
|
'large',
|
|
|
|
);
|
|
|
|
|
2016-11-15 14:26:30 +01:00
|
|
|
/**
|
|
|
|
* Filters the image sizes generated for non-image mime types.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2019-11-03 23:23:01 +01:00
|
|
|
* @param string[] $fallback_sizes An array of image size names.
|
|
|
|
* @param array $metadata Current attachment metadata.
|
2016-11-15 14:26:30 +01:00
|
|
|
*/
|
|
|
|
$fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
|
|
|
|
|
2019-06-15 03:32:53 +02:00
|
|
|
$registered_sizes = wp_get_registered_image_subsizes();
|
|
|
|
$merged_sizes = array_intersect_key( $registered_sizes, array_flip( $fallback_sizes ) );
|
2016-12-16 21:30:42 +01:00
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
// Force thumbnails to be soft crops.
|
|
|
|
if ( isset( $merged_sizes['thumbnail'] ) && is_array( $merged_sizes['thumbnail'] ) ) {
|
|
|
|
$merged_sizes['thumbnail']['crop'] = false;
|
Media: Add support for rendering PDF thumbnails.
When support for PDFs is available, on upload,
render 'Thumbnail', 'Medium', 'Large', and 'Full' sizes of
the first page, and save them in attachment meta.
Use these renders within Add Media, Media Gallery and List views,
Attachment Details, Post/Attachment Edit screens, and Attachment pages.
Support available by default via Imagick -> ImageMagick -> Ghostscript,
but can be provided by any `WP_Image_Editor` that supports PDFs.
Props adamsilverstein, azaozz, celloexpressions, desrosj, dglingren, ericlewis, ipstenu, joemcgill, joyously, markoheijnen, melchoyce, mikeschroder, tomauger.
Fixes #31050.
Built from https://develop.svn.wordpress.org/trunk@38949
git-svn-id: http://core.svn.wordpress.org/trunk@38892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-26 09:28:32 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 14:26:30 +01:00
|
|
|
// Only load PDFs in an image editor if we're processing sizes.
|
2019-06-15 03:02:52 +02:00
|
|
|
if ( ! empty( $merged_sizes ) ) {
|
2016-11-15 14:26:30 +01:00
|
|
|
$editor = wp_get_image_editor( $file );
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
if ( ! is_wp_error( $editor ) ) { // No support for this type of file.
|
2017-02-27 16:39:47 +01:00
|
|
|
/*
|
|
|
|
* PDFs may have the same file filename as JPEGs.
|
|
|
|
* Ensure the PDF preview image does not overwrite any JPEG images that already exist.
|
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
$dirname = dirname( $file ) . '/';
|
|
|
|
$ext = '.' . pathinfo( $file, PATHINFO_EXTENSION );
|
2017-02-27 16:39:47 +01:00
|
|
|
$preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' );
|
|
|
|
|
|
|
|
$uploaded = $editor->save( $preview_file, 'image/jpeg' );
|
2016-11-15 14:26:30 +01:00
|
|
|
unset( $editor );
|
Media: Add support for rendering PDF thumbnails.
When support for PDFs is available, on upload,
render 'Thumbnail', 'Medium', 'Large', and 'Full' sizes of
the first page, and save them in attachment meta.
Use these renders within Add Media, Media Gallery and List views,
Attachment Details, Post/Attachment Edit screens, and Attachment pages.
Support available by default via Imagick -> ImageMagick -> Ghostscript,
but can be provided by any `WP_Image_Editor` that supports PDFs.
Props adamsilverstein, azaozz, celloexpressions, desrosj, dglingren, ericlewis, ipstenu, joemcgill, joyously, markoheijnen, melchoyce, mikeschroder, tomauger.
Fixes #31050.
Built from https://develop.svn.wordpress.org/trunk@38949
git-svn-id: http://core.svn.wordpress.org/trunk@38892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-26 09:28:32 +02:00
|
|
|
|
2016-11-15 14:26:30 +01:00
|
|
|
// Resize based on the full size image, rather than the source.
|
|
|
|
if ( ! is_wp_error( $uploaded ) ) {
|
2019-10-11 09:54:03 +02:00
|
|
|
$image_file = $uploaded['path'];
|
2016-11-15 14:26:30 +01:00
|
|
|
unset( $uploaded['path'] );
|
Media: Add support for rendering PDF thumbnails.
When support for PDFs is available, on upload,
render 'Thumbnail', 'Medium', 'Large', and 'Full' sizes of
the first page, and save them in attachment meta.
Use these renders within Add Media, Media Gallery and List views,
Attachment Details, Post/Attachment Edit screens, and Attachment pages.
Support available by default via Imagick -> ImageMagick -> Ghostscript,
but can be provided by any `WP_Image_Editor` that supports PDFs.
Props adamsilverstein, azaozz, celloexpressions, desrosj, dglingren, ericlewis, ipstenu, joemcgill, joyously, markoheijnen, melchoyce, mikeschroder, tomauger.
Fixes #31050.
Built from https://develop.svn.wordpress.org/trunk@38949
git-svn-id: http://core.svn.wordpress.org/trunk@38892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-26 09:28:32 +02:00
|
|
|
|
2019-10-11 09:54:03 +02:00
|
|
|
$metadata['sizes'] = array(
|
|
|
|
'full' => $uploaded,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Save the meta data before any image post-processing errors could happen.
|
|
|
|
wp_update_attachment_metadata( $attachment_id, $metadata );
|
|
|
|
|
|
|
|
// Create sub-sizes saving the image meta after each.
|
2022-09-06 23:13:12 +02:00
|
|
|
$metadata = _wp_make_subsizes( $merged_sizes, $image_file, $metadata, $attachment_id );
|
Media: Add support for rendering PDF thumbnails.
When support for PDFs is available, on upload,
render 'Thumbnail', 'Medium', 'Large', and 'Full' sizes of
the first page, and save them in attachment meta.
Use these renders within Add Media, Media Gallery and List views,
Attachment Details, Post/Attachment Edit screens, and Attachment pages.
Support available by default via Imagick -> ImageMagick -> Ghostscript,
but can be provided by any `WP_Image_Editor` that supports PDFs.
Props adamsilverstein, azaozz, celloexpressions, desrosj, dglingren, ericlewis, ipstenu, joemcgill, joyously, markoheijnen, melchoyce, mikeschroder, tomauger.
Fixes #31050.
Built from https://develop.svn.wordpress.org/trunk@38949
git-svn-id: http://core.svn.wordpress.org/trunk@38892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2016-10-26 09:28:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 15:39:10 +01:00
|
|
|
|
2014-07-17 11:14:16 +02:00
|
|
|
// Remove the blob of binary data from the array.
|
2021-06-15 18:52:56 +02:00
|
|
|
unset( $metadata['image']['data'] );
|
2013-03-21 05:55:42 +01:00
|
|
|
|
Media: Store attachment’s file size in metadata.
Store the file size of all newly uploaded attachments, as part of the metadata stored in post meta. Storing file size means, developers will not have to resort to doing `filesize` function calls, that can be time consuming on assets on offloaded to services like Amazon’s S3.
This change also introduces a new helper function called, `wp_filesize`. This is a wrapper around the `filesize` php function, that adds some helpful filters and ensures the return value is an integer.
Props Cybr, Spacedmonkey, SergeyBiryukov, johnwatkins0, swissspidy, desrosj, joemcgill, azaozz, antpb, adamsilverstein, uday17035.
Fixes #49412.
Built from https://develop.svn.wordpress.org/trunk@52837
git-svn-id: http://core.svn.wordpress.org/trunk@52426 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-03-10 14:10:02 +01:00
|
|
|
// Capture file size for cases where it has not been captured yet, such as PDFs.
|
|
|
|
if ( ! isset( $metadata['filesize'] ) && file_exists( $file ) ) {
|
|
|
|
$metadata['filesize'] = wp_filesize( $file );
|
|
|
|
}
|
|
|
|
|
2014-02-20 07:27:13 +01:00
|
|
|
/**
|
2016-05-22 20:01:30 +02:00
|
|
|
* Filters the generated attachment meta data.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2019-10-31 01:13:01 +01:00
|
|
|
* @since 5.3.0 The `$context` parameter was added.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
2019-10-30 22:10:04 +01:00
|
|
|
* @param array $metadata An array of attachment meta data.
|
|
|
|
* @param int $attachment_id Current attachment ID.
|
|
|
|
* @param string $context Additional context. Can be 'create' when metadata was initially created for new attachment
|
|
|
|
* or 'update' when the metadata was updated.
|
2014-02-20 07:27:13 +01:00
|
|
|
*/
|
2019-10-30 22:10:04 +01:00
|
|
|
return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' );
|
2007-05-25 09:16:21 +02:00
|
|
|
}
|
|
|
|
|
2008-09-17 02:40:10 +02:00
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Converts a fraction string to a decimal.
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
2008-10-10 20:21:16 +02:00
|
|
|
* @since 2.5.0
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
* @param string $str Fraction string.
|
|
|
|
* @return int|float Returns calculated fraction or integer 0 on invalid input.
|
2008-09-17 02:40:10 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function wp_exif_frac2dec( $str ) {
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
if ( ! is_scalar( $str ) || is_bool( $str ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_string( $str ) ) {
|
|
|
|
return $str; // This can only be an integer or float, so this is fine.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fractions passed as a string must contain a single `/`.
|
|
|
|
if ( substr_count( $str, '/' ) !== 1 ) {
|
|
|
|
if ( is_numeric( $str ) ) {
|
|
|
|
return (float) $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-07-09 07:45:58 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 01:07:07 +01:00
|
|
|
list( $numerator, $denominator ) = explode( '/', $str );
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
|
|
|
|
// Both the numerator and the denominator must be numbers.
|
|
|
|
if ( ! is_numeric( $numerator ) || ! is_numeric( $denominator ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The denominator must not be zero.
|
Coding Standards: Upgrade WPCS to version 3.0.0.
This is an important release which makes significant changes to improve the accuracy, performance, stability and maintainability of all sniffs, as well as making WordPressCS much better at handling modern PHP.
WordPressCS 3.0.0 contains breaking changes, both for people using ignore annotations, people maintaining custom rulesets, as well as for sniff developers who maintain a custom PHPCS standard based on WordPressCS.
If you are an end-user or maintain a custom WordPressCS based ruleset, please start by reading the [https://github.com/WordPress/WordPress-Coding-Standards/wiki/Upgrade-Guide-to-WordPressCS-3.0.0-for-ruleset-maintainers Upgrade Guide to WordPressCS 3.0.0 for ruleset maintainers] which lists the most important changes and contains a step by step guide for upgrading.
If you are a maintainer of an external standard based on WordPressCS and any of your custom sniffs are based on or extend WordPressCS sniffs, please read the [https://github.com/WordPress/WordPress-Coding-Standards/wiki/Upgrade-Guide-to-WordPressCS-3.0.0-for-Developers-of-external-standards Upgrade Guide to WordPressCS 3.0.0 for Developers].
In all cases, please read the complete changelog carefully before you upgrade.
This commit:
* Updates the Composer dependencies to use the new version, including updating the underlying PHP_CodeSniffer dependency to the new minimum supported version for WPCS.[[BR]] Note: the Composer PHPCS installer plugin is no longer explicitly required as it is now a dependency of WPCS, so the dependency is inherited automatically.
* Updates the ruleset for WPCS 3.0.0. This includes:
* Raising the memory limit to be on the safe side as WPCS 3.0.0 contains a lot more sniffs.
* Removing explicit inclusions of extra rules, which have now been added to the `WordPress-Core` ruleset..
* Updating property names for select sniffs.
* Updating one exclusion — the `WordPress.CodeAnalysis.AssignmentInCondition` sniff has been (partially) replaced by the `Generic.CodeAnalysis.AssignmentInCondition` sniff.
* Adding one new exclusion.
* Downgrades one new error to a warning.[[BR]] The `Generic.Files.OneObjectStructurePerFile` sniff enforces that there is only one OO structure declaration per file. At this time, this sniff would yield 29 errors. By downgrading the sniff to a ''warning'', the build can pass and the issues can be fixed in due time. For now, the test directory will be excluded until the issues are fixed (as the test directory CS run does not allow for warnings).
* Updates ignore annotations for WPCS 3.0.0.
Reference: [https://github.com/WordPress/WordPress-Coding-Standards/releases/tag/3.0.0 WPCS 3.0.0 release notes].
Follow-up to [43571], [44574], [45600], [47927].
Props jrf, jorbin, desrosj.
See #59161.
Built from https://develop.svn.wordpress.org/trunk@56695
git-svn-id: http://core.svn.wordpress.org/trunk@56207 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-09-26 02:27:12 +02:00
|
|
|
if ( 0 == $denominator ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- Deliberate loose comparison.
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
return 0;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
|
|
|
|
return $numerator / $denominator;
|
2007-11-05 01:01:26 +01:00
|
|
|
}
|
|
|
|
|
2008-09-17 02:40:10 +02:00
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Converts the exif date format to a unix timestamp.
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
2008-10-10 20:21:16 +02:00
|
|
|
* @since 2.5.0
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
2021-12-01 13:17:00 +01:00
|
|
|
* @param string $str A date string expected to be in Exif format (Y:m:d H:i:s).
|
|
|
|
* @return int|false The unix timestamp, or false on failure.
|
2008-09-17 02:40:10 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function wp_exif_date2ts( $str ) {
|
2019-07-09 07:45:58 +02:00
|
|
|
list( $date, $time ) = explode( ' ', trim( $str ) );
|
|
|
|
list( $y, $m, $d ) = explode( ':', $date );
|
2007-11-05 01:01:26 +01:00
|
|
|
|
|
|
|
return strtotime( "{$y}-{$m}-{$d} {$time}" );
|
|
|
|
}
|
|
|
|
|
2008-09-17 02:40:10 +02:00
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Gets extended image metadata, exif or iptc as available.
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
2008-10-10 20:21:16 +02:00
|
|
|
* Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
|
|
|
|
* created_timestamp, focal_length, shutter_speed, and title.
|
|
|
|
*
|
|
|
|
* The IPTC metadata that is retrieved is APP13, credit, byline, created date
|
|
|
|
* and time, caption, copyright, and title. Also includes FNumber, Model,
|
|
|
|
* DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
|
|
|
|
*
|
|
|
|
* @todo Try other exif libraries if available.
|
|
|
|
* @since 2.5.0
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
|
|
|
* @param string $file
|
2021-01-04 18:18:04 +01:00
|
|
|
* @return array|false Image metadata array on success, false on failure.
|
2008-09-17 02:40:10 +02:00
|
|
|
*/
|
2007-11-05 01:01:26 +01:00
|
|
|
function wp_read_image_metadata( $file ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! file_exists( $file ) ) {
|
2007-11-05 01:01:26 +01:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2007-11-05 01:01:26 +01:00
|
|
|
|
2021-02-02 17:53:04 +01:00
|
|
|
list( , , $image_type ) = wp_getimagesize( $file );
|
2007-12-20 23:18:28 +01:00
|
|
|
|
2014-07-17 11:14:16 +02:00
|
|
|
/*
|
|
|
|
* EXIF contains a bunch of data we'll probably never need formatted in ways
|
|
|
|
* that are difficult to use. We'll normalize it and just extract the fields
|
|
|
|
* that are likely to be useful. Fractions and numbers are converted to
|
|
|
|
* floats, dates to unix timestamps, and everything else to strings.
|
|
|
|
*/
|
2007-11-05 01:01:26 +01:00
|
|
|
$meta = array(
|
2017-12-01 00:11:00 +01:00
|
|
|
'aperture' => 0,
|
|
|
|
'credit' => '',
|
|
|
|
'camera' => '',
|
|
|
|
'caption' => '',
|
2007-11-05 01:01:26 +01:00
|
|
|
'created_timestamp' => 0,
|
2017-12-01 00:11:00 +01:00
|
|
|
'copyright' => '',
|
|
|
|
'focal_length' => 0,
|
|
|
|
'iso' => 0,
|
|
|
|
'shutter_speed' => 0,
|
|
|
|
'title' => '',
|
|
|
|
'orientation' => 0,
|
|
|
|
'keywords' => array(),
|
2007-11-05 01:01:26 +01:00
|
|
|
);
|
|
|
|
|
2015-09-22 06:49:24 +02:00
|
|
|
$iptc = array();
|
2021-03-19 01:01:03 +01:00
|
|
|
$info = array();
|
2014-07-17 11:14:16 +02:00
|
|
|
/*
|
|
|
|
* Read IPTC first, since it might contain data not available in exif such
|
|
|
|
* as caption, description etc.
|
|
|
|
*/
|
2010-02-20 13:09:30 +01:00
|
|
|
if ( is_callable( 'iptcparse' ) ) {
|
2021-02-02 17:53:04 +01:00
|
|
|
wp_getimagesize( $file, $info );
|
2010-02-20 10:17:34 +01:00
|
|
|
|
2010-02-20 13:09:30 +01:00
|
|
|
if ( ! empty( $info['APP13'] ) ) {
|
2021-05-05 20:47:57 +02:00
|
|
|
// Don't silence errors when in debug mode, unless running unit tests.
|
|
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
|
|
|
|
&& ! defined( 'WP_RUN_CORE_TESTS' )
|
2021-02-02 17:53:04 +01:00
|
|
|
) {
|
|
|
|
$iptc = iptcparse( $info['APP13'] );
|
|
|
|
} else {
|
2023-09-29 14:21:24 +02:00
|
|
|
// Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480
|
2021-02-02 17:53:04 +01:00
|
|
|
$iptc = @iptcparse( $info['APP13'] );
|
|
|
|
}
|
2010-02-20 10:17:34 +01:00
|
|
|
|
2022-04-28 13:00:10 +02:00
|
|
|
if ( ! is_array( $iptc ) ) {
|
|
|
|
$iptc = array();
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Headline, "A brief synopsis of the caption".
|
2014-07-17 11:14:16 +02:00
|
|
|
if ( ! empty( $iptc['2#105'][0] ) ) {
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['title'] = trim( $iptc['2#105'][0] );
|
2017-12-01 00:11:00 +01:00
|
|
|
/*
|
|
|
|
* Title, "Many use the Title field to store the filename of the image,
|
2020-01-29 01:45:18 +01:00
|
|
|
* though the field may be used in many ways".
|
2017-12-01 00:11:00 +01:00
|
|
|
*/
|
2014-07-17 11:14:16 +02:00
|
|
|
} elseif ( ! empty( $iptc['2#005'][0] ) ) {
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['title'] = trim( $iptc['2#005'][0] );
|
2014-07-17 11:14:16 +02:00
|
|
|
}
|
2010-02-20 10:17:34 +01:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
if ( ! empty( $iptc['2#120'][0] ) ) { // Description / legacy caption.
|
2012-09-19 01:38:25 +02:00
|
|
|
$caption = trim( $iptc['2#120'][0] );
|
2014-06-24 00:21:15 +02:00
|
|
|
|
2015-03-10 06:07:28 +01:00
|
|
|
mbstring_binary_safe_encoding();
|
|
|
|
$caption_length = strlen( $caption );
|
|
|
|
reset_mbstring_encoding();
|
|
|
|
|
|
|
|
if ( empty( $meta['title'] ) && $caption_length < 80 ) {
|
2010-02-20 10:17:34 +01:00
|
|
|
// Assume the title is stored in 2:120 if it's short.
|
2015-03-10 06:07:28 +01:00
|
|
|
$meta['title'] = $caption;
|
2010-02-20 10:17:34 +01:00
|
|
|
}
|
2015-03-10 06:07:28 +01:00
|
|
|
|
|
|
|
$meta['caption'] = $caption;
|
2010-02-20 10:17:34 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 01:54:04 +01:00
|
|
|
if ( ! empty( $iptc['2#110'][0] ) ) { // Credit.
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['credit'] = trim( $iptc['2#110'][0] );
|
2020-01-18 01:54:04 +01:00
|
|
|
} elseif ( ! empty( $iptc['2#080'][0] ) ) { // Creator / legacy byline.
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['credit'] = trim( $iptc['2#080'][0] );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-02-20 10:17:34 +01:00
|
|
|
|
2020-01-18 01:54:04 +01:00
|
|
|
if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) { // Created date and time.
|
2010-02-20 13:09:30 +01:00
|
|
|
$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-02-20 10:17:34 +01:00
|
|
|
|
2020-01-18 01:54:04 +01:00
|
|
|
if ( ! empty( $iptc['2#116'][0] ) ) { // Copyright.
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['copyright'] = trim( $iptc['2#116'][0] );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-09-22 06:19:24 +02:00
|
|
|
|
2020-01-18 01:54:04 +01:00
|
|
|
if ( ! empty( $iptc['2#025'][0] ) ) { // Keywords array.
|
2015-09-22 06:19:24 +02:00
|
|
|
$meta['keywords'] = array_values( $iptc['2#025'] );
|
|
|
|
}
|
2017-11-27 00:57:55 +01:00
|
|
|
}
|
2007-11-05 01:01:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-17 03:51:36 +02:00
|
|
|
$exif = array();
|
2018-03-25 22:45:29 +02:00
|
|
|
|
2014-02-20 07:27:13 +01:00
|
|
|
/**
|
2016-05-22 20:01:30 +02:00
|
|
|
* Filters the image types to check for exif data.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
2021-12-07 13:20:02 +01:00
|
|
|
* @param int[] $image_types Array of image types to check for exif data. Each value
|
|
|
|
* is usually one of the `IMAGETYPE_*` constants.
|
2014-02-20 07:27:13 +01:00
|
|
|
*/
|
2018-03-25 22:41:29 +02:00
|
|
|
$exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) );
|
|
|
|
|
2019-06-15 03:02:52 +02:00
|
|
|
if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) {
|
2021-05-05 20:47:57 +02:00
|
|
|
// Don't silence errors when in debug mode, unless running unit tests.
|
|
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
|
|
|
|
&& ! defined( 'WP_RUN_CORE_TESTS' )
|
2021-02-02 17:53:04 +01:00
|
|
|
) {
|
|
|
|
$exif = exif_read_data( $file );
|
|
|
|
} else {
|
2023-09-29 14:21:24 +02:00
|
|
|
// Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480
|
2021-02-02 17:53:04 +01:00
|
|
|
$exif = @exif_read_data( $file );
|
|
|
|
}
|
2010-02-20 10:17:34 +01:00
|
|
|
|
2022-04-28 13:00:10 +02:00
|
|
|
if ( ! is_array( $exif ) ) {
|
|
|
|
$exif = array();
|
|
|
|
}
|
|
|
|
|
2024-01-10 22:59:13 +01:00
|
|
|
$exif_description = '';
|
|
|
|
$exif_usercomment = '';
|
2010-02-20 10:17:34 +01:00
|
|
|
if ( ! empty( $exif['ImageDescription'] ) ) {
|
2024-01-10 22:59:13 +01:00
|
|
|
$exif_description = trim( $exif['ImageDescription'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $exif['COMPUTED']['UserComment'] ) ) {
|
|
|
|
$exif_usercomment = trim( $exif['COMPUTED']['UserComment'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $exif_description ) {
|
2014-06-24 00:21:15 +02:00
|
|
|
mbstring_binary_safe_encoding();
|
2024-01-10 22:59:13 +01:00
|
|
|
$description_length = strlen( $exif_description );
|
2014-06-24 00:21:15 +02:00
|
|
|
reset_mbstring_encoding();
|
|
|
|
if ( empty( $meta['title'] ) && $description_length < 80 ) {
|
2020-01-18 01:54:04 +01:00
|
|
|
// Assume the title is stored in ImageDescription.
|
2024-01-10 22:59:13 +01:00
|
|
|
$meta['title'] = $exif_description;
|
2015-03-10 06:07:28 +01:00
|
|
|
}
|
|
|
|
|
2024-01-10 22:59:13 +01:00
|
|
|
// If both user comments and description are present.
|
|
|
|
if ( empty( $meta['caption'] ) && $exif_description && $exif_usercomment ) {
|
|
|
|
if ( ! empty( $meta['title'] ) && $exif_description === $meta['title'] ) {
|
|
|
|
$caption = $exif_usercomment;
|
|
|
|
} else {
|
|
|
|
if ( $exif_description === $exif_usercomment ) {
|
|
|
|
$caption = $exif_description;
|
|
|
|
} else {
|
|
|
|
$caption = trim( $exif_description . ' ' . $exif_usercomment );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$meta['caption'] = $caption;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $meta['caption'] ) && $exif_usercomment ) {
|
|
|
|
$meta['caption'] = $exif_usercomment;
|
2015-03-10 06:07:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $meta['caption'] ) ) {
|
2024-01-10 22:59:13 +01:00
|
|
|
$meta['caption'] = $exif_description;
|
|
|
|
}
|
|
|
|
} elseif ( empty( $meta['caption'] ) && $exif_usercomment ) {
|
|
|
|
$meta['caption'] = $exif_usercomment;
|
|
|
|
$description_length = strlen( $exif_usercomment );
|
|
|
|
if ( empty( $meta['title'] ) && $description_length < 80 ) {
|
|
|
|
$meta['title'] = trim( $exif_usercomment );
|
2010-02-20 10:17:34 +01:00
|
|
|
}
|
2015-03-10 06:07:28 +01:00
|
|
|
} elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) {
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['caption'] = trim( $exif['Comments'] );
|
2010-02-20 10:17:34 +01:00
|
|
|
}
|
|
|
|
|
2014-05-11 07:54:15 +02:00
|
|
|
if ( empty( $meta['credit'] ) ) {
|
|
|
|
if ( ! empty( $exif['Artist'] ) ) {
|
|
|
|
$meta['credit'] = trim( $exif['Artist'] );
|
2017-12-01 00:11:00 +01:00
|
|
|
} elseif ( ! empty( $exif['Author'] ) ) {
|
2014-05-11 07:54:15 +02:00
|
|
|
$meta['credit'] = trim( $exif['Author'] );
|
|
|
|
}
|
|
|
|
}
|
2010-02-20 10:17:34 +01:00
|
|
|
|
2014-05-11 07:54:15 +02:00
|
|
|
if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) {
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['copyright'] = trim( $exif['Copyright'] );
|
2014-05-11 07:54:15 +02:00
|
|
|
}
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
if ( ! empty( $exif['FNumber'] ) && is_scalar( $exif['FNumber'] ) ) {
|
2007-11-05 01:01:26 +01:00
|
|
|
$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
|
2014-05-11 07:54:15 +02:00
|
|
|
}
|
|
|
|
if ( ! empty( $exif['Model'] ) ) {
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['camera'] = trim( $exif['Model'] );
|
2014-05-11 07:54:15 +02:00
|
|
|
}
|
|
|
|
if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) {
|
|
|
|
$meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] );
|
|
|
|
}
|
|
|
|
if ( ! empty( $exif['FocalLength'] ) ) {
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
$meta['focal_length'] = (string) $exif['FocalLength'];
|
|
|
|
if ( is_scalar( $exif['FocalLength'] ) ) {
|
|
|
|
$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] );
|
|
|
|
}
|
2014-05-11 07:54:15 +02:00
|
|
|
}
|
|
|
|
if ( ! empty( $exif['ISOSpeedRatings'] ) ) {
|
2012-06-02 03:39:00 +02:00
|
|
|
$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta['iso'] = trim( $meta['iso'] );
|
2012-06-02 03:39:00 +02:00
|
|
|
}
|
2014-05-11 07:54:15 +02:00
|
|
|
if ( ! empty( $exif['ExposureTime'] ) ) {
|
Media: Fix `TypeError` and improve `wp_exif_frac2dec()` to only return `int` or `float`.
For certain images, `wp_exif_frac2dec()` unexpectedly returned a string instead of `int` or `float`. This can occur when an image is missing meta and calls the function with `'0/0'`. For those images, a fatal error was thrown on PHP 8.0+:
{{{
TypeError: round(): Argument #1 ($num) must be of type int|float, string given
}}}
Upon deeper review, inconsistent and unexpected results were returned from different types of input values passed to the function.
Changes are:
* Maintains backwards-compatibility for valid input values.
* Fixes handling of invalid input values by bailing out to return the documented type of `int|float` by returning `0`.
* Improves the fractional conditional check.
* Improves the calculated fraction handling to ensure (a) the numerator and denominator are both numeric and (b) the denominator is not equal to zero.
* Safeguards the behavior via tests for all possible ways code could flow through the function.
* Safeguards the backwards-compatibility of the `wp_read_image_metadata()` by adding some defensive coding around the calls to the `wp_exif_frac2dec()` function.
These changes fix the fatal error and make the function more secure, stable, and predictable while maintaining backwards-compatibility for valid input values.
Follow-up to [6313], [9119], [22319], [28367], [45611], [47287].
Props adamsilverstein, jrf, peterwilsoncc, praem90, stevegs, tobiasbg.
Fixes #54385.
Built from https://develop.svn.wordpress.org/trunk@52269
git-svn-id: http://core.svn.wordpress.org/trunk@51861 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-11-29 20:36:05 +01:00
|
|
|
$meta['shutter_speed'] = (string) $exif['ExposureTime'];
|
|
|
|
if ( is_scalar( $exif['ExposureTime'] ) ) {
|
|
|
|
$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] );
|
|
|
|
}
|
2014-05-11 07:54:15 +02:00
|
|
|
}
|
2014-07-25 00:14:16 +02:00
|
|
|
if ( ! empty( $exif['Orientation'] ) ) {
|
|
|
|
$meta['orientation'] = $exif['Orientation'];
|
|
|
|
}
|
2007-11-05 01:01:26 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 01:38:25 +02:00
|
|
|
foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) {
|
2014-05-11 07:54:15 +02:00
|
|
|
if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) {
|
2012-09-19 01:38:25 +02:00
|
|
|
$meta[ $key ] = utf8_encode( $meta[ $key ] );
|
2014-05-11 07:54:15 +02:00
|
|
|
}
|
2012-09-19 01:38:25 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 15:53:27 +01:00
|
|
|
foreach ( $meta['keywords'] as $key => $keyword ) {
|
|
|
|
if ( ! seems_utf8( $keyword ) ) {
|
|
|
|
$meta['keywords'][ $key ] = utf8_encode( $keyword );
|
2014-11-20 16:25:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 15:53:27 +01:00
|
|
|
$meta = wp_kses_post_deep( $meta );
|
|
|
|
|
2014-02-20 07:27:13 +01:00
|
|
|
/**
|
2016-05-22 20:01:30 +02:00
|
|
|
* Filters the array of meta data read from an image's exif data.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
|
|
|
* @since 2.5.0
|
2015-09-22 06:19:24 +02:00
|
|
|
* @since 4.4.0 The `$iptc` parameter was added.
|
2018-03-25 22:45:29 +02:00
|
|
|
* @since 5.0.0 The `$exif` parameter was added.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
2018-03-25 22:41:29 +02:00
|
|
|
* @param array $meta Image meta data.
|
|
|
|
* @param string $file Path to image file.
|
|
|
|
* @param int $image_type Type of image, one of the `IMAGETYPE_XXX` constants.
|
|
|
|
* @param array $iptc IPTC data.
|
2018-03-25 22:45:29 +02:00
|
|
|
* @param array $exif EXIF data.
|
2014-02-20 07:27:13 +01:00
|
|
|
*/
|
2018-03-25 22:45:29 +02:00
|
|
|
return apply_filters( 'wp_read_image_metadata', $meta, $file, $image_type, $iptc, $exif );
|
2007-11-05 01:01:26 +01:00
|
|
|
}
|
|
|
|
|
2008-09-17 02:40:10 +02:00
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Validates that file is an image.
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
2008-10-10 20:21:16 +02:00
|
|
|
* @since 2.5.0
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
|
|
|
* @param string $path File path to test if valid image.
|
|
|
|
* @return bool True if valid image, false if not valid image.
|
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function file_is_valid_image( $path ) {
|
2021-02-02 17:53:04 +01:00
|
|
|
$size = wp_getimagesize( $path );
|
2017-12-01 00:11:00 +01:00
|
|
|
return ! empty( $size );
|
2008-03-03 05:17:37 +01:00
|
|
|
}
|
|
|
|
|
2008-09-17 02:40:10 +02:00
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Validates that file is suitable for displaying within a web page.
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
2008-10-10 20:21:16 +02:00
|
|
|
* @since 2.5.0
|
2008-09-17 02:40:10 +02:00
|
|
|
*
|
|
|
|
* @param string $path File path to test.
|
|
|
|
* @return bool True if suitable, false if not suitable.
|
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function file_is_displayable_image( $path ) {
|
2021-12-31 14:55:03 +01:00
|
|
|
$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO, IMAGETYPE_WEBP );
|
2018-03-05 02:03:31 +01:00
|
|
|
|
2021-02-02 17:53:04 +01:00
|
|
|
$info = wp_getimagesize( $path );
|
2014-05-27 15:25:14 +02:00
|
|
|
if ( empty( $info ) ) {
|
2008-03-03 05:17:37 +01:00
|
|
|
$result = false;
|
2019-06-15 03:02:52 +02:00
|
|
|
} elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) {
|
2008-03-03 05:17:37 +01:00
|
|
|
$result = false;
|
2014-05-27 15:25:14 +02:00
|
|
|
} else {
|
2008-03-03 05:17:37 +01:00
|
|
|
$result = true;
|
2014-05-27 15:25:14 +02:00
|
|
|
}
|
2008-08-09 07:36:14 +02:00
|
|
|
|
2014-02-20 07:27:13 +01:00
|
|
|
/**
|
2016-05-22 20:01:30 +02:00
|
|
|
* Filters whether the current image is displayable in the browser.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
|
|
|
* @param bool $result Whether the image can be displayed. Default true.
|
|
|
|
* @param string $path Path to the image.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'file_is_displayable_image', $result, $path );
|
2008-03-03 05:17:37 +01:00
|
|
|
}
|
2012-04-06 22:47:24 +02:00
|
|
|
|
2012-05-16 19:47:55 +02:00
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Loads an image resource for editing.
|
2012-05-16 19:47:55 +02:00
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
*
|
2020-09-20 18:23:07 +02:00
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string $mime_type Image mime type.
|
|
|
|
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
|
|
|
|
* of width and height values in pixels (in that order). Default 'full'.
|
2020-08-16 15:33:09 +02:00
|
|
|
* @return resource|GdImage|false The resulting image resource or GdImage instance on success,
|
|
|
|
* false on failure.
|
2012-05-16 19:47:55 +02:00
|
|
|
*/
|
|
|
|
function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
|
|
|
|
$filepath = _load_image_to_edit_path( $attachment_id, $size );
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $filepath ) ) {
|
2012-04-06 22:47:24 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-04-06 22:47:24 +02:00
|
|
|
|
|
|
|
switch ( $mime_type ) {
|
|
|
|
case 'image/jpeg':
|
2017-12-01 00:11:00 +01:00
|
|
|
$image = imagecreatefromjpeg( $filepath );
|
2012-04-06 22:47:24 +02:00
|
|
|
break;
|
|
|
|
case 'image/png':
|
2017-12-01 00:11:00 +01:00
|
|
|
$image = imagecreatefrompng( $filepath );
|
2012-04-06 22:47:24 +02:00
|
|
|
break;
|
|
|
|
case 'image/gif':
|
2017-12-01 00:11:00 +01:00
|
|
|
$image = imagecreatefromgif( $filepath );
|
2012-04-06 22:47:24 +02:00
|
|
|
break;
|
Images: enable WebP support.
Add support for uploading, editing and saving WebP images when supported by the server.
Add 'image/webp' to supported mime types. Correctly identify WebP images and sizes even when PHP doesn't support WebP. Resize uploaded WebP files (when supported) and use for front end markup.
Props markoheijne, blobfolio, Clorith, joemcgill, atjn, desrosj, spacedmonkey, marylauc, mikeschroder, hellofromtonya, flixos90.
Fixes #35725.
Built from https://develop.svn.wordpress.org/trunk@50810
git-svn-id: http://core.svn.wordpress.org/trunk@50419 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-05-04 16:44:58 +02:00
|
|
|
case 'image/webp':
|
|
|
|
$image = false;
|
|
|
|
if ( function_exists( 'imagecreatefromwebp' ) ) {
|
|
|
|
$image = imagecreatefromwebp( $filepath );
|
|
|
|
}
|
|
|
|
break;
|
2012-04-06 22:47:24 +02:00
|
|
|
default:
|
|
|
|
$image = false;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-16 15:33:09 +02:00
|
|
|
|
|
|
|
if ( is_gd_image( $image ) ) {
|
2014-02-20 07:27:13 +01:00
|
|
|
/**
|
2016-05-22 20:01:30 +02:00
|
|
|
* Filters the current image being loaded for editing.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
*
|
2020-08-16 15:33:09 +02:00
|
|
|
* @param resource|GdImage $image Current image.
|
2020-09-20 16:05:05 +02:00
|
|
|
* @param int $attachment_id Attachment ID.
|
2020-09-20 18:23:07 +02:00
|
|
|
* @param string|int[] $size Requested image size. Can be any registered image size name, or
|
|
|
|
* an array of width and height values in pixels (in that order).
|
2014-02-20 07:27:13 +01:00
|
|
|
*/
|
|
|
|
$image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
|
2020-08-16 15:33:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
|
|
|
|
imagealphablending( $image, false );
|
|
|
|
imagesavealpha( $image, true );
|
2012-04-06 22:47:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 15:33:09 +02:00
|
|
|
|
2012-04-06 22:47:24 +02:00
|
|
|
return $image;
|
|
|
|
}
|
2012-05-16 19:47:55 +02:00
|
|
|
|
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Retrieves the path or URL of an attachment's attached file.
|
2012-05-16 19:47:55 +02:00
|
|
|
*
|
|
|
|
* If the attached file is not present on the local filesystem (usually due to replication plugins),
|
2020-09-20 17:55:10 +02:00
|
|
|
* then the URL of the file is returned if `allow_url_fopen` is supported.
|
2012-05-16 19:47:55 +02:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @access private
|
|
|
|
*
|
2020-09-20 18:23:07 +02:00
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
|
|
|
|
* of width and height values in pixels (in that order). Default 'full'.
|
|
|
|
* @return string|false File path or URL on success, false on failure.
|
2012-05-16 19:47:55 +02:00
|
|
|
*/
|
|
|
|
function _load_image_to_edit_path( $attachment_id, $size = 'full' ) {
|
|
|
|
$filepath = get_attached_file( $attachment_id );
|
|
|
|
|
|
|
|
if ( $filepath && file_exists( $filepath ) ) {
|
2019-06-15 18:24:52 +02:00
|
|
|
if ( 'full' !== $size ) {
|
|
|
|
$data = image_get_intermediate_size( $attachment_id, $size );
|
|
|
|
|
|
|
|
if ( $data ) {
|
|
|
|
$filepath = path_join( dirname( $filepath ), $data['file'] );
|
|
|
|
|
|
|
|
/**
|
2020-09-20 17:55:10 +02:00
|
|
|
* Filters the path to an attachment's file when editing the image.
|
2019-06-15 18:24:52 +02:00
|
|
|
*
|
|
|
|
* The filter is evaluated for all image sizes except 'full'.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
2020-09-20 18:23:07 +02:00
|
|
|
* @param string $path Path to the current image.
|
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string|int[] $size Requested image size. Can be any registered image size name, or
|
|
|
|
* an array of width and height values in pixels (in that order).
|
2019-06-15 18:24:52 +02:00
|
|
|
*/
|
|
|
|
$filepath = apply_filters( 'load_image_to_edit_filesystempath', $filepath, $attachment_id, $size );
|
|
|
|
}
|
2012-05-16 19:47:55 +02:00
|
|
|
}
|
2019-06-15 18:24:52 +02:00
|
|
|
} elseif ( function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) ) {
|
2014-02-20 07:27:13 +01:00
|
|
|
/**
|
2020-09-20 17:55:10 +02:00
|
|
|
* Filters the path to an attachment's URL when editing the image.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
2020-09-20 17:55:10 +02:00
|
|
|
* The filter is only evaluated if the file isn't stored locally and `allow_url_fopen` is enabled on the server.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
2021-01-03 22:57:09 +01:00
|
|
|
* @param string|false $image_url Current image URL.
|
2020-09-20 18:23:07 +02:00
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string|int[] $size Requested image size. Can be any registered image size name, or
|
|
|
|
* an array of width and height values in pixels (in that order).
|
2014-02-20 07:27:13 +01:00
|
|
|
*/
|
2012-05-16 19:47:55 +02:00
|
|
|
$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
|
|
|
|
}
|
|
|
|
|
2014-02-20 07:27:13 +01:00
|
|
|
/**
|
2016-05-22 20:01:30 +02:00
|
|
|
* Filters the returned path or URL of the current image.
|
2014-02-20 07:27:13 +01:00
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
*
|
2021-01-03 23:04:04 +01:00
|
|
|
* @param string|false $filepath File path or URL to current image, or false.
|
2020-09-20 18:23:07 +02:00
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string|int[] $size Requested image size. Can be any registered image size name, or
|
|
|
|
* an array of width and height values in pixels (in that order).
|
2014-02-20 07:27:13 +01:00
|
|
|
*/
|
2012-05-16 19:47:55 +02:00
|
|
|
return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-05 16:07:09 +02:00
|
|
|
* Copies an existing image file.
|
2012-05-16 19:47:55 +02:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @access private
|
|
|
|
*
|
2020-09-20 16:05:05 +02:00
|
|
|
* @param int $attachment_id Attachment ID.
|
2012-05-16 19:47:55 +02:00
|
|
|
* @return string|false New file path on success, false on failure.
|
|
|
|
*/
|
|
|
|
function _copy_image_file( $attachment_id ) {
|
2019-06-15 03:02:52 +02:00
|
|
|
$dst_file = get_attached_file( $attachment_id );
|
|
|
|
$src_file = $dst_file;
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! file_exists( $src_file ) ) {
|
2012-05-16 19:47:55 +02:00
|
|
|
$src_file = _load_image_to_edit_path( $attachment_id );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-05-16 19:47:55 +02:00
|
|
|
|
|
|
|
if ( $src_file ) {
|
2019-03-01 21:58:52 +01:00
|
|
|
$dst_file = str_replace( wp_basename( $dst_file ), 'copy-' . wp_basename( $dst_file ), $dst_file );
|
|
|
|
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) );
|
2012-06-10 03:26:12 +02:00
|
|
|
|
2014-07-17 11:14:16 +02:00
|
|
|
/*
|
|
|
|
* The directory containing the original file may no longer
|
|
|
|
* exist when using a replication plugin.
|
|
|
|
*/
|
2012-06-10 03:26:12 +02:00
|
|
|
wp_mkdir_p( dirname( $dst_file ) );
|
|
|
|
|
2019-07-09 07:45:58 +02:00
|
|
|
if ( ! copy( $src_file, $dst_file ) ) {
|
2012-05-16 19:47:55 +02:00
|
|
|
$dst_file = false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2012-05-16 19:47:55 +02:00
|
|
|
} else {
|
|
|
|
$dst_file = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dst_file;
|
|
|
|
}
|