Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
WC_Regenerate_Images::resize_and_return_image( int $attachment_id, array $image, string $size, bool $icon )
Regenerate the image according to the required size
Description Description
Parameters Parameters
- $attachment_id
-
(Required) Attachment ID.
- $image
-
(Required) Original Image.
- $size
-
(Required) Size to return for new URL.
- $icon
-
(Required) If icon or not.
Return Return
(string)
Source Source
File: includes/class-wc-regenerate-images.php
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; }