Search code examples
phpgdgifanimated-gif

Display animated GIF from outside public directory (GD?)


Once users upload their images to a non public (i.e outside htdocs or public_html folder) folder, I use GD to fetch the image from that folder. This is all in the name of security, as the images themselves are never displayed without being processed.

The issue lies in animated GIFs, which GD is only capable of displaying by showing only it's first frame.

I know that there are classes out there that can slice the gifs up into their respective frames and "glue" them back together, but I'm looking to display these on the fly...

I don't want to create a new animated gif and show that, but rather have a GD script that renders the image from the information it gets from the photo directory, located outside of the public folder.

Thanks so much!


Solution

  • If you're not manipulating the image at all, I would just return the contents of the file with the correct header.

    For example:

    <?php 
    
    $file = 'whatever.gif';
    
    // Do whatever checks you want on permissions etc
    
    header('Content-type: image/gif');
    readfile($file);
    
    ?>
    

    Readfile outputs the contents of the file, and the header makes sure the browser sees it as a gif and shows it. Because you're not using GD it will still be animated.

    The catch with this method is you'll need to know the content type for each file to server it correctly.

    http://php.net/manual/en/function.readfile.php