2007-05-25 03:16:21 -04:00
|
|
|
<?php
|
2007-11-03 14:33:19 -04:00
|
|
|
/**
|
|
|
|
* File contains all the administration image manipulation functions.
|
|
|
|
*
|
|
|
|
* @package WordPress
|
2008-09-16 20:40:10 -04:00
|
|
|
* @subpackage Administration
|
2007-11-03 14:33:19 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-07-22 20:52:05 -04:00
|
|
|
* Crops an image to a given size.
|
2007-11-03 14:33:19 -04:00
|
|
|
*
|
2008-10-10 14:21:16 -04:00
|
|
|
* @since 2.1.0
|
2007-11-03 14:33:19 -04:00
|
|
|
*
|
2021-01-05 12:16:11 -05: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-01 21:01:59 -05:00
|
|
|
* @return string|WP_Error New filepath on success, WP_Error on failure.
|
2007-11-03 14:33:19 -04:00
|
|
|
*/
|
2012-11-10 15:42:27 -05: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-17 19:54:04 -05:00
|
|
|
if ( is_numeric( $src ) ) { // Handle int as attachment ID.
|
2012-11-10 15:42:27 -05:00
|
|
|
$src_file = get_attached_file( $src );
|
|
|
|
|
2012-04-06 16:47:24 -04:00
|
|
|
if ( ! file_exists( $src_file ) ) {
|
2016-03-12 07:39:27 -05:00
|
|
|
// If the file doesn't exist, attempt a URL fopen on the src link.
|
2012-04-06 16:47:24 -04:00
|
|
|
// This can occur with certain file replication plugins.
|
2012-11-12 19:03:55 -05:00
|
|
|
$src = _load_image_to_edit_path( $src, 'full' );
|
|
|
|
} else {
|
|
|
|
$src = $src_file;
|
2012-04-06 16:47:24 -04:00
|
|
|
}
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2012-11-22 04:52:16 -05:00
|
|
|
$editor = wp_get_image_editor( $src );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( is_wp_error( $editor ) ) {
|
2012-10-11 14:59:41 -04:00
|
|
|
return $editor;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2012-10-11 14:59:41 -04:00
|
|
|
$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( is_wp_error( $src ) ) {
|
2012-10-01 16:59:06 -04:00
|
|
|
return $src;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $dst_file ) {
|
2019-03-01 15:58:52 -05:00
|
|
|
$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2014-07-17 05:14:16 -04:00
|
|
|
/*
|
|
|
|
* The directory containing the original file may no longer exist when
|
|
|
|
* using a replication plugin.
|
|
|
|
*/
|
2012-04-06 16:47:24 -04:00
|
|
|
wp_mkdir_p( dirname( $dst_file ) );
|
|
|
|
|
2019-03-01 15:58:52 -05:00
|
|
|
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) );
|
2012-05-14 13:53:03 -04:00
|
|
|
|
2012-10-01 16:59:06 -04:00
|
|
|
$result = $editor->save( $dst_file );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( is_wp_error( $result ) ) {
|
2013-02-01 21:01:59 -05:00
|
|
|
return $result;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2013-02-01 21:01:59 -05:00
|
|
|
|
2022-04-27 01:21:08 -04:00
|
|
|
if ( ! empty( $result['path'] ) ) {
|
|
|
|
return $result['path'];
|
|
|
|
}
|
|
|
|
|
2012-10-01 16:59:06 -04:00
|
|
|
return $dst_file;
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
2007-11-03 14:33:19 -04:00
|
|
|
/**
|
2022-09-06 17:13:12 -04: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 14:33:19 -04:00
|
|
|
*
|
2019-06-14 21:02:52 -04:00
|
|
|
* Registered sub-sizes that are larger than the image are skipped.
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
2019-06-14 21:02:52 -04:00
|
|
|
* @since 5.3.0
|
|
|
|
*
|
2022-09-06 17:13:12 -04:00
|
|
|
* @param int $attachment_id The image attachment post ID.
|
2021-12-07 07:20:02 -05:00
|
|
|
* @return array[] Associative array of arrays of image sub-size information for
|
|
|
|
* missing image sizes, keyed by image size name.
|
2007-11-03 14:33:19 -04:00
|
|
|
*/
|
2022-09-06 17:13:12 -04:00
|
|
|
function wp_get_missing_image_subsizes( $attachment_id ) {
|
2019-06-14 21:02:52 -04:00
|
|
|
if ( ! wp_attachment_is_image( $attachment_id ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
2007-05-25 03:16:21 -04:00
|
|
|
|
2019-06-14 21:02:52 -04: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 03:28:32 -04:00
|
|
|
|
2019-06-14 21:02:52 -04:00
|
|
|
// Meta error?
|
|
|
|
if ( empty( $image_meta ) ) {
|
2019-06-14 21:32:53 -04:00
|
|
|
return $registered_sizes;
|
2019-06-14 21:02:52 -04:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:51:02 -05: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 11:53:04 -05:00
|
|
|
$imagesize = wp_getimagesize( $image_file );
|
2019-11-07 13:51:02 -05: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-14 21:02:52 -04: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-17 19:54:04 -05: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 17:13:12 -04:00
|
|
|
$missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] );
|
2019-06-14 21:02:52 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filters the array of missing image sub-sizes for an uploaded image.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
|
|
|
*
|
2021-12-07 07:20:02 -05: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-14 21:02:52 -04:00
|
|
|
*/
|
2022-09-06 17:13:12 -04:00
|
|
|
return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id );
|
2019-06-14 21:02:52 -04: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-03 21:11:54 -04: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-14 21:02:52 -04:00
|
|
|
*/
|
|
|
|
function wp_update_image_subsizes( $attachment_id ) {
|
2019-09-03 21:11:54 -04:00
|
|
|
$image_meta = wp_get_attachment_metadata( $attachment_id );
|
2019-09-06 21:07:55 -04:00
|
|
|
$image_file = wp_get_original_image_path( $attachment_id );
|
2019-09-03 21:11:54 -04:00
|
|
|
|
|
|
|
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
|
|
|
|
// Previously failed upload?
|
|
|
|
// If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
|
|
|
|
if ( ! empty( $image_file ) ) {
|
2019-10-30 17:10:04 -04:00
|
|
|
$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
|
2019-09-03 21:11:54 -04:00
|
|
|
} else {
|
|
|
|
return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
|
|
|
|
}
|
2019-10-30 17:10:04 -04:00
|
|
|
} else {
|
2022-09-06 17:13:12 -04:00
|
|
|
$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
|
2019-09-03 21:11:54 -04:00
|
|
|
|
2022-09-06 17:13:12 -04:00
|
|
|
if ( empty( $missing_sizes ) ) {
|
|
|
|
return $image_meta;
|
2019-10-30 17:10:04 -04:00
|
|
|
}
|
2019-06-14 21:02:52 -04:00
|
|
|
|
2022-09-06 17:13:12 -04:00
|
|
|
// This also updates the image meta.
|
|
|
|
$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
|
2019-06-14 21:02:52 -04:00
|
|
|
}
|
|
|
|
|
2019-10-30 17:10:04 -04: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-14 21:02:52 -04: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 14:21:57 -04:00
|
|
|
/**
|
|
|
|
* Updates the attached file and image meta data when the original image was edited.
|
|
|
|
*
|
|
|
|
* @since 5.3.0
|
2022-06-21 07:18:17 -04: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 14:21:57 -04:00
|
|
|
* @access private
|
|
|
|
*
|
2019-09-30 21:19:57 -04: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 14:21:57 -04: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 08:10:02 -05:00
|
|
|
// Add image file size.
|
|
|
|
$image_meta['filesize'] = wp_filesize( $new_file );
|
|
|
|
|
2022-06-21 07:18:17 -04: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 14:21:57 -04:00
|
|
|
return $image_meta;
|
|
|
|
}
|
|
|
|
|
2019-06-14 21:02:52 -04:00
|
|
|
/**
|
2022-09-06 17:13:12 -04:00
|
|
|
* Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
|
2019-06-14 21:02:52 -04: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 09:54:00 -05:00
|
|
|
* @param int $attachment_id Attachment ID to process.
|
2019-09-06 21:07:55 -04:00
|
|
|
* @return array The image attachment meta data.
|
2019-06-14 21:02:52 -04:00
|
|
|
*/
|
2019-09-06 21:07:55 -04:00
|
|
|
function wp_create_image_subsizes( $file, $attachment_id ) {
|
2021-02-02 11:53:04 -05:00
|
|
|
$imagesize = wp_getimagesize( $file );
|
2019-09-06 21:07:55 -04:00
|
|
|
|
|
|
|
if ( empty( $imagesize ) ) {
|
|
|
|
// File is not an image.
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2020-01-17 19:54:04 -05:00
|
|
|
// Default image meta.
|
2019-09-06 21:07:55 -04: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 08:10:02 -05:00
|
|
|
'width' => $imagesize[0],
|
|
|
|
'height' => $imagesize[1],
|
|
|
|
'file' => _wp_relative_upload_path( $file ),
|
|
|
|
'filesize' => wp_filesize( $file ),
|
|
|
|
'sizes' => array(),
|
2019-09-06 21:07:55 -04: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 13:03:31 -05: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 14:03:13 -04: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 17:13:12 -04:00
|
|
|
if ( 'image/png' !== $imagesize['mime'] ) {
|
|
|
|
|
2019-12-01 13:26:05 -05: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 );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// If the original image's dimensions are over the threshold,
|
|
|
|
// scale the image and use it as the "full" size.
|
2022-09-06 17:13:12 -04: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-28 19:45:18 -05:00
|
|
|
// Resize the image.
|
2019-12-01 13:26:05 -05:00
|
|
|
$resized = $editor->resize( $threshold, $threshold );
|
2022-09-06 17:13:12 -04: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 14:21:57 -04:00
|
|
|
|
2019-12-01 13:26:05 -05:00
|
|
|
// If there is EXIF data, rotate according to EXIF Orientation.
|
|
|
|
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
|
2022-09-06 17:13:12 -04:00
|
|
|
$resized = $editor->maybe_exif_rotate();
|
|
|
|
$rotated = $resized;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_wp_error( $resized ) ) {
|
|
|
|
// 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).
|
|
|
|
$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 14:21:57 -04:00
|
|
|
}
|
2020-02-09 11:55:09 -05:00
|
|
|
} elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
|
2019-12-01 13:26:05 -05:00
|
|
|
// Rotate the whole original image if there is EXIF data and "orientation" is not 1.
|
2022-09-06 17:13:12 -04: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 13:26:05 -05:00
|
|
|
$rotated = $editor->maybe_exif_rotate();
|
2016-08-22 00:37:28 -04:00
|
|
|
|
2022-09-06 17:13:12 -04:00
|
|
|
if ( true === $rotated ) {
|
|
|
|
// Append `-rotated` to the image file name.
|
|
|
|
$saved = $editor->save( $editor->generate_filename( 'rotated' ) );
|
2019-11-04 12:07:03 -05:00
|
|
|
|
2022-09-06 17:13:12 -04:00
|
|
|
if ( ! is_wp_error( $saved ) ) {
|
|
|
|
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
|
2019-06-14 21:02:52 -04:00
|
|
|
|
2022-09-06 17:13:12 -04: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 14:03:13 -04:00
|
|
|
}
|
2019-06-14 21:02:52 -04:00
|
|
|
|
2022-09-06 17:13:12 -04: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 14:03:13 -04:00
|
|
|
|
2022-09-06 17:13:12 -04: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-14 21:02:52 -04: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 17:13:12 -04: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-14 21:02:52 -04:00
|
|
|
* @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
|
|
|
|
*/
|
2022-09-06 17:13:12 -04:00
|
|
|
function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
|
2019-09-06 21:07:55 -04:00
|
|
|
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
|
|
|
|
// Not an image attachment.
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2019-06-14 21:02:52 -04: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-17 19:54:04 -05: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-14 21:02:52 -04:00
|
|
|
if ( array_key_exists( $size_name, $new_sizes ) ) {
|
2022-09-06 17:13:12 -04:00
|
|
|
unset( $new_sizes[ $size_name ] );
|
2016-08-22 00:37:28 -04:00
|
|
|
}
|
2009-12-08 16:08:19 -05:00
|
|
|
}
|
2019-06-14 21:02:52 -04:00
|
|
|
} else {
|
|
|
|
$image_meta['sizes'] = array();
|
|
|
|
}
|
2009-12-08 16:08:19 -05:00
|
|
|
|
2019-09-06 21:07:55 -04:00
|
|
|
if ( empty( $new_sizes ) ) {
|
|
|
|
// Nothing to do...
|
|
|
|
return $image_meta;
|
|
|
|
}
|
2019-07-16 17:48:56 -04:00
|
|
|
|
2020-01-17 19:54:04 -05: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-06 21:07:55 -04:00
|
|
|
$priority = array(
|
|
|
|
'medium' => null,
|
|
|
|
'large' => null,
|
|
|
|
'thumbnail' => null,
|
|
|
|
'medium_large' => null,
|
|
|
|
);
|
2019-07-16 17:48:56 -04:00
|
|
|
|
2019-09-06 21:07:55 -04:00
|
|
|
$new_sizes = array_filter( array_merge( $priority, $new_sizes ) );
|
2019-06-14 21:02:52 -04:00
|
|
|
|
2022-09-06 17:13:12 -04:00
|
|
|
$editor = wp_get_image_editor( $file );
|
2008-08-09 01:36:14 -04:00
|
|
|
|
2019-09-06 21:07:55 -04: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 14:21:57 -04: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-28 19:45:18 -05: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 14:21:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:07:55 -04:00
|
|
|
if ( method_exists( $editor, 'make_subsize' ) ) {
|
|
|
|
foreach ( $new_sizes as $new_size_name => $new_size_data ) {
|
Media: Generate WebP only for certain registered image sizes.
The existing filter `image_editor_output_format` receives an additional parameter `$size_name` which is populated whenever it controls the output format for a specific registered image size to create. Otherwise, it remains empty. In order to achieve this, a low level change has been added in bringing a new `$size_name` class property to the `WP_Image_Editor` base class, which is introduced in a backward compatible way that will not cause conflicts with custom implementations.
This parameter is then used in new logic inside the `wp_default_image_output_mapping()` callback function for the filter, controlling whether `image/jpeg` should map to `image/webp` output or not. By default, this is enabled for all WordPress core image sizes by default, and this list can be modified using a new `wp_image_sizes_with_additional_mime_type_support` filter, e.g. to remove core sizes or add custom sizes.
The customization per image size may be further enhanced by providing a more declarative API via a new parameter on the `add_image_size()` function.
Props eugenemanuilov, flixos90, adamsilverstein, joegrainger.
Fixes #56526.
See #55443, #56288.
Built from https://develop.svn.wordpress.org/trunk@54097
git-svn-id: http://core.svn.wordpress.org/trunk@53656 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-07 17:45:14 -04:00
|
|
|
// Include size name in the data.
|
|
|
|
$new_size_data['name'] = $new_size_name;
|
|
|
|
|
2019-09-06 21:07:55 -04:00
|
|
|
$new_size_meta = $editor->make_subsize( $new_size_data );
|
|
|
|
|
|
|
|
if ( is_wp_error( $new_size_meta ) ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// TODO: Log errors.
|
2019-06-14 21:02:52 -04:00
|
|
|
} else {
|
2019-09-06 21:07:55 -04:00
|
|
|
// Save the size meta value.
|
2022-09-06 17:13:12 -04:00
|
|
|
$image_meta['sizes'][ $new_size_name ] = $new_size_meta;
|
2019-10-14 16:05:01 -04:00
|
|
|
wp_update_attachment_metadata( $attachment_id, $image_meta );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2019-09-06 21:07:55 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Fall back to `$editor->multi_resize()`.
|
|
|
|
$created_sizes = $editor->multi_resize( $new_sizes );
|
|
|
|
|
|
|
|
if ( ! empty( $created_sizes ) ) {
|
2022-09-06 17:13:12 -04:00
|
|
|
$image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
|
2019-09-06 21:07:55 -04:00
|
|
|
wp_update_attachment_metadata( $attachment_id, $image_meta );
|
2012-11-14 09:26:52 -05:00
|
|
|
}
|
2019-06-14 21:02:52 -04:00
|
|
|
}
|
2008-08-09 01:36:14 -04:00
|
|
|
|
2019-06-14 21:02:52 -04:00
|
|
|
return $image_meta;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Generates attachment meta data and create image sub-sizes for images.
|
2019-06-14 21:02:52 -04:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2022-06-21 07:18:17 -04:00
|
|
|
* @since 6.0.0 The `$filesize` value was added to the returned array.
|
2019-06-14 21:02:52 -04:00
|
|
|
*
|
2021-12-12 09:54:00 -05:00
|
|
|
* @param int $attachment_id Attachment ID to process.
|
|
|
|
* @param string $file Filepath of the attached image.
|
2021-06-15 12:52:56 -04:00
|
|
|
* @return array Metadata for attachment.
|
2019-06-14 21:02:52 -04: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-06 21:07:55 -04:00
|
|
|
$metadata = wp_create_image_subsizes( $file, $attachment_id );
|
2015-03-06 15:26:26 -05:00
|
|
|
} elseif ( wp_attachment_is( 'video', $attachment ) ) {
|
2013-03-21 00:55:42 -04:00
|
|
|
$metadata = wp_read_video_metadata( $file );
|
2017-11-30 18:11:00 -05:00
|
|
|
$support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' );
|
2015-03-06 15:26:26 -05:00
|
|
|
} elseif ( wp_attachment_is( 'audio', $attachment ) ) {
|
2013-03-21 00:55:42 -04:00
|
|
|
$metadata = wp_read_audio_metadata( $file );
|
2017-11-30 18:11:00 -05:00
|
|
|
$support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' );
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
2008-09-16 20:40:10 -04:00
|
|
|
|
2021-06-15 15:10:58 -04: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 12:52:56 -04:00
|
|
|
if ( ! is_array( $metadata ) ) {
|
|
|
|
$metadata = array();
|
|
|
|
}
|
|
|
|
|
2013-03-21 00:55:42 -04:00
|
|
|
if ( $support && ! empty( $metadata['image']['data'] ) ) {
|
2014-07-17 05:14:16 -04:00
|
|
|
// Check for existing cover.
|
2017-11-30 18:11:00 -05: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 16:54:15 -04:00
|
|
|
$exists = reset( $posts );
|
|
|
|
|
|
|
|
if ( ! empty( $exists ) ) {
|
|
|
|
update_post_meta( $attachment_id, '_thumbnail_id', $exists );
|
|
|
|
} else {
|
|
|
|
$ext = '.jpg';
|
|
|
|
switch ( $metadata['image']['mime'] ) {
|
2017-11-30 18:11:00 -05: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 10:44:58 -04:00
|
|
|
case 'image/webp':
|
|
|
|
$ext = '.webp';
|
|
|
|
break;
|
2014-03-30 16:54:15 -04:00
|
|
|
}
|
2019-03-01 15:58:52 -05:00
|
|
|
$basename = str_replace( '.', '-', wp_basename( $file ) ) . '-image' . $ext;
|
2014-03-30 16:54:15 -04:00
|
|
|
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
|
|
|
|
if ( false === $uploaded['error'] ) {
|
|
|
|
$image_attachment = array(
|
|
|
|
'post_mime_type' => $metadata['image']['mime'],
|
2017-11-30 18:11:00 -05:00
|
|
|
'post_type' => 'attachment',
|
|
|
|
'post_content' => '',
|
2014-03-30 16:54:15 -04:00
|
|
|
);
|
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters the parameters for the attachment thumbnail creation.
|
2014-03-30 16:54:15 -04:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
|
|
|
* @param array $image_attachment An array of parameters to create the thumbnail.
|
2014-04-07 16:10:16 -04:00
|
|
|
* @param array $metadata Current attachment metadata.
|
2020-03-16 14:40:07 -04:00
|
|
|
* @param array $uploaded {
|
|
|
|
* Information about the newly-uploaded file.
|
|
|
|
*
|
2022-09-06 17:13:12 -04: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 14:40:07 -04:00
|
|
|
* }
|
2014-03-30 16:54:15 -04: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 00:55:42 -04:00
|
|
|
}
|
2019-01-11 01:40:50 -05: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 03:28:32 -04:00
|
|
|
$fallback_sizes = array(
|
|
|
|
'thumbnail',
|
|
|
|
'medium',
|
|
|
|
'large',
|
|
|
|
);
|
|
|
|
|
2016-11-15 08:26:30 -05:00
|
|
|
/**
|
|
|
|
* Filters the image sizes generated for non-image mime types.
|
|
|
|
*
|
|
|
|
* @since 4.7.0
|
|
|
|
*
|
2019-11-03 17:23:01 -05:00
|
|
|
* @param string[] $fallback_sizes An array of image size names.
|
|
|
|
* @param array $metadata Current attachment metadata.
|
2016-11-15 08:26:30 -05:00
|
|
|
*/
|
|
|
|
$fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
|
|
|
|
|
2019-06-14 21:32:53 -04:00
|
|
|
$registered_sizes = wp_get_registered_image_subsizes();
|
|
|
|
$merged_sizes = array_intersect_key( $registered_sizes, array_flip( $fallback_sizes ) );
|
2016-12-16 15:30:42 -05:00
|
|
|
|
2019-06-14 21:02:52 -04: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 03:28:32 -04:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:26:30 -05:00
|
|
|
// Only load PDFs in an image editor if we're processing sizes.
|
2019-06-14 21:02:52 -04:00
|
|
|
if ( ! empty( $merged_sizes ) ) {
|
2016-11-15 08:26:30 -05:00
|
|
|
$editor = wp_get_image_editor( $file );
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
if ( ! is_wp_error( $editor ) ) { // No support for this type of file.
|
2017-02-27 10:39:47 -05: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-11-30 18:11:00 -05:00
|
|
|
$dirname = dirname( $file ) . '/';
|
|
|
|
$ext = '.' . pathinfo( $file, PATHINFO_EXTENSION );
|
2017-02-27 10:39:47 -05:00
|
|
|
$preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' );
|
|
|
|
|
|
|
|
$uploaded = $editor->save( $preview_file, 'image/jpeg' );
|
2016-11-15 08:26:30 -05: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 03:28:32 -04:00
|
|
|
|
2016-11-15 08:26:30 -05:00
|
|
|
// Resize based on the full size image, rather than the source.
|
|
|
|
if ( ! is_wp_error( $uploaded ) ) {
|
2019-10-11 03:54:03 -04:00
|
|
|
$image_file = $uploaded['path'];
|
2016-11-15 08:26:30 -05: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 03:28:32 -04:00
|
|
|
|
2019-10-11 03:54:03 -04: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 17:13:12 -04: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 03:28:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 10:39:10 -04:00
|
|
|
|
2014-07-17 05:14:16 -04:00
|
|
|
// Remove the blob of binary data from the array.
|
2021-06-15 12:52:56 -04:00
|
|
|
unset( $metadata['image']['data'] );
|
2013-03-21 00:55:42 -04: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 08:10:02 -05: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 01:27:13 -05:00
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters the generated attachment meta data.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2019-10-30 20:13:01 -04:00
|
|
|
* @since 5.3.0 The `$context` parameter was added.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
2019-10-30 17:10:04 -04: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 01:27:13 -05:00
|
|
|
*/
|
2019-10-30 17:10:04 -04:00
|
|
|
return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' );
|
2007-05-25 03:16:21 -04:00
|
|
|
}
|
|
|
|
|
2008-09-16 20:40:10 -04:00
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Converts a fraction string to a decimal.
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
2008-10-10 14:21:16 -04:00
|
|
|
* @since 2.5.0
|
2008-09-16 20:40:10 -04: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 14:36:05 -05:00
|
|
|
* @param string $str Fraction string.
|
|
|
|
* @return int|float Returns calculated fraction or integer 0 on invalid input.
|
2008-09-16 20:40:10 -04:00
|
|
|
*/
|
2017-11-30 18:11:00 -05: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 14:36:05 -05: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 01:45:58 -04:00
|
|
|
}
|
|
|
|
|
2020-02-13 19:07:07 -05: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 14:36:05 -05: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.
|
|
|
|
if ( 0 == $denominator ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- Deliberate loose comparison.
|
|
|
|
return 0;
|
2017-11-30 18:11:00 -05: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 14:36:05 -05:00
|
|
|
|
|
|
|
return $numerator / $denominator;
|
2007-11-04 19:01:26 -05:00
|
|
|
}
|
|
|
|
|
2008-09-16 20:40:10 -04:00
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Converts the exif date format to a unix timestamp.
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
2008-10-10 14:21:16 -04:00
|
|
|
* @since 2.5.0
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
2021-12-01 07:17:00 -05: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-16 20:40:10 -04:00
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
function wp_exif_date2ts( $str ) {
|
2019-07-09 01:45:58 -04:00
|
|
|
list( $date, $time ) = explode( ' ', trim( $str ) );
|
|
|
|
list( $y, $m, $d ) = explode( ':', $date );
|
2007-11-04 19:01:26 -05:00
|
|
|
|
|
|
|
return strtotime( "{$y}-{$m}-{$d} {$time}" );
|
|
|
|
}
|
|
|
|
|
2008-09-16 20:40:10 -04:00
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Gets extended image metadata, exif or iptc as available.
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
2008-10-10 14:21:16 -04: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-16 20:40:10 -04:00
|
|
|
*
|
|
|
|
* @param string $file
|
2021-01-04 12:18:04 -05:00
|
|
|
* @return array|false Image metadata array on success, false on failure.
|
2008-09-16 20:40:10 -04:00
|
|
|
*/
|
2007-11-04 19:01:26 -05:00
|
|
|
function wp_read_image_metadata( $file ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! file_exists( $file ) ) {
|
2007-11-04 19:01:26 -05:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2007-11-04 19:01:26 -05:00
|
|
|
|
2021-02-02 11:53:04 -05:00
|
|
|
list( , , $image_type ) = wp_getimagesize( $file );
|
2007-12-20 17:18:28 -05:00
|
|
|
|
2014-07-17 05:14:16 -04: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-04 19:01:26 -05:00
|
|
|
$meta = array(
|
2017-11-30 18:11:00 -05:00
|
|
|
'aperture' => 0,
|
|
|
|
'credit' => '',
|
|
|
|
'camera' => '',
|
|
|
|
'caption' => '',
|
2007-11-04 19:01:26 -05:00
|
|
|
'created_timestamp' => 0,
|
2017-11-30 18:11:00 -05:00
|
|
|
'copyright' => '',
|
|
|
|
'focal_length' => 0,
|
|
|
|
'iso' => 0,
|
|
|
|
'shutter_speed' => 0,
|
|
|
|
'title' => '',
|
|
|
|
'orientation' => 0,
|
|
|
|
'keywords' => array(),
|
2007-11-04 19:01:26 -05:00
|
|
|
);
|
|
|
|
|
2015-09-22 00:49:24 -04:00
|
|
|
$iptc = array();
|
2021-03-18 20:01:03 -04:00
|
|
|
$info = array();
|
2014-07-17 05:14:16 -04:00
|
|
|
/*
|
|
|
|
* Read IPTC first, since it might contain data not available in exif such
|
|
|
|
* as caption, description etc.
|
|
|
|
*/
|
2010-02-20 07:09:30 -05:00
|
|
|
if ( is_callable( 'iptcparse' ) ) {
|
2021-02-02 11:53:04 -05:00
|
|
|
wp_getimagesize( $file, $info );
|
2010-02-20 04:17:34 -05:00
|
|
|
|
2010-02-20 07:09:30 -05:00
|
|
|
if ( ! empty( $info['APP13'] ) ) {
|
2021-05-05 14:47:57 -04: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 11:53:04 -05:00
|
|
|
) {
|
|
|
|
$iptc = iptcparse( $info['APP13'] );
|
|
|
|
} else {
|
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480
|
|
|
|
$iptc = @iptcparse( $info['APP13'] );
|
|
|
|
}
|
2010-02-20 04:17:34 -05:00
|
|
|
|
2022-04-28 07:00:10 -04:00
|
|
|
if ( ! is_array( $iptc ) ) {
|
|
|
|
$iptc = array();
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Headline, "A brief synopsis of the caption".
|
2014-07-17 05:14:16 -04:00
|
|
|
if ( ! empty( $iptc['2#105'][0] ) ) {
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['title'] = trim( $iptc['2#105'][0] );
|
2017-11-30 18:11:00 -05:00
|
|
|
/*
|
|
|
|
* Title, "Many use the Title field to store the filename of the image,
|
2020-01-28 19:45:18 -05:00
|
|
|
* though the field may be used in many ways".
|
2017-11-30 18:11:00 -05:00
|
|
|
*/
|
2014-07-17 05:14:16 -04:00
|
|
|
} elseif ( ! empty( $iptc['2#005'][0] ) ) {
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['title'] = trim( $iptc['2#005'][0] );
|
2014-07-17 05:14:16 -04:00
|
|
|
}
|
2010-02-20 04:17:34 -05:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
if ( ! empty( $iptc['2#120'][0] ) ) { // Description / legacy caption.
|
2012-09-18 19:38:25 -04:00
|
|
|
$caption = trim( $iptc['2#120'][0] );
|
2014-06-23 18:21:15 -04:00
|
|
|
|
2015-03-10 01:07:28 -04:00
|
|
|
mbstring_binary_safe_encoding();
|
|
|
|
$caption_length = strlen( $caption );
|
|
|
|
reset_mbstring_encoding();
|
|
|
|
|
|
|
|
if ( empty( $meta['title'] ) && $caption_length < 80 ) {
|
2010-02-20 04:17:34 -05:00
|
|
|
// Assume the title is stored in 2:120 if it's short.
|
2015-03-10 01:07:28 -04:00
|
|
|
$meta['title'] = $caption;
|
2010-02-20 04:17:34 -05:00
|
|
|
}
|
2015-03-10 01:07:28 -04:00
|
|
|
|
|
|
|
$meta['caption'] = $caption;
|
2010-02-20 04:17:34 -05:00
|
|
|
}
|
|
|
|
|
2020-01-17 19:54:04 -05:00
|
|
|
if ( ! empty( $iptc['2#110'][0] ) ) { // Credit.
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['credit'] = trim( $iptc['2#110'][0] );
|
2020-01-17 19:54:04 -05:00
|
|
|
} elseif ( ! empty( $iptc['2#080'][0] ) ) { // Creator / legacy byline.
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['credit'] = trim( $iptc['2#080'][0] );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-02-20 04:17:34 -05:00
|
|
|
|
2020-01-17 19:54:04 -05:00
|
|
|
if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) { // Created date and time.
|
2010-02-20 07:09:30 -05:00
|
|
|
$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-02-20 04:17:34 -05:00
|
|
|
|
2020-01-17 19:54:04 -05:00
|
|
|
if ( ! empty( $iptc['2#116'][0] ) ) { // Copyright.
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['copyright'] = trim( $iptc['2#116'][0] );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-09-22 00:19:24 -04:00
|
|
|
|
2020-01-17 19:54:04 -05:00
|
|
|
if ( ! empty( $iptc['2#025'][0] ) ) { // Keywords array.
|
2015-09-22 00:19:24 -04:00
|
|
|
$meta['keywords'] = array_values( $iptc['2#025'] );
|
|
|
|
}
|
2017-11-26 18:57:55 -05:00
|
|
|
}
|
2007-11-04 19:01:26 -05:00
|
|
|
}
|
|
|
|
|
2018-08-16 21:51:36 -04:00
|
|
|
$exif = array();
|
2018-03-25 16:45:29 -04:00
|
|
|
|
2014-02-20 01:27:13 -05:00
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters the image types to check for exif data.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
2021-12-07 07:20:02 -05: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 01:27:13 -05:00
|
|
|
*/
|
2018-03-25 16:41:29 -04:00
|
|
|
$exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) );
|
|
|
|
|
2019-06-14 21:02:52 -04:00
|
|
|
if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) {
|
2021-05-05 14:47:57 -04: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 11:53:04 -05:00
|
|
|
) {
|
|
|
|
$exif = exif_read_data( $file );
|
|
|
|
} else {
|
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silencing notice and warning is intentional. See https://core.trac.wordpress.org/ticket/42480
|
|
|
|
$exif = @exif_read_data( $file );
|
|
|
|
}
|
2010-02-20 04:17:34 -05:00
|
|
|
|
2022-04-28 07:00:10 -04:00
|
|
|
if ( ! is_array( $exif ) ) {
|
|
|
|
$exif = array();
|
|
|
|
}
|
|
|
|
|
2010-02-20 04:17:34 -05:00
|
|
|
if ( ! empty( $exif['ImageDescription'] ) ) {
|
2014-06-23 18:21:15 -04:00
|
|
|
mbstring_binary_safe_encoding();
|
|
|
|
$description_length = strlen( $exif['ImageDescription'] );
|
|
|
|
reset_mbstring_encoding();
|
|
|
|
|
|
|
|
if ( empty( $meta['title'] ) && $description_length < 80 ) {
|
2020-01-17 19:54:04 -05:00
|
|
|
// Assume the title is stored in ImageDescription.
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['title'] = trim( $exif['ImageDescription'] );
|
2015-03-10 01:07:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) {
|
|
|
|
$meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $meta['caption'] ) ) {
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['caption'] = trim( $exif['ImageDescription'] );
|
2010-02-20 04:17:34 -05:00
|
|
|
}
|
2015-03-10 01:07:28 -04:00
|
|
|
} elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) {
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['caption'] = trim( $exif['Comments'] );
|
2010-02-20 04:17:34 -05:00
|
|
|
}
|
|
|
|
|
2014-05-11 01:54:15 -04:00
|
|
|
if ( empty( $meta['credit'] ) ) {
|
|
|
|
if ( ! empty( $exif['Artist'] ) ) {
|
|
|
|
$meta['credit'] = trim( $exif['Artist'] );
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( ! empty( $exif['Author'] ) ) {
|
2014-05-11 01:54:15 -04:00
|
|
|
$meta['credit'] = trim( $exif['Author'] );
|
|
|
|
}
|
|
|
|
}
|
2010-02-20 04:17:34 -05:00
|
|
|
|
2014-05-11 01:54:15 -04:00
|
|
|
if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) {
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['copyright'] = trim( $exif['Copyright'] );
|
2014-05-11 01:54:15 -04: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 14:36:05 -05:00
|
|
|
if ( ! empty( $exif['FNumber'] ) && is_scalar( $exif['FNumber'] ) ) {
|
2007-11-04 19:01:26 -05:00
|
|
|
$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
|
2014-05-11 01:54:15 -04:00
|
|
|
}
|
|
|
|
if ( ! empty( $exif['Model'] ) ) {
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['camera'] = trim( $exif['Model'] );
|
2014-05-11 01:54:15 -04: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 14:36:05 -05:00
|
|
|
$meta['focal_length'] = (string) $exif['FocalLength'];
|
|
|
|
if ( is_scalar( $exif['FocalLength'] ) ) {
|
|
|
|
$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] );
|
|
|
|
}
|
2014-05-11 01:54:15 -04:00
|
|
|
}
|
|
|
|
if ( ! empty( $exif['ISOSpeedRatings'] ) ) {
|
2012-06-01 21:39:00 -04:00
|
|
|
$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings'];
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta['iso'] = trim( $meta['iso'] );
|
2012-06-01 21:39:00 -04:00
|
|
|
}
|
2014-05-11 01:54:15 -04: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 14:36:05 -05:00
|
|
|
$meta['shutter_speed'] = (string) $exif['ExposureTime'];
|
|
|
|
if ( is_scalar( $exif['ExposureTime'] ) ) {
|
|
|
|
$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] );
|
|
|
|
}
|
2014-05-11 01:54:15 -04:00
|
|
|
}
|
2014-07-24 18:14:16 -04:00
|
|
|
if ( ! empty( $exif['Orientation'] ) ) {
|
|
|
|
$meta['orientation'] = $exif['Orientation'];
|
|
|
|
}
|
2007-11-04 19:01:26 -05:00
|
|
|
}
|
|
|
|
|
2012-09-18 19:38:25 -04:00
|
|
|
foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) {
|
2014-05-11 01:54:15 -04:00
|
|
|
if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) {
|
2012-09-18 19:38:25 -04:00
|
|
|
$meta[ $key ] = utf8_encode( $meta[ $key ] );
|
2014-05-11 01:54:15 -04:00
|
|
|
}
|
2012-09-18 19:38:25 -04:00
|
|
|
}
|
|
|
|
|
2016-02-01 09:53:27 -05:00
|
|
|
foreach ( $meta['keywords'] as $key => $keyword ) {
|
|
|
|
if ( ! seems_utf8( $keyword ) ) {
|
|
|
|
$meta['keywords'][ $key ] = utf8_encode( $keyword );
|
2014-11-20 10:25:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 09:53:27 -05:00
|
|
|
$meta = wp_kses_post_deep( $meta );
|
|
|
|
|
2014-02-20 01:27:13 -05:00
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters the array of meta data read from an image's exif data.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
|
|
|
* @since 2.5.0
|
2015-09-22 00:19:24 -04:00
|
|
|
* @since 4.4.0 The `$iptc` parameter was added.
|
2018-03-25 16:45:29 -04:00
|
|
|
* @since 5.0.0 The `$exif` parameter was added.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
2018-03-25 16:41:29 -04: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 16:45:29 -04:00
|
|
|
* @param array $exif EXIF data.
|
2014-02-20 01:27:13 -05:00
|
|
|
*/
|
2018-03-25 16:45:29 -04:00
|
|
|
return apply_filters( 'wp_read_image_metadata', $meta, $file, $image_type, $iptc, $exif );
|
2007-11-04 19:01:26 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-09-16 20:40:10 -04:00
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Validates that file is an image.
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
2008-10-10 14:21:16 -04:00
|
|
|
* @since 2.5.0
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
|
|
|
* @param string $path File path to test if valid image.
|
|
|
|
* @return bool True if valid image, false if not valid image.
|
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
function file_is_valid_image( $path ) {
|
2021-02-02 11:53:04 -05:00
|
|
|
$size = wp_getimagesize( $path );
|
2017-11-30 18:11:00 -05:00
|
|
|
return ! empty( $size );
|
2008-03-02 23:17:37 -05:00
|
|
|
}
|
|
|
|
|
2008-09-16 20:40:10 -04:00
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Validates that file is suitable for displaying within a web page.
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
2008-10-10 14:21:16 -04:00
|
|
|
* @since 2.5.0
|
2008-09-16 20:40:10 -04:00
|
|
|
*
|
|
|
|
* @param string $path File path to test.
|
|
|
|
* @return bool True if suitable, false if not suitable.
|
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
function file_is_displayable_image( $path ) {
|
2021-12-31 08:55:03 -05:00
|
|
|
$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO, IMAGETYPE_WEBP );
|
2018-03-04 20:03:31 -05:00
|
|
|
|
2021-02-02 11:53:04 -05:00
|
|
|
$info = wp_getimagesize( $path );
|
2014-05-27 09:25:14 -04:00
|
|
|
if ( empty( $info ) ) {
|
2008-03-02 23:17:37 -05:00
|
|
|
$result = false;
|
2019-06-14 21:02:52 -04:00
|
|
|
} elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) {
|
2008-03-02 23:17:37 -05:00
|
|
|
$result = false;
|
2014-05-27 09:25:14 -04:00
|
|
|
} else {
|
2008-03-02 23:17:37 -05:00
|
|
|
$result = true;
|
2014-05-27 09:25:14 -04:00
|
|
|
}
|
2008-08-09 01:36:14 -04:00
|
|
|
|
2014-02-20 01:27:13 -05:00
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters whether the current image is displayable in the browser.
|
2014-02-20 01:27:13 -05: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-02 23:17:37 -05:00
|
|
|
}
|
2012-04-06 16:47:24 -04:00
|
|
|
|
2012-05-16 13:47:55 -04:00
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Loads an image resource for editing.
|
2012-05-16 13:47:55 -04:00
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
*
|
2020-09-20 12:23:07 -04: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 09:33:09 -04:00
|
|
|
* @return resource|GdImage|false The resulting image resource or GdImage instance on success,
|
|
|
|
* false on failure.
|
2012-05-16 13:47:55 -04:00
|
|
|
*/
|
|
|
|
function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {
|
|
|
|
$filepath = _load_image_to_edit_path( $attachment_id, $size );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( empty( $filepath ) ) {
|
2012-04-06 16:47:24 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-04-06 16:47:24 -04:00
|
|
|
|
|
|
|
switch ( $mime_type ) {
|
|
|
|
case 'image/jpeg':
|
2017-11-30 18:11:00 -05:00
|
|
|
$image = imagecreatefromjpeg( $filepath );
|
2012-04-06 16:47:24 -04:00
|
|
|
break;
|
|
|
|
case 'image/png':
|
2017-11-30 18:11:00 -05:00
|
|
|
$image = imagecreatefrompng( $filepath );
|
2012-04-06 16:47:24 -04:00
|
|
|
break;
|
|
|
|
case 'image/gif':
|
2017-11-30 18:11:00 -05:00
|
|
|
$image = imagecreatefromgif( $filepath );
|
2012-04-06 16:47:24 -04: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 10:44:58 -04:00
|
|
|
case 'image/webp':
|
|
|
|
$image = false;
|
|
|
|
if ( function_exists( 'imagecreatefromwebp' ) ) {
|
|
|
|
$image = imagecreatefromwebp( $filepath );
|
|
|
|
}
|
|
|
|
break;
|
2012-04-06 16:47:24 -04:00
|
|
|
default:
|
|
|
|
$image = false;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-16 09:33:09 -04:00
|
|
|
|
|
|
|
if ( is_gd_image( $image ) ) {
|
2014-02-20 01:27:13 -05:00
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters the current image being loaded for editing.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
*
|
2020-08-16 09:33:09 -04:00
|
|
|
* @param resource|GdImage $image Current image.
|
2020-09-20 10:05:05 -04:00
|
|
|
* @param int $attachment_id Attachment ID.
|
2020-09-20 12:23:07 -04: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 01:27:13 -05:00
|
|
|
*/
|
|
|
|
$image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size );
|
2020-08-16 09:33:09 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
|
|
|
|
imagealphablending( $image, false );
|
|
|
|
imagesavealpha( $image, true );
|
2012-04-06 16:47:24 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 09:33:09 -04:00
|
|
|
|
2012-04-06 16:47:24 -04:00
|
|
|
return $image;
|
|
|
|
}
|
2012-05-16 13:47:55 -04:00
|
|
|
|
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Retrieves the path or URL of an attachment's attached file.
|
2012-05-16 13:47:55 -04:00
|
|
|
*
|
|
|
|
* If the attached file is not present on the local filesystem (usually due to replication plugins),
|
2020-09-20 11:55:10 -04:00
|
|
|
* then the URL of the file is returned if `allow_url_fopen` is supported.
|
2012-05-16 13:47:55 -04:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @access private
|
|
|
|
*
|
2020-09-20 12:23:07 -04: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 13:47:55 -04: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 12:24:52 -04:00
|
|
|
if ( 'full' !== $size ) {
|
|
|
|
$data = image_get_intermediate_size( $attachment_id, $size );
|
|
|
|
|
|
|
|
if ( $data ) {
|
|
|
|
$filepath = path_join( dirname( $filepath ), $data['file'] );
|
|
|
|
|
|
|
|
/**
|
2020-09-20 11:55:10 -04:00
|
|
|
* Filters the path to an attachment's file when editing the image.
|
2019-06-15 12:24:52 -04:00
|
|
|
*
|
|
|
|
* The filter is evaluated for all image sizes except 'full'.
|
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
2020-09-20 12:23:07 -04: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 12:24:52 -04:00
|
|
|
*/
|
|
|
|
$filepath = apply_filters( 'load_image_to_edit_filesystempath', $filepath, $attachment_id, $size );
|
|
|
|
}
|
2012-05-16 13:47:55 -04:00
|
|
|
}
|
2019-06-15 12:24:52 -04:00
|
|
|
} elseif ( function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) ) {
|
2014-02-20 01:27:13 -05:00
|
|
|
/**
|
2020-09-20 11:55:10 -04:00
|
|
|
* Filters the path to an attachment's URL when editing the image.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
2020-09-20 11:55:10 -04: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 01:27:13 -05:00
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
2021-01-03 16:57:09 -05:00
|
|
|
* @param string|false $image_url Current image URL.
|
2020-09-20 12:23:07 -04: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 01:27:13 -05:00
|
|
|
*/
|
2012-05-16 13:47:55 -04:00
|
|
|
$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size );
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:27:13 -05:00
|
|
|
/**
|
2016-05-22 14:01:30 -04:00
|
|
|
* Filters the returned path or URL of the current image.
|
2014-02-20 01:27:13 -05:00
|
|
|
*
|
|
|
|
* @since 2.9.0
|
|
|
|
*
|
2021-01-03 17:04:04 -05:00
|
|
|
* @param string|false $filepath File path or URL to current image, or false.
|
2020-09-20 12:23:07 -04: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 01:27:13 -05:00
|
|
|
*/
|
2012-05-16 13:47:55 -04:00
|
|
|
return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-08-05 10:07:09 -04:00
|
|
|
* Copies an existing image file.
|
2012-05-16 13:47:55 -04:00
|
|
|
*
|
|
|
|
* @since 3.4.0
|
|
|
|
* @access private
|
|
|
|
*
|
2020-09-20 10:05:05 -04:00
|
|
|
* @param int $attachment_id Attachment ID.
|
2012-05-16 13:47:55 -04:00
|
|
|
* @return string|false New file path on success, false on failure.
|
|
|
|
*/
|
|
|
|
function _copy_image_file( $attachment_id ) {
|
2019-06-14 21:02:52 -04:00
|
|
|
$dst_file = get_attached_file( $attachment_id );
|
|
|
|
$src_file = $dst_file;
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! file_exists( $src_file ) ) {
|
2012-05-16 13:47:55 -04:00
|
|
|
$src_file = _load_image_to_edit_path( $attachment_id );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-05-16 13:47:55 -04:00
|
|
|
|
|
|
|
if ( $src_file ) {
|
2019-03-01 15:58:52 -05: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-09 21:26:12 -04:00
|
|
|
|
2014-07-17 05:14:16 -04:00
|
|
|
/*
|
|
|
|
* The directory containing the original file may no longer
|
|
|
|
* exist when using a replication plugin.
|
|
|
|
*/
|
2012-06-09 21:26:12 -04:00
|
|
|
wp_mkdir_p( dirname( $dst_file ) );
|
|
|
|
|
2019-07-09 01:45:58 -04:00
|
|
|
if ( ! copy( $src_file, $dst_file ) ) {
|
2012-05-16 13:47:55 -04:00
|
|
|
$dst_file = false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-05-16 13:47:55 -04:00
|
|
|
} else {
|
|
|
|
$dst_file = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dst_file;
|
|
|
|
}
|