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];
if($animated){
$size="full";
}
elseif(!$animated && ($width >= '558')) {
$size="medium";
}else{
$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) {
break;
} else {
$str_loc = $where1 + 1;
$where2 = strpos($filecontents, "\x00\x2C", $str_loc);
if ($where2 === false) {
break;
} else {
if ($where1 + 8 == $where2) {
$count++;
}
$str_loc = $where2+1;
}
}
}
if ($count > 1) {
return true ;
} else {
return false;
}
}
Do you have any ideas?