I'd like to compress images once they're uploaded to media library. Is there any hook that fires once the image is uploaded and the image sizes generated?
2 Answers
Is there any hook that fires once the image is uploaded and the image sizes generated?
wp_handle_upload
fires after the image is uploaded. After the follow-up question, I discovered that images would not be sized at this point.
add_filter( 'wp_handle_upload' 'wpse_256351_upload', 10, 2 );
function wpse_256351_upload( $upload, $context ) {
//* Do something interesting
return $upload;
}
Added:
Images are resized on line 135 of image.php. There are no hooks in method to resize the images.
At the end of the wp_generate_attachment_metadata() function, wp_generate_attachment_metadata
fires. This is after the image sizes are generated.
wp_read_image_metadata
is another option. It fires before wp_generate_attachment_metadata
but after image sizes are generated.
-
Hmm, are the image sizes already generated at this time? Commented Feb 13, 2017 at 22:10
-
1@user1049961 You're right. The image sizes would not be generated when wp_handle_upload fires. Not sure what I was thinking. I've updated my answer. Commented Feb 13, 2017 at 23:09
Use the wp_generate_attachment_metadata
filter for this, it's fired in the wp_generate_attachment_metadata function.