0

I am trying to optimize my images when uploading to website. I trying to write plugin, that will use hook 'add_attachment' and after attachment uploaded do image converting.

add_action( 'add_attachment', 'optimize_psio', 0);
// Action for optimizing on upload
function optimize_psio( $post_ID){
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-ps-image-optimizer-converter.php';
    $converter = new Ps_Image_Optimizer_Converter($post_ID);
    $converter->optimize();
}

The optimize_psio function works great when working standalone. But when I trying to register hook and upload image, I getting error:

Post-processing of the image failed likely because the server is busy or does not have enough resources. Uploading a smaller image may help. Suggested maximum size is 2500 pixels.

Image uploading, but not cropped and not converted. What am I doing wrong?

4
  • the code for Ps_Image_Optimizer_Converter is missing!
    – Tom J Nowell
    Jan 22, 2022 at 18:02
  • There only converting to webp function there and it was tested without action and worked.
    – Denny
    Jan 22, 2022 at 19:52
  • 1
    You still need to share the class, all of the relevant code to answering the question is contained inside that class. You must edit your question to include it. I know you do not believe it is relevant, but you're also trying to figure out a problem and have come here and asked, so humour us, after all if it is indeed irrelevant it will be clear from the code and nobody will ask again
    – Tom J Nowell
    Jan 23, 2022 at 1:46
  • Thank you @TomJNowell I found what is my error. Hook fires before image was cropped and that is why I can't optimize images that not exists.
    – Denny
    Jan 24, 2022 at 12:45

1 Answer 1

1

Hook add_attachment fires before images cropped via wp_ajax_crop_image(). That's why Wordpress trying to work with files, that is not existed for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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