Porto

Thumbnail From Animated GIF

Thumbnail From Animated GIF

Seperating the individual images in an animated gif with PHP has never been easier than now with the Imagick extension. The Imagick extension allows a vast array of functionality with image manipulation. Here an animated gif is iterated over frame by frame and each frame is resized and thumbnailed. Note the use of writeImages() instead of writeImage(). A handy trick when needing to thumbnail an animated gif.


<?php

try
{
    
/*** Read in the animated gif ***/
    
$animation = new Imagick("animation.gif");

    
/*** Loop through the frames ***/
    
foreach ($animation as $frame)
    {
        
/*** Thumbnail each frame ***/
        
$frame->thumbnailImage(100100);
    
        
/*** Set virtual canvas size to 100x100 ***/
        
$frame->setImagePage(10010000);
    }

    
/*** Write image to disk. Notice writeImages instead of writeImage ***/
    
$animation->writeImages("animation_thumbnail.gif");

    echo 
"Images written";
}
catch(
Exception $e)
{
    echo 
$e->getMessage();
}

?>