WC_Regenerate_Images::resize_and_return_image() private WC 1.0
Regenerate the image according to the required size
{} Это метод класса: WC_Regenerate_Images{}
Хуков нет.
Возвращает
Строку.
Использование
$result = WC_Regenerate_Images::resize_and_return_image( $attachment_id, $image, $size, $icon );
- $attachment_id(число) (обязательный)
- Attachment ID.
- $image(массив) (обязательный)
- Original Image.
- $size(строка) (обязательный)
- Size to return for new URL.
- $icon(true/false) (обязательный)
- If icon or not.
Код WC_Regenerate_Images::resize_and_return_image() WC Regenerate Images::resize and return image WC 5.0.0
private static function resize_and_return_image( $attachment_id, $image, $size, $icon ) {
if ( ! self::is_regeneratable( $attachment_id ) ) {
return $image;
}
$fullsizepath = get_attached_file( $attachment_id );
if ( false === $fullsizepath || is_wp_error( $fullsizepath ) || ! file_exists( $fullsizepath ) ) {
return $image;
}
if ( ! function_exists( 'wp_crop_image' ) ) {
include ABSPATH . 'wp-admin/includes/image.php';
}
self::$regenerate_size = is_customize_preview() ? $size . '_preview' : $size;
if ( is_customize_preview() ) {
$image_size = wc_get_image_size( $size );
// Make sure registered image size matches the size we're requesting.
add_image_size( self::$regenerate_size, absint( $image_size['width'] ), absint( $image_size['height'] ), $image_size['crop'] );
$thumbnail = self::get_image( $fullsizepath, absint( $image_size['width'] ), absint( $image_size['height'] ), $image_size['crop'] );
// If the file is already there perhaps just load it if we're using the customizer. No need to store in meta data.
if ( $thumbnail && file_exists( $thumbnail['filename'] ) ) {
$wp_uploads = wp_upload_dir( null, false );
$wp_uploads_dir = $wp_uploads['basedir'];
$wp_uploads_url = $wp_uploads['baseurl'];
return array(
0 => str_replace( $wp_uploads_dir, $wp_uploads_url, $thumbnail['filename'] ),
1 => $thumbnail['width'],
2 => $thumbnail['height'],
);
}
}
$metadata = wp_get_attachment_metadata( $attachment_id );
// Fix for images with no metadata.
if ( ! is_array( $metadata ) ) {
$metadata = array();
}
// We only want to regen a specific image size.
add_filter( 'intermediate_image_sizes', array( __CLASS__, 'adjust_intermediate_image_sizes' ) );
// This function will generate the new image sizes.
$new_metadata = wp_generate_attachment_metadata( $attachment_id, $fullsizepath );
// Remove custom filter.
remove_filter( 'intermediate_image_sizes', array( __CLASS__, 'adjust_intermediate_image_sizes' ) );
// If something went wrong lets just return the original image.
if ( is_wp_error( $new_metadata ) || empty( $new_metadata ) ) {
return $image;
}
if ( isset( $new_metadata['sizes'][ self::$regenerate_size ] ) ) {
$metadata['sizes'][ self::$regenerate_size ] = $new_metadata['sizes'][ self::$regenerate_size ];
wp_update_attachment_metadata( $attachment_id, $metadata );
}
// Now we've done our regen, attempt to return the new size.
$new_image = self::unfiltered_image_downsize( $attachment_id, self::$regenerate_size );
return $new_image ? $new_image : $image;
}