post_type || 'image/' !== substr( $attachment->post_mime_type, 0, 6 ) ) { return $image; } if ( ! function_exists( 'wp_crop_image' ) ) { include( ABSPATH . 'wp-admin/includes/image.php' ); } $wp_uploads = wp_upload_dir( null, false ); $wp_uploads_dir = $wp_uploads['basedir']; $wp_uploads_url = $wp_uploads['baseurl']; $original_image_file_path = get_attached_file( $attachment->ID ); if ( ! file_exists( $original_image_file_path ) || ! getimagesize( $original_image_file_path ) ) { return $image; } $info = pathinfo( $original_image_file_path ); $ext = $info['extension']; list( $orig_w, $orig_h ) = getimagesize( $original_image_file_path ); // Get image size after cropping. $image_size = wc_get_image_size( $size ); $dimensions = image_resize_dimensions( $orig_w, $orig_h, $image_size['width'], $image_size['height'], $image_size['crop'] ); if ( ! $dimensions || ! is_array( $dimensions ) ) { return $image; } $dst_w = $dimensions[4]; $dst_h = $dimensions[5]; $suffix = "{$dst_w}x{$dst_h}"; $dst_rel_path = str_replace( '.' . $ext, '', $original_image_file_path ); $destfilename = "{$dst_rel_path}-{$suffix}.{$ext}"; // If the file is already there perhaps just load it. if ( file_exists( $destfilename ) ) { return array( 0 => str_replace( $wp_uploads_dir, $wp_uploads_url, $destfilename ), 1 => $image_size['width'], 2 => $image_size['height'], ); } // Lets resize the image if it does not exist yet. $editor = wp_get_image_editor( $original_image_file_path ); if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $image_size['width'], $image_size['height'], $image_size['crop'] ) ) ) { return $image; } $resized_file = $editor->save(); if ( ! is_wp_error( $resized_file ) ) { $img_url = str_replace( $wp_uploads_dir, $wp_uploads_url, $resized_file['path'] ); return array( 0 => $img_url, 1 => $image_size['width'], 2 => $image_size['height'], ); } // Lets just add this here as a fallback. return $image; } /** * Check if we should regenerate the product images when options change. * * @param mixed $old_value Old option value. * @param mixed $new_value New option value. * @param string $option Option name. */ public static function maybe_regenerate_images_option_update( $old_value, $new_value, $option ) { if ( $new_value === $old_value ) { return; } self::queue_image_regeneration(); } /** * Check if we should generate images when new themes declares custom sizes * * @return void */ public static function maybe_regenerate_image_theme_switch() { $theme_support = get_theme_support( 'woocommerce' ); $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; // Only queue image generation if the theme declares custom sizes via theme_support. if ( is_array( $theme_support ) && ( isset( $theme_support['single_image_width'] ) || isset( $theme_support['thumbnail_image_width'] ) ) ) { self::queue_image_regeneration(); } } /** * Get list of images and queue them for regeneration * * @return void */ private static function queue_image_regeneration() { global $wpdb; // First lets cancel existing running queue to avoid running it more than once. self::$background_process->cancel_process(); // Now lets find all product image attachments IDs and pop them onto the queue. $images = $wpdb->get_results( // @codingStandardsIgnoreLine "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC" ); foreach ( $images as $image ) { self::$background_process->push_to_queue( array( 'attachment_id' => $image->ID, ) ); } // Lets dispatch the queue to start processing. self::$background_process->save()->dispatch(); } } WC_Regenerate_Images::init();