I want to do some actions according to image type and size, but the script I'm using is kind of heavy and is skyrocketing my wait time (first byte) from 5 to even 40 seconds (usually it's bellow a second).

$image_data = wp_get_attachment_image_src( get_post_thumbnail_id($post -> ID), 'full');

$animated = is_animated_gif($image_data[0]);
$width = $image_data[1];

    $size = 'full';
elseif(!$animated && ($width >= '558')) {
    $size = 'medium';
    $size = 'full';

checking animated gifs with this function

function is_animated_gif($filename){
$filecontents = file_get_contents($filename);

$str_loc = 0;
$count = 0;
while ($count < 2) { # There is no point in continuing after we find a 2nd frame

    $where1 = strpos($filecontents, "\x00\x21\xF9\x04", $str_loc);

    if ($where1 === false) {
    } else {

        $str_loc = $where1 + 1;
        $where2 = strpos($filecontents, "\x00\x2C", $str_loc);

        if ($where2 === false) {
        } else {
            if ($where1 + 8 == $where2) {
            $str_loc = $where2+1;


if ($count > 1) {
    return true ;
} else {
    return false;

Do you have any ideas?

  • I've done some additional testing and the result is that the only thing slowing my page is the is_animated_gif function. I'm still looking for a solution to that... – Georgi Demirev Jan 26 '13 at 13:55

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?

  • That is a great advice! Now, I've over 5000 posts and it'd be tedious to go through them all. The good thing is that I've less than a dozen unanimated gifs so it wouldn't be such a huge loss if, for the greater good, they go in the same lane as the others. I would just add a check if the image is gif and process them all (animated & unanimated) the same way. One thing though, what would you consider faster WP's get_post_mime_type or PHP's pathinfo? – Georgi Demirev Jan 26 '13 at 17:15
  • I haven't compared get_post_mime_type to pathinfo in terms of performance but finfo_file is probably a better comparison case. Also get_post_mime_type pulls precalculated data from the DB. – s_ha_dum Jan 26 '13 at 17:24
  • finfo_file is a bit to complicated for my needs. I'll stick with pathinfo($filename, PATHINFO_EXTENSION); Thanks for the tip :) – Georgi Demirev Jan 26 '13 at 19:36

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.