Edit File by line
/home/zeestwma/redstone.../wp-admin/includes
File: image.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* File contains all the administration image manipulation functions.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Administration
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Crops an image to a given size.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 2.1.0
[11] Fix | Delete
*
[12] Fix | Delete
* @param string|int $src The source file or Attachment ID.
[13] Fix | Delete
* @param int $src_x The start x position to crop from.
[14] Fix | Delete
* @param int $src_y The start y position to crop from.
[15] Fix | Delete
* @param int $src_w The width to crop.
[16] Fix | Delete
* @param int $src_h The height to crop.
[17] Fix | Delete
* @param int $dst_w The destination width.
[18] Fix | Delete
* @param int $dst_h The destination height.
[19] Fix | Delete
* @param bool|false $src_abs Optional. If the source crop points are absolute.
[20] Fix | Delete
* @param string|false $dst_file Optional. The destination file to write to.
[21] Fix | Delete
* @return string|WP_Error New filepath on success, WP_Error on failure.
[22] Fix | Delete
*/
[23] Fix | Delete
function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
[24] Fix | Delete
$src_file = $src;
[25] Fix | Delete
if ( is_numeric( $src ) ) { // Handle int as attachment ID.
[26] Fix | Delete
$src_file = get_attached_file( $src );
[27] Fix | Delete
[28] Fix | Delete
if ( ! file_exists( $src_file ) ) {
[29] Fix | Delete
/*
[30] Fix | Delete
* If the file doesn't exist, attempt a URL fopen on the src link.
[31] Fix | Delete
* This can occur with certain file replication plugins.
[32] Fix | Delete
*/
[33] Fix | Delete
$src = _load_image_to_edit_path( $src, 'full' );
[34] Fix | Delete
} else {
[35] Fix | Delete
$src = $src_file;
[36] Fix | Delete
}
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
$editor = wp_get_image_editor( $src );
[40] Fix | Delete
if ( is_wp_error( $editor ) ) {
[41] Fix | Delete
return $editor;
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );
[45] Fix | Delete
if ( is_wp_error( $src ) ) {
[46] Fix | Delete
return $src;
[47] Fix | Delete
}
[48] Fix | Delete
[49] Fix | Delete
if ( ! $dst_file ) {
[50] Fix | Delete
$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file );
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
/*
[54] Fix | Delete
* The directory containing the original file may no longer exist when
[55] Fix | Delete
* using a replication plugin.
[56] Fix | Delete
*/
[57] Fix | Delete
wp_mkdir_p( dirname( $dst_file ) );
[58] Fix | Delete
[59] Fix | Delete
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) );
[60] Fix | Delete
[61] Fix | Delete
$result = $editor->save( $dst_file );
[62] Fix | Delete
if ( is_wp_error( $result ) ) {
[63] Fix | Delete
return $result;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
if ( ! empty( $result['path'] ) ) {
[67] Fix | Delete
return $result['path'];
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
return $dst_file;
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
/**
[74] Fix | Delete
* Compare the existing image sub-sizes (as saved in the attachment meta)
[75] Fix | Delete
* to the currently registered image sub-sizes, and return the difference.
[76] Fix | Delete
*
[77] Fix | Delete
* Registered sub-sizes that are larger than the image are skipped.
[78] Fix | Delete
*
[79] Fix | Delete
* @since 5.3.0
[80] Fix | Delete
*
[81] Fix | Delete
* @param int $attachment_id The image attachment post ID.
[82] Fix | Delete
* @return array[] Associative array of arrays of image sub-size information for
[83] Fix | Delete
* missing image sizes, keyed by image size name.
[84] Fix | Delete
*/
[85] Fix | Delete
function wp_get_missing_image_subsizes( $attachment_id ) {
[86] Fix | Delete
if ( ! wp_attachment_is_image( $attachment_id ) ) {
[87] Fix | Delete
return array();
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
$registered_sizes = wp_get_registered_image_subsizes();
[91] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[92] Fix | Delete
[93] Fix | Delete
// Meta error?
[94] Fix | Delete
if ( empty( $image_meta ) ) {
[95] Fix | Delete
return $registered_sizes;
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
// Use the originally uploaded image dimensions as full_width and full_height.
[99] Fix | Delete
if ( ! empty( $image_meta['original_image'] ) ) {
[100] Fix | Delete
$image_file = wp_get_original_image_path( $attachment_id );
[101] Fix | Delete
$imagesize = wp_getimagesize( $image_file );
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
if ( ! empty( $imagesize ) ) {
[105] Fix | Delete
$full_width = $imagesize[0];
[106] Fix | Delete
$full_height = $imagesize[1];
[107] Fix | Delete
} else {
[108] Fix | Delete
$full_width = (int) $image_meta['width'];
[109] Fix | Delete
$full_height = (int) $image_meta['height'];
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
$possible_sizes = array();
[113] Fix | Delete
[114] Fix | Delete
// Skip registered sizes that are too large for the uploaded image.
[115] Fix | Delete
foreach ( $registered_sizes as $size_name => $size_data ) {
[116] Fix | Delete
if ( image_resize_dimensions( $full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'] ) ) {
[117] Fix | Delete
$possible_sizes[ $size_name ] = $size_data;
[118] Fix | Delete
}
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
if ( empty( $image_meta['sizes'] ) ) {
[122] Fix | Delete
$image_meta['sizes'] = array();
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/*
[126] Fix | Delete
* Remove sizes that already exist. Only checks for matching "size names".
[127] Fix | Delete
* It is possible that the dimensions for a particular size name have changed.
[128] Fix | Delete
* For example the user has changed the values on the Settings -> Media screen.
[129] Fix | Delete
* However we keep the old sub-sizes with the previous dimensions
[130] Fix | Delete
* as the image may have been used in an older post.
[131] Fix | Delete
*/
[132] Fix | Delete
$missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] );
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* Filters the array of missing image sub-sizes for an uploaded image.
[136] Fix | Delete
*
[137] Fix | Delete
* @since 5.3.0
[138] Fix | Delete
*
[139] Fix | Delete
* @param array[] $missing_sizes Associative array of arrays of image sub-size information for
[140] Fix | Delete
* missing image sizes, keyed by image size name.
[141] Fix | Delete
* @param array $image_meta The image meta data.
[142] Fix | Delete
* @param int $attachment_id The image attachment post ID.
[143] Fix | Delete
*/
[144] Fix | Delete
return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id );
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* If any of the currently registered image sub-sizes are missing,
[149] Fix | Delete
* create them and update the image meta data.
[150] Fix | Delete
*
[151] Fix | Delete
* @since 5.3.0
[152] Fix | Delete
*
[153] Fix | Delete
* @param int $attachment_id The image attachment post ID.
[154] Fix | Delete
* @return array|WP_Error The updated image meta data array or WP_Error object
[155] Fix | Delete
* if both the image meta and the attached file are missing.
[156] Fix | Delete
*/
[157] Fix | Delete
function wp_update_image_subsizes( $attachment_id ) {
[158] Fix | Delete
$image_meta = wp_get_attachment_metadata( $attachment_id );
[159] Fix | Delete
$image_file = wp_get_original_image_path( $attachment_id );
[160] Fix | Delete
[161] Fix | Delete
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
[162] Fix | Delete
/*
[163] Fix | Delete
* Previously failed upload?
[164] Fix | Delete
* If there is an uploaded file, make all sub-sizes and generate all of the attachment meta.
[165] Fix | Delete
*/
[166] Fix | Delete
if ( ! empty( $image_file ) ) {
[167] Fix | Delete
$image_meta = wp_create_image_subsizes( $image_file, $attachment_id );
[168] Fix | Delete
} else {
[169] Fix | Delete
return new WP_Error( 'invalid_attachment', __( 'The attached file cannot be found.' ) );
[170] Fix | Delete
}
[171] Fix | Delete
} else {
[172] Fix | Delete
$missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
[173] Fix | Delete
[174] Fix | Delete
if ( empty( $missing_sizes ) ) {
[175] Fix | Delete
return $image_meta;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
// This also updates the image meta.
[179] Fix | Delete
$image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
/** This filter is documented in wp-admin/includes/image.php */
[183] Fix | Delete
$image_meta = apply_filters( 'wp_generate_attachment_metadata', $image_meta, $attachment_id, 'update' );
[184] Fix | Delete
[185] Fix | Delete
// Save the updated metadata.
[186] Fix | Delete
wp_update_attachment_metadata( $attachment_id, $image_meta );
[187] Fix | Delete
[188] Fix | Delete
return $image_meta;
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
/**
[192] Fix | Delete
* Updates the attached file and image meta data when the original image was edited.
[193] Fix | Delete
*
[194] Fix | Delete
* @since 5.3.0
[195] Fix | Delete
* @since 6.0.0 The `$filesize` value was added to the returned array.
[196] Fix | Delete
* @access private
[197] Fix | Delete
*
[198] Fix | Delete
* @param array $saved_data The data returned from WP_Image_Editor after successfully saving an image.
[199] Fix | Delete
* @param string $original_file Path to the original file.
[200] Fix | Delete
* @param array $image_meta The image meta data.
[201] Fix | Delete
* @param int $attachment_id The attachment post ID.
[202] Fix | Delete
* @return array The updated image meta data.
[203] Fix | Delete
*/
[204] Fix | Delete
function _wp_image_meta_replace_original( $saved_data, $original_file, $image_meta, $attachment_id ) {
[205] Fix | Delete
$new_file = $saved_data['path'];
[206] Fix | Delete
[207] Fix | Delete
// Update the attached file meta.
[208] Fix | Delete
update_attached_file( $attachment_id, $new_file );
[209] Fix | Delete
[210] Fix | Delete
// Width and height of the new image.
[211] Fix | Delete
$image_meta['width'] = $saved_data['width'];
[212] Fix | Delete
$image_meta['height'] = $saved_data['height'];
[213] Fix | Delete
[214] Fix | Delete
// Make the file path relative to the upload dir.
[215] Fix | Delete
$image_meta['file'] = _wp_relative_upload_path( $new_file );
[216] Fix | Delete
[217] Fix | Delete
// Add image file size.
[218] Fix | Delete
$image_meta['filesize'] = wp_filesize( $new_file );
[219] Fix | Delete
[220] Fix | Delete
// Store the original image file name in image_meta.
[221] Fix | Delete
$image_meta['original_image'] = wp_basename( $original_file );
[222] Fix | Delete
[223] Fix | Delete
return $image_meta;
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
/**
[227] Fix | Delete
* Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
[228] Fix | Delete
*
[229] Fix | Delete
* Intended for use after an image is uploaded. Saves/updates the image metadata after each
[230] Fix | Delete
* sub-size is created. If there was an error, it is added to the returned image metadata array.
[231] Fix | Delete
*
[232] Fix | Delete
* @since 5.3.0
[233] Fix | Delete
*
[234] Fix | Delete
* @param string $file Full path to the image file.
[235] Fix | Delete
* @param int $attachment_id Attachment ID to process.
[236] Fix | Delete
* @return array The image attachment meta data.
[237] Fix | Delete
*/
[238] Fix | Delete
function wp_create_image_subsizes( $file, $attachment_id ) {
[239] Fix | Delete
$imagesize = wp_getimagesize( $file );
[240] Fix | Delete
[241] Fix | Delete
if ( empty( $imagesize ) ) {
[242] Fix | Delete
// File is not an image.
[243] Fix | Delete
return array();
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
// Default image meta.
[247] Fix | Delete
$image_meta = array(
[248] Fix | Delete
'width' => $imagesize[0],
[249] Fix | Delete
'height' => $imagesize[1],
[250] Fix | Delete
'file' => _wp_relative_upload_path( $file ),
[251] Fix | Delete
'filesize' => wp_filesize( $file ),
[252] Fix | Delete
'sizes' => array(),
[253] Fix | Delete
);
[254] Fix | Delete
[255] Fix | Delete
// Fetch additional metadata from EXIF/IPTC.
[256] Fix | Delete
$exif_meta = wp_read_image_metadata( $file );
[257] Fix | Delete
[258] Fix | Delete
if ( $exif_meta ) {
[259] Fix | Delete
$image_meta['image_meta'] = $exif_meta;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
/**
[263] Fix | Delete
* Filters the "BIG image" threshold value.
[264] Fix | Delete
*
[265] Fix | Delete
* If the original image width or height is above the threshold, it will be scaled down. The threshold is
[266] Fix | Delete
* used as max width and max height. The scaled down image will be used as the largest available size, including
[267] Fix | Delete
* the `_wp_attached_file` post meta value.
[268] Fix | Delete
*
[269] Fix | Delete
* Returning `false` from the filter callback will disable the scaling.
[270] Fix | Delete
*
[271] Fix | Delete
* @since 5.3.0
[272] Fix | Delete
*
[273] Fix | Delete
* @param int $threshold The threshold value in pixels. Default 2560.
[274] Fix | Delete
* @param array $imagesize {
[275] Fix | Delete
* Indexed array of the image width and height in pixels.
[276] Fix | Delete
*
[277] Fix | Delete
* @type int $0 The image width.
[278] Fix | Delete
* @type int $1 The image height.
[279] Fix | Delete
* }
[280] Fix | Delete
* @param string $file Full path to the uploaded image file.
[281] Fix | Delete
* @param int $attachment_id Attachment post ID.
[282] Fix | Delete
*/
[283] Fix | Delete
$threshold = (int) apply_filters( 'big_image_size_threshold', 2560, $imagesize, $file, $attachment_id );
[284] Fix | Delete
[285] Fix | Delete
/*
[286] Fix | Delete
* If the original image's dimensions are over the threshold,
[287] Fix | Delete
* scale the image and use it as the "full" size.
[288] Fix | Delete
*/
[289] Fix | Delete
$scale_down = false;
[290] Fix | Delete
$convert = false;
[291] Fix | Delete
[292] Fix | Delete
if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
[293] Fix | Delete
// The image will be converted if needed on saving.
[294] Fix | Delete
$scale_down = true;
[295] Fix | Delete
} else {
[296] Fix | Delete
// The image may need to be converted regardless of its dimensions.
[297] Fix | Delete
$output_format = wp_get_image_editor_output_format( $file, $imagesize['mime'] );
[298] Fix | Delete
[299] Fix | Delete
if (
[300] Fix | Delete
is_array( $output_format ) &&
[301] Fix | Delete
array_key_exists( $imagesize['mime'], $output_format ) &&
[302] Fix | Delete
$output_format[ $imagesize['mime'] ] !== $imagesize['mime']
[303] Fix | Delete
) {
[304] Fix | Delete
$convert = true;
[305] Fix | Delete
}
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
if ( $scale_down || $convert ) {
[309] Fix | Delete
$editor = wp_get_image_editor( $file );
[310] Fix | Delete
[311] Fix | Delete
if ( is_wp_error( $editor ) ) {
[312] Fix | Delete
// This image cannot be edited.
[313] Fix | Delete
return $image_meta;
[314] Fix | Delete
}
[315] Fix | Delete
[316] Fix | Delete
if ( $scale_down ) {
[317] Fix | Delete
// Resize the image. This will also convet it if needed.
[318] Fix | Delete
$resized = $editor->resize( $threshold, $threshold );
[319] Fix | Delete
} elseif ( $convert ) {
[320] Fix | Delete
// The image will be converted (if possible) when saved.
[321] Fix | Delete
$resized = true;
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
$rotated = null;
[325] Fix | Delete
[326] Fix | Delete
// If there is EXIF data, rotate according to EXIF Orientation.
[327] Fix | Delete
if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
[328] Fix | Delete
$resized = $editor->maybe_exif_rotate();
[329] Fix | Delete
$rotated = $resized; // bool true or WP_Error
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
if ( ! is_wp_error( $resized ) ) {
[333] Fix | Delete
/*
[334] Fix | Delete
* Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
[335] Fix | Delete
* This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
[336] Fix | Delete
*/
[337] Fix | Delete
if ( $scale_down ) {
[338] Fix | Delete
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
[339] Fix | Delete
} elseif ( $convert ) {
[340] Fix | Delete
// Pass an empty string to avoid adding a suffix to converted file names.
[341] Fix | Delete
$saved = $editor->save( $editor->generate_filename( '' ) );
[342] Fix | Delete
} else {
[343] Fix | Delete
$saved = $editor->save();
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
if ( ! is_wp_error( $saved ) ) {
[347] Fix | Delete
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
[348] Fix | Delete
[349] Fix | Delete
// If the image was rotated update the stored EXIF data.
[350] Fix | Delete
if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) {
[351] Fix | Delete
$image_meta['image_meta']['orientation'] = 1;
[352] Fix | Delete
}
[353] Fix | Delete
} else {
[354] Fix | Delete
// TODO: Log errors.
[355] Fix | Delete
}
[356] Fix | Delete
} else {
[357] Fix | Delete
// TODO: Log errors.
[358] Fix | Delete
}
[359] Fix | Delete
} elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
[360] Fix | Delete
// Rotate the whole original image if there is EXIF data and "orientation" is not 1.
[361] Fix | Delete
$editor = wp_get_image_editor( $file );
[362] Fix | Delete
[363] Fix | Delete
if ( is_wp_error( $editor ) ) {
[364] Fix | Delete
// This image cannot be edited.
[365] Fix | Delete
return $image_meta;
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
// Rotate the image.
[369] Fix | Delete
$rotated = $editor->maybe_exif_rotate();
[370] Fix | Delete
[371] Fix | Delete
if ( true === $rotated ) {
[372] Fix | Delete
// Append `-rotated` to the image file name.
[373] Fix | Delete
$saved = $editor->save( $editor->generate_filename( 'rotated' ) );
[374] Fix | Delete
[375] Fix | Delete
if ( ! is_wp_error( $saved ) ) {
[376] Fix | Delete
$image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
[377] Fix | Delete
[378] Fix | Delete
// Update the stored EXIF data.
[379] Fix | Delete
if ( ! empty( $image_meta['image_meta']['orientation'] ) ) {
[380] Fix | Delete
$image_meta['image_meta']['orientation'] = 1;
[381] Fix | Delete
}
[382] Fix | Delete
} else {
[383] Fix | Delete
// TODO: Log errors.
[384] Fix | Delete
}
[385] Fix | Delete
}
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
/*
[389] Fix | Delete
* Initial save of the new metadata.
[390] Fix | Delete
* At this point the file was uploaded and moved to the uploads directory
[391] Fix | Delete
* but the image sub-sizes haven't been created yet and the `sizes` array is empty.
[392] Fix | Delete
*/
[393] Fix | Delete
wp_update_attachment_metadata( $attachment_id, $image_meta );
[394] Fix | Delete
[395] Fix | Delete
$new_sizes = wp_get_registered_image_subsizes();
[396] Fix | Delete
[397] Fix | Delete
/**
[398] Fix | Delete
* Filters the image sizes automatically generated when uploading an image.
[399] Fix | Delete
*
[400] Fix | Delete
* @since 2.9.0
[401] Fix | Delete
* @since 4.4.0 Added the `$image_meta` argument.
[402] Fix | Delete
* @since 5.3.0 Added the `$attachment_id` argument.
[403] Fix | Delete
*
[404] Fix | Delete
* @param array $new_sizes Associative array of image sizes to be created.
[405] Fix | Delete
* @param array $image_meta The image meta data: width, height, file, sizes, etc.
[406] Fix | Delete
* @param int $attachment_id The attachment post ID for the image.
[407] Fix | Delete
*/
[408] Fix | Delete
$new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id );
[409] Fix | Delete
[410] Fix | Delete
return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id );
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Low-level function to create image sub-sizes.
[415] Fix | Delete
*
[416] Fix | Delete
* Updates the image meta after each sub-size is created.
[417] Fix | Delete
* Errors are stored in the returned image metadata array.
[418] Fix | Delete
*
[419] Fix | Delete
* @since 5.3.0
[420] Fix | Delete
* @access private
[421] Fix | Delete
*
[422] Fix | Delete
* @param array $new_sizes Array defining what sizes to create.
[423] Fix | Delete
* @param string $file Full path to the image file.
[424] Fix | Delete
* @param array $image_meta The attachment meta data array.
[425] Fix | Delete
* @param int $attachment_id Attachment ID to process.
[426] Fix | Delete
* @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
[427] Fix | Delete
*/
[428] Fix | Delete
function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
[429] Fix | Delete
if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
[430] Fix | Delete
// Not an image attachment.
[431] Fix | Delete
return array();
[432] Fix | Delete
}
[433] Fix | Delete
[434] Fix | Delete
// Check if any of the new sizes already exist.
[435] Fix | Delete
if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) {
[436] Fix | Delete
foreach ( $image_meta['sizes'] as $size_name => $size_meta ) {
[437] Fix | Delete
/*
[438] Fix | Delete
* Only checks "size name" so we don't override existing images even if the dimensions
[439] Fix | Delete
* don't match the currently defined size with the same name.
[440] Fix | Delete
* To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta.
[441] Fix | Delete
*/
[442] Fix | Delete
if ( array_key_exists( $size_name, $new_sizes ) ) {
[443] Fix | Delete
unset( $new_sizes[ $size_name ] );
[444] Fix | Delete
}
[445] Fix | Delete
}
[446] Fix | Delete
} else {
[447] Fix | Delete
$image_meta['sizes'] = array();
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
if ( empty( $new_sizes ) ) {
[451] Fix | Delete
// Nothing to do...
[452] Fix | Delete
return $image_meta;
[453] Fix | Delete
}
[454] Fix | Delete
[455] Fix | Delete
/*
[456] Fix | Delete
* Sort the image sub-sizes in order of priority when creating them.
[457] Fix | Delete
* This ensures there is an appropriate sub-size the user can access immediately
[458] Fix | Delete
* even when there was an error and not all sub-sizes were created.
[459] Fix | Delete
*/
[460] Fix | Delete
$priority = array(
[461] Fix | Delete
'medium' => null,
[462] Fix | Delete
'large' => null,
[463] Fix | Delete
'thumbnail' => null,
[464] Fix | Delete
'medium_large' => null,
[465] Fix | Delete
);
[466] Fix | Delete
[467] Fix | Delete
$new_sizes = array_filter( array_merge( $priority, $new_sizes ) );
[468] Fix | Delete
[469] Fix | Delete
$editor = wp_get_image_editor( $file );
[470] Fix | Delete
[471] Fix | Delete
if ( is_wp_error( $editor ) ) {
[472] Fix | Delete
// The image cannot be edited.
[473] Fix | Delete
return $image_meta;
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
// If stored EXIF data exists, rotate the source image before creating sub-sizes.
[477] Fix | Delete
if ( ! empty( $image_meta['image_meta'] ) ) {
[478] Fix | Delete
$rotated = $editor->maybe_exif_rotate();
[479] Fix | Delete
[480] Fix | Delete
if ( is_wp_error( $rotated ) ) {
[481] Fix | Delete
// TODO: Log errors.
[482] Fix | Delete
}
[483] Fix | Delete
}
[484] Fix | Delete
[485] Fix | Delete
if ( method_exists( $editor, 'make_subsize' ) ) {
[486] Fix | Delete
foreach ( $new_sizes as $new_size_name => $new_size_data ) {
[487] Fix | Delete
$new_size_meta = $editor->make_subsize( $new_size_data );
[488] Fix | Delete
[489] Fix | Delete
if ( is_wp_error( $new_size_meta ) ) {
[490] Fix | Delete
// TODO: Log errors.
[491] Fix | Delete
} else {
[492] Fix | Delete
// Save the size meta value.
[493] Fix | Delete
$image_meta['sizes'][ $new_size_name ] = $new_size_meta;
[494] Fix | Delete
wp_update_attachment_metadata( $attachment_id, $image_meta );
[495] Fix | Delete
}
[496] Fix | Delete
}
[497] Fix | Delete
} else {
[498] Fix | Delete
// Fall back to `$editor->multi_resize()`.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function