Site icon Hip-Hop Website Design and Development

Hook add_attachment error

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?