3

Animated gifs are getting more popular on the web (again) and currently there is no good tool for resizing animated gifs.

So I want to disable resizing/generation of image sizes for the gif mimetype and just save the original gif.

Someone that can help me out with this? Which filter to use will be a good start.

  • What if the gif is not animated? Where are you using the animated gifs: in the post editor or as a post thumbnail? Did you try searching this site? – birgire Jun 14 '16 at 11:01
  • @birgire It is for personal use, so I would not upload a gif without it being animated. I could also make a function that check if its animated. I'm going to use animated gifs both in the editor and thumbnails. The reason why I want this is if a specific image size for a thumbnail/image doesn't exist it will fall back to the original. This way if I don't have any other sizes of the gif, it will fall back to the original animated gif. I tried searching the site and the codex, but have not found a clear answer. – Lasse M. Tvedt Jun 14 '16 at 11:30
4

image_make_intermediate_size was not the hook I was looking for, but intermediate_image_sizes_advanced.

Here is a working code:

function disable_upload_sizes( $sizes, $metadata ) {

    // Get filetype data.
    $filetype = wp_check_filetype($metadata['file']);

    // Check if is gif. 
    if($filetype['type'] == 'image/gif') {
        // Unset sizes if file is gif.
        $sizes = array();
    }

    // Return sizes you want to create from image (None if image is gif.)
    return $sizes;
}   
add_filter('intermediate_image_sizes_advanced', 'disable_upload_sizes', 10, 2); 
2

currently there is no good tool for resizing animated gifs

Resizing of animated gifs is supported by ImageMagick, which happens to be the default image library of WordPress. The only thing is WP doesn't support this filter in its default API to ImageMagick.

Fortunately, it is possible to intercept image_make_intermediate_size, the hook that produces the resized images. Here you could intercept gifs and have them handled in a different way. Perhaps you could take inspiration from this image sharpening plugin to see how to apply ImageMagick methods to your images.

  • yes convert is a great command line tool. – birgire Jun 14 '16 at 11:23
  • @birgire You mean it cannot be converted to work inside php? – cjbj Jun 14 '16 at 11:28
  • I haven't thought about it from the PHP context, I just like to work on the Linux command line and there this tool is ideal ;-) I haven't checked recently how WP handles animated gifs, but It would be interesting to check if it's possible to extend the WP_Image_Editor_Imagick class if needed to support it. Maybe there're other ways to support it within WordPress? – birgire Jun 14 '16 at 11:35
  • 2
    This post seems to suggest you can get convert working in php as well. – cjbj Jun 14 '16 at 11:35
  • But for the purpose of OP building a fairly complex plugin to get this done is probably a bit too much. – cjbj Jun 14 '16 at 11:37

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.