Actions according to image type and size

That function looks like the kind of thing that would slow down a site. 🙂

The first thing I’d recommend is that you not run that function on the front end. Run it when the image is uploaded and store the result in the database using wp_update_attachment_metadata. You can then retrieve the data with wp_get_attachment_metadata. An administrator/editor can swallow a 40 second lag. Your readers shouldn’t have to.

If you have a huge number of images and can’t loop over them to alter the metadata so you have to run that function on image display, save the data with wp_update_attachment_metadata anyway so that it only has to run once.

Secondly, I would use get_post_mime_type at the top of that function and exit if the attachment isn’t a GIF. Only a GIF can be an animated GIF right?