5

Is there any way to prevent WordPress from cropping files based on their file extension. Actually, I want to disable cropping for GIF files because after cropping GIF image...

  1. GiF image does not remain animated
  2. Even it does remain animated, I will not be using those small GIF sizes.

So is there any way to disable thumbnail generation (for GIF) based on what file extension a image have.

  • Why not just use the original files in your theme/posts and not the thumbnails? – jounileander Sep 24 '14 at 11:11
  • What? How would I do that? – Robert hue Sep 24 '14 at 11:12
  • Wordpress creates different sizes of your image, but it doesn't remove the original file. How / where are you using your gif files? – jounileander Sep 24 '14 at 11:14
  • That's the question. I am using original GIF in website/theme but all other sizes are useless for GIF images. So I want to disable generating thumbnails for GIF images. – Robert hue Sep 24 '14 at 11:16
  • Okay. I see your point. I am just wondering if there is really a need to achieve this other than that. Gif files take minimal space on server, so I wouldn't waste a lot of time to achieve something that doesn't really effect the server nor your site. I will get back to you if I actually have an answer to your original question. – jounileander Sep 24 '14 at 11:20
6

Unsetting sizes can normally be done easily via intermediate_image_sizes_advanced hook. The only problem is, there is only the $sizes array available, so this sufficient for a general level, but not if you want to do this conditionally. The information needed to do it conditionally can be gathered with debug_backtrace - debug_backtrace is performance-wise somewhat expensive though. Anyhow, I imagine it could be done like shown below:

add_action(
    'intermediate_image_sizes_advanced',
    'wpse162413_unset_sizes_if_gif'
);
function wpse162413_unset_sizes_if_gif( $sizes ) {
    // we're only having the sizes available
    // we're using debug_backtrace to get additional information
    $dbg_back = debug_backtrace();
    // scan $dbg_back array for function and get $args
    foreach ( $dbg_back as $sub ) {
        if ( $sub[ 'function'] == 'wp_generate_attachment_metadata' ) {
            $args = $sub[ 'args' ]; 
        }
    }
    // attachment id
    $att_id       = $args[0];
    // attachment path
    $att_path     = $args[1];
    // split up file information
    $pathinfo = pathinfo( $att_path );
    // if extension is gif unset sizes
    if ( $pathinfo[ 'extension' ] == 'gif' ) {
        // get all intermediate sizes
        $intermediate_image_sizes = get_intermediate_image_sizes();
        // loop trough the intermediate sizes array
        foreach ( $intermediate_image_sizes as $size ) {
            // unset the size in the sizes array
            unset( $sizes[ $size ] );
        }
    }
    // return sizes
    return $sizes;
}
  • sorry I could not test it earlier. Just tested this functions and YES it seems to be working exactly like I require. You are awesome. Thanks a lot. – Robert hue Sep 27 '14 at 6:14
  • You said debug_backtrace is expensive for performance but since we will be uploading files from backend only then should it make any effect on overall website performance? – Robert hue Sep 27 '14 at 6:15
  • 1
    My pleasure, it's always nice if something actually works :) About the performance, I just add that as additional information, most likely it's fine, but you could have a site with a lot of users and all of them are sharing gif pics like crazy...you get the point. @Roberthue – Nicolai Sep 27 '14 at 6:37
  • That's great! I think it should not slow down my website because I have 5 users and they will be uploading maximum of 20-30 GIF images in 24 hours. – Robert hue Sep 27 '14 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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