Processing resized images upon upload


WordPress creates resized copies of images that are uploaded to the media library. When I upload an image, I want to do some processing on these resized images. Currently, I’m using a add_attachment hook that gives me the attachment_id of the image that is being uploaded. However, I’m unable to get the path of the resized images that are added by WordPress.

add_action("add_attachment", 'analyse_attachment');
function analyse_attachment( $attachment_ID ) {    
    $sizes = get_intermediate_image_sizes();
    foreach ( $sizes as $size ) {
      $resized_image = image_get_intermediate_size( $attachment_ID, $size ); 
      if ( is_array( $resized_image ) ) {
        $file_path = resized_image['path'];
        // do some processing
    return $attachment_ID;

Tried using image_get_intermediate_size to get the image path but it doesn’t return anything. What am I doing wrong here?

bazinga 1 year 2021-08-10T16:53:39-05:00 0 Answers 0 views 0

Leave an answer