Regen Thumbnails not maintaining User Crops
-
I think User Crop preservation is a new addtion to WP, not sure which version, but when a user crops an image in the Image Editor, it creates another copy of the image with a timestamp hash. I’ve written a small plugin to ensure that user-cropped images are kept attached to the image, rather than resetting to the original.
Maybe it’ll be of use to roll into the plugin? Or maybe it’s more useful as a core submission – either way, here it is!
class ZG_Regenerate_Thumbnail_Enhancements { protected $current_image_size = ''; function __construct() { add_action( 'wp_generate_attachment_metadata', array( $this, 'reselect_user_cropped_images' ), 10, 2 ); } function reselect_user_cropped_images( $metadata, $attachment_id ) { $filepath = $metadata['file']; $last_dot_pos = strrpos( $filepath, '.' ); $last_slash_pos = strrpos( $filepath, '/' )+1; $ext = substr( $filepath, $last_dot_pos ); $directory = substr( $filepath, 0, $last_slash_pos ); $filename = substr( $filepath, $last_slash_pos, $last_dot_pos - $last_slash_pos ); $wp_upload_dir = wp_upload_dir( $directory ); $images = glob( $wp_upload_dir['path'] . "/$filename-*$ext" ); // Loop through the image's sizes and check to see if we have any // user-cropped versions foreach( $metadata['sizes'] as $size => &$data ) { $this->current_image_size = $data; $matches = array_filter( $images, array( $this, 'find_user_crop_images' ) ); if ( $matches ) { $filepath = end( $matches ); $last_slash_pos = strrpos( $filepath, '/' )+1; $data['file'] = substr( $filepath, $last_slash_pos ); } } return $metadata; } function find_user_crop_images( $image ) { return strpos( $image, $this->current_image_size['width'] . 'x' . $this->current_image_size['height'] ); } } new ZG_Regenerate_Thumbnail_Enhancements;
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Regen Thumbnails not maintaining User Crops’ is closed to new replies.