4

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?

7

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
}

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? – user1049961 Feb 13 '17 at 22:10
  • @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. – Nathan Johnson Feb 13 '17 at 23:09
0

Use the wp_generate_attachment_metadata filter for this, it's fired in the wp_generate_attachment_metadata function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.