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