Timber\ImageHelper::is_animated_gif PHP Method

is_animated_gif() public static method

checks to see if the given file is an aimated gif
public static is_animated_gif ( string $file ) : boolean
$file string local filepath to a file, not a URL
return boolean true if it's an animated gif, false if not
    public static function is_animated_gif($file)
    {
        if (strpos(strtolower($file), '.gif') == -1) {
            //doesn't have .gif, bail
            return false;
        }
        //its a gif so test
        if (!($fh = @fopen($file, 'rb'))) {
            return false;
        }
        $count = 0;
        //an animated gif contains multiple "frames", with each frame having a
        //header made up of:
        // * a static 4-byte sequence (\x00\x21\xF9\x04)
        // * 4 variable bytes
        // * a static 2-byte sequence (\x00\x2C)
        // We read through the file til we reach the end of the file, or we've found
        // at least 2 frame headers
        while (!feof($fh) && $count < 2) {
            $chunk = fread($fh, 1024 * 100);
            //read 100kb at a time
            $count += preg_match_all('#\\x00\\x21\\xF9\\x04.{4}\\x00[\\x2C\\x21]#s', $chunk, $matches);
        }
        fclose($fh);
        return $count > 1;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Performs the actual image manipulation,
  * including saving the target file.
  *
  * @param  string $load_filename filepath (not URL) to source file
  *                               (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
  * @param  string $save_filename filepath (not URL) where result file should be saved
  *                               (ex: /src/var/www/wp-content/uploads/my-pic-300x200-c-default.jpg)
  * @return boolean|null                  true if everything went fine, false otherwise
  */
 public function run($load_filename, $save_filename)
 {
     //should be resized by gif resizer
     if (\Timber\ImageHelper::is_animated_gif($load_filename)) {
         //attempt to resize
         //return if successful
         //proceed if not
         $gif = self::run_animated_gif($load_filename, $save_filename);
         if ($gif) {
             return true;
         }
     }
     $image = wp_get_image_editor($load_filename);
     if (!is_wp_error($image)) {
         $crop = self::get_target_sizes($load_filename);
         $image->crop($crop['x'], $crop['y'], $crop['src_w'], $crop['src_h'], $crop['target_w'], $crop['target_h']);
         $result = $image->save($save_filename);
         if (is_wp_error($result)) {
             // @codeCoverageIgnoreStart
             Helper::error_log('Error resizing image');
             Helper::error_log($result);
             return false;
             // @codeCoverageIgnoreEnd
         } else {
             return true;
         }
     } else {
         if (isset($image->error_data['error_loading_image'])) {
             // @codeCoverageIgnoreStart
             Helper::error_log('Error loading ' . $image->error_data['error_loading_image']);
         } else {
             Helper::error_log($image);
             // @codeCoverageIgnoreEnd
         }
     }
 }