How to save two copies of the same image in a wordpress upload?

So here’s what I’m trying to do, basically when the users upload a new image, I want to make an image half that size (keeping the proportion) and half the resolution, but save both versions. Maybe save the original as ‘image-upload.jpg’ and the one I modify using php save it as ‘image-upload-halved.jpg’

I’ve messed around with wordpress filters, but can’t seem to get it. Below is along the lines of what I was thinking I should do, but I really have no idea.

Read More
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

function custom_upload_filter( $file ){
    // here I was hoping I could do the image manipulation
    // and also save both versions of the image
}

Any advice or links to other wordpress filters that might fit the job better would be awesome, too.

Thanks!

Related posts

1 comment

  1. Take a look at the documentation for add_image_size https://developer.wordpress.org/reference/functions/add_image_size/

    You should be able to add a new image size like this:

    add_image_size( 'custom-size', 220, 180 ); // 220 pixels wide by 180 pixels tall, soft proportional crop mode
    

    Replace “custom-size” with a name for your size and the pixel values that you want.

    You can call the image in your template like this:

    // Assuming your Media Library image has a post id of 42...
    echo wp_get_attachment_image( 42, 'your-custom-size' );
    

Comments are closed.