Support » Plugin: Converter for Media - Optimize images | Convert WebP & AVIF » Quick question about image deletion

  • Resolved JapeNZ

    (@japenz)


    Hi there,

    Just wanted to check.

    If I delete an image from my media library, are the corresponding webp versions of the image created by Converter for Media also deleted?

    I realise they are generated in a seperate folder, and wanted to double check.

    If it does, is it fair to assume that using the following snippet to remove product images when you delete a product will also automatically work with the webp duplicates?

    // Automatically Delete Woocommerce Images After Deleting a Product
    
    add_action( 'before_delete_post', 'delete_product_images', 10, 1 );
     
    function delete_product_images( $post_id ) {
            // Check if user has the capability to delete products
            if ( !current_user_can( 'delete_products' ) ) {
                return;
            }
        $product = wc_get_product( $post_id );
     
        if ( !$product ) {
            return;
        }
     
        $featured_image_id = $product->get_image_id();
        $image_galleries_id = $product->get_gallery_image_ids();
     
        if( !empty( $featured_image_id ) ) {
            $is_featured_image_used = is_image_used( $featured_image_id, $post_id );
            if ( !$is_featured_image_used ) {
                wp_delete_attachment( $featured_image_id, true );
            }
        }
     
        if( !empty( $image_galleries_id ) ) {
            foreach( $image_galleries_id as $single_image_id ) {
                $is_image_used = is_image_used( $single_image_id, $post_id );
                if ( !$is_image_used ) {
                    wp_delete_attachment( $single_image_id, true );
                }
            }
        }
    }
     
    function is_image_used( $image_id, $current_product_id ) {
        $query = new WP_Query( array(
            'post_type' => 'product',
            'post_status' => 'publish',
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key' => '_thumbnail_id',
                    'value' => $image_id,
                    'compare' => '='
                ),
                array(
                    'key' => '_product_image_gallery',
                    'value' => '"'.$image_id.'"',
                    'compare' => 'LIKE'
                )
            ),
            'post__not_in' => array( $current_product_id ),
            'fields' => 'ids',
            'posts_per_page' => -1
        ) );
     
        return ( $query->have_posts() );
    }

    thank you for your help!

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    Hi @japenz,

    Thank you for your message.

    If you delete an image in the WordPress panel in the Media Library tab, the converted WebP and AVIF files are deleted. Converted files are saved in the /wp-content/uploads-webpc/uploads/ directory.

    As for your code, it will be best if you just test it. Please let me know what the test result was.

    Best,
    Mateusz

    Thread Starter JapeNZ

    (@japenz)

    Hi @mateuszgbiorczyk,

    Thank you for confirming, I assumed it would be the case but thought I should doublecheck.

    Have just tested the snippet and deleting a product resulted in the original media files and the webp files being removed as hoped for 🙂

    Another kind of related question.

    Do you know of a way I can automatically delete the original image once uploaded and converted to the theme / Woocommerce sizes?
    I don’t want to delete all non webp images or anything, I’m looking for a way that I can keep the sizes generated by my theme / Woocommerce, but remove the original image.

    Ideally this would result in the original sized image being removed or not added to the Converter for Media folder too.

    I realise this isn’t specifically related to your plugin, just thought I’d ask incase you had any insight that mioght be helpful 🙂

    Thank you for your help!

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @japenz I don’t know about this option and I think it’s a bad idea. The original images are very important because they can be used to generate other image sizes. I am against deleting original files.

    Thread Starter JapeNZ

    (@japenz)

    Hi @mateuszgbiorczyk,

    Fair enough, I appreciate your thoughts 🙂

    I totally agree in general.
    The reason I’m trying to find a way to do this is that I don’t require images to be larger than my Woocommerce product image size for my store.

    However my product (comic books) suppliers provide cover images with the listings in a csv file that are often absolutely massive.

    This means I have to download the images to my computer, resize them manually and upload them.
    Then update the links in the csv files to match the resized images I’ve added to my media library.

    Ideally, I could just run the csv import, and the massive images would be downloaded from the suppliers links, resized and deleted.
    Leaving me with correctly sized images and many hours saved haha!

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @japenz Could you send some examples of these original images? Provide URLs to them.

    Thread Starter JapeNZ

    (@japenz)

    I think I might have found a plugin that can take care of it for me!

    Imsanity

    Looks like I can set maximum size and it will reduce the uploaded image to match that size, and discard the original… effectively creating a smaller ‘original image’ to be used for creating thumbnails etc.

    I’ll do some testing! 😀

    • This reply was modified 2 months, 3 weeks ago by JapeNZ.
    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @japenz Please tell me about your test results?

    Thread Starter JapeNZ

    (@japenz)

    Hi @mateuszgbiorczyk,

    So far so good.

    We’re waiting for our stock shipments to arrive in the next couple of days, we’ll then be uploading new products and can make sure it’s working exactly how expected.

    I’ll let you know how it goes at the end of the week 🙂

    Thread Starter JapeNZ

    (@japenz)

    Hi @mateuszgbiorczyk,

    Quick update.

    The Imsanity plugin works really well.
    I’ve set the size slightly larger than the Woocommerce product image size so if a customer clicks on the product image they have a larger version in the popup.

    With regards to Converter for Media, I’ve just uploaded a batch of images provided by our suppliers.
    Many of these were 6000+ x 4000px.
    Imsanity automatically converted them to maximum height 720px as they were uploaded, and Converter for Media then created Webp versions at the desired size.

    It’s worked brilliantly, and the upload times are substanially better too 🙂

    Plugin Author Mateusz Gbiorczyk

    (@mateuszgbiorczyk)

    @japenz Thank you for the information.

    I also recommend you to try the AVIF format on your website. Images converted to the AVIF format on your website will weigh aboutb50% less than images converted to WebP only, maintaining even better image quality. This will further improve the performance of your website.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.