GifCreator

repository·master·Indexed 43 days ago

https://github.com/sybio/gifcreator

A PHP class for generating animated GIF images from multiple sources, including file paths, URLs, binary data, and PHP image resources. It provides methods to define frame durations and loop counts via create(), retrieve the resulting binary data with getGif(), and reset the instance state.

Tokens
1.3K
Snippets
5
Records
7
Agent score
6%

What's inside GifCreator

  1. GifCreator behavior: Transparency and Dimensions

    master

    When using GifCreator, be aware of how it handles image properties:

    • Transparency: Transparency is determined by the first frame provided. Transparency will only be preserved in the output if all subsequent frames share the same transparent background.
    • Dimensions: The dimensions of the final animated GIF are determined by the first frame. If your input frames have different dimensions, you should resize them to match the first frame's dimensions before passing them to GifCreator (the documentation suggests using ImageWorkshop for resizing).
  2. Create an animated GIF with GifCreator

    master

    To create an animated GIF, provide an array of image sources and an array of durations (in milliseconds) to the create() method.

    Supported image sources in the $frames array:

    • PHP image resources (e.g., from imagecreatefrompng())
    • Local file paths
    • Binary image data (e.g., from file_get_contents())
    • Image URLs

    The create() method accepts three parameters:

    1. $frames: An array of image sources.
    2. $durations: An array of integers representing the duration of each frame in milliseconds.
    3. $loops: The number of times the GIF should loop. Set this to 0 for an infinite loop.
    // 1. Prepare frames (resources, paths, binary, or URLs)
    $frames = array(
        imagecreatefrompng("/../images/pic1.png"),
        "/../images/pic2.png",
        file_get_contents("/../images/pic3.jpg"),
        'http://thisisafakedomain.com/images/pic4.jpg',
    );
    
    // 2. Prepare durations in milliseconds
    $durations = array(40, 80, 40, 20);
    
    // 3. Initialize and create the GIF (5 loops)
    $gc = new GifCreator();
    $gc->create($frames, $durations, 5);
  3. Retrieve and output the GIF binary

    master

    After calling create(), use the getGif() method to retrieve the raw binary data of the generated animated GIF. You can then output this binary directly to a browser or save it to a file.

    // Get the binary data
    $gifBinary = $gc->getGif();
    
    // Option A: Display in a web browser
    header('Content-type: image/gif');
    header('Content-Disposition: filename="butterfly.gif"');
    echo $gifBinary;
    exit;
    
    // Option B: Save to a file
    file_put_contents('/myfolder/animated_picture.gif', $gifBinary);
  4. Create an animated GIF with GifCreator::create()

    master

    The create() method is the primary entry point for generating an animated GIF. It accepts an array of frames, an array of durations, and a loop count.

    Frame Types Supported:

    • File paths: A string representing a local file path.
    • URLs: A string representing a valid image URL.
    • Binary data: A string containing raw image binary data.
    • Image Resources: PHP GD image resources.

    Parameters:

    • $frames (array): An array of the frame types listed above.
    • $durations (array): An array of integers representing the duration of each frame.
    • $loop (integer): The number of times the GIF should loop. Set to 0 for an infinite loop.
    use GifCreator\GifCreator;
    
    $creator = new GifCreator();
    
    $frames = [
        'path/to/image1.png',
        'https://example.com/image2.jpg',
        $gdResource // A GD image resource
    ];
    
    $durations = [100, 200, 150]; // Durations in milliseconds
    $loop = 0; // Infinite loop
    
    $gifBinaryData = $creator->create($frames, $durations, $loop);
    
    // Save the resulting GIF to a file
    file_put_contents('animation.gif', $gifBinaryData);
  5. Error codes and exceptions in GifCreator

    master

    The GifCreator class throws Exception objects when certain conditions are not met. The error messages include specific error codes:

    • ERR00: Does not supported function for only one image.
    • ERR01: Source is not a GIF image.
    • ERR02: You have to give resource image variables, image URL or image binary sources in $frames array.
    • ERR03: Does not make animation from animated GIF source.