简体   繁体   中英

Output animated GIF from file in PHP GD

I have an animated GIF image in a folder not accessible by the web. I would like to output that image through PHP GD, but when outputted, the GIF is unanimated.

Any help?

Instead of processing the image all over again in PHP-GD (you will be allocating far more resources that way instead of just reading a file that already exists.

  1. See if the file exists AND is readable
  2. Set the appropriate header for the intended output
  3. Return the file data to the client

     if (!is_readable($FILE)){ exit('Undefined file'); } header('Content-type: image/gif'); echo file_get_contents($FILE); 

You use a 'routing script' to access such images (or whatever types of files):

client->routing script->open file and forward content->client

The client requests an url like this: http://your.domain/some/path/image.php?id=animation

Use something like this to output the files content to the client:

<?php
    // some catalog (or database) where file-paths are kept
    $catalog = array(
        'animation' => '/path/to/animated_gif.gif';
    );

    // read which file the client wants to access
    $id = $_GET['animation'];

    // decide action on where that file is located physically
    if (!isset($catalog['id'])
    {
        // die with error message
        die('forbidden');
    }

    // send file content
    $success = readfile($catalog['id']);
    if (FALSE === $success)
    {
        // die with error message
        die('sorry, problems to give the file.');
    }

Note that for this to work the gif file does not ahve to be accessible directly via the http server. It is the local script that accesses the file based inside the local file system. So the script acts as a router for the clients request.

Inside the script to can do all sorts of fancy things:

  • count requests
  • block unpremitted requests
  • modify whatever you forward
  • ... endless possibilities ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM