wp-media / imagify-plugin

Speed up your website with lighter images without losing quality.
https://imagify.io
70 stars 25 forks source link

Add a filter to exclude file types when running bulk optimization #516

Open iCaspar opened 4 years ago

iCaspar commented 4 years ago

We currently have a filter imagify_auto_optimize_attachment to disable certain file types from being optimized automatically in the upload process. Ideally we should have a filter for the bulk optimization process as well.

tpetcoukdev commented 1 year ago

Is there a code change that could be made to get this result. We are setting up the plugin and have over 10,000 images and 10,000 PDFs. The PDFs take considerably longer and we would like to focus on images for now. I understand that a code change would be overwritten on updates but I really do need this functionality before I hunt for a different plugin. Many thanks.

blogtutor commented 1 year ago

We just ran into an issue with this, on a website that sells PDF sewing patterns. The Imagify bulk optimization flattened the layers and was causing issues for the customers.

I've added the code to exclude PDFs from Auto Optimization on upload, but if they can also be excluded from bulk optimization that would really help.

tpetcoukdev commented 1 year ago

I've managed to turn off bulk optimsation for PDFs by modifying /inc/functions/attachements.php

Comment out line 26 as shown below

function imagify_get_mime_types( $type = null ) {
    $mimes = array();

    if ( 'not-image' !== $type ) {
        $mimes = array(
            'jpg|jpeg|jpe' => 'image/jpeg',
            'png'          => 'image/png',
            'gif'          => 'image/gif',
        );
    }

    if ( 'image' !== $type ) {
                //Comment line below to prevent optimisation (including bulk) of pdfs
        //$mimes['pdf'] = 'application/pdf';
    }

    return $mimes;
}

This will be overwritten when the plugin is updated but is a working hotfix whilst we wait for a an update from imagify.

Hope this helps.

mountbatt commented 9 months ago

+1 My pdf loose all accessibility features on upload / bulk. We need a „exclude file types“ setting in the plugin etc…

Garconis commented 8 months ago

Pretty please? 3 years later?

JerissCloudCenter commented 8 months ago

Still no news on this? This is a serious issue...

@tpetcoukdev is there no way to add this code in functions.php to avoid the code from disappearing with eveey update?

tatof commented 8 months ago

Bump! what is the status? Just switched from Short pixel to this plugin. But there are some serious limits like not having the option to disable .pdf optimalisation

JerissCloudCenter commented 8 months ago

yes... https://github.com/wp-media/imagify-plugin/issues/516#issuecomment-1369647979 is the only solution I use in the meantime... but after every release update, you have to re-do this action

tpetcoukdev commented 7 months ago

Still no news on this? This is a serious issue...

@tpetcoukdev is there no way to add this code in functions.php to avoid the code from disappearing with eveey update?

Possibly, I'll try and take some time to have a look at it this/next week. For now I can confirm that editing the code still works on this end. It would be good if Imagify could include this in an update, it really would not take much to have a checkbox in the plugin settings to "disable PDF files from bulk optimisation" - I'll even do the work if they want and they can simply test it and push it into the next update! lol

tpetcoukdev commented 7 months ago

Had a quick look at this, and it doesn't seem like there is a way to do this as no hooks are available on the function to allow us to override it from functions.php - there are some "hacky" workarounds but it looks like edits would still need to be made to the plugin files, which of course are lost on update.

Ref: https://stackoverflow.com/questions/33147735/override-plugin-function-in-wordpress

rhw2008 commented 6 months ago

I have tried t install imagify-helpers-master.zip to be able to skip animated gif images but I get an error after uploading the plugin saying :

The package could not be installed. No valid plugins were found.

Plugin installation failed.

Any help please ?

vyskoczilova commented 5 months ago

+1 Optimizing images at the site with a couple of GB of PDFs led to twice as much storage space being used because the PDFs' originals were backed up before optimization. I don't want to optimize them at the very first.

joffreyc-vortexsolution commented 2 months ago

Hello! Current solution for the plugin is to generate an error before optimization.

    /**
     * Stop processing optimization when it is a PDF.
     * The downside is that we have to return an error.
     *
     * @return null|WP_Error
     */
    add_filter(
      'imagify_before_optimize_size',
      function( $null, $process_class, $file ) {
        if ( 'application/pdf' === mime_content_type( $file->get_path() ) ) {
          return new WP_Error( 'no_pdf_optimization', 'PDF Optimization is disabled.' );
        }
        return $null;
      },
      10,
      3
    );

    /**
     * Prevent automatic optimization for PDF.
     *
     * @param  bool  $optimize      True to optimize, false otherwise.
     * @param  int   $attachment_id Attachment ID.
     * @return bool
     */
    function no_auto_optimize_pdf( $optimize, $attachment_id ) {
      if ( ! $optimize ) {
        return false;
      }
      return 'application/pdf' !== get_post_mime_type( $attachment_id );
    }
    add_filter( 'imagify_auto_optimize_attachment', 'no_auto_optimize_pdf', 10, 2 );

Wait and see if we can have an option that will prevent this in a good way.

JerissCloudCenter commented 2 months ago

@joffreyc-vortexsolution

Thanks for your code. This code goes in child theme > functions.php right?

So if I understand well, when the bulk optimisation runs, it will throw an error for PDFs but the bulk will not get blocked right? It will just skip and proceed to next file ?

joffreyc-vortexsolution commented 2 months ago

@JerissCloudCenter

Yes, in your functions.php file.

Yes, wherever you trigger it (Bulk, media list, media item, etc.) you will see the error.

It will not stop the bulk optimization. It will say the error text for the file and go to the next file.

JerissCloudCenter commented 2 months ago

Ok I will try it right now. It looks like a good enough workaround for me, while waiting for the next 10 years for Imagify to implement this feature -_- Thanks!

JerissCloudCenter commented 2 months ago

Perfect thanks! Your code works fine, it correctly blocks the optimization of PDFs.

image

image

tatof commented 2 months ago

I switched back to Shortpixel.. More advanced settings including this one, AVIF, webp etc. And also unlimited package now.