1

I’m trying to get the contents of imagegif() to a variable, or save a gif in a variable.

I guess I could do a temporary file and file_get_contents(), but I’m searching for a simpler method. I don’t use Imagemagick, I use GD.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
Vixxs
  • 569
  • 7
  • 21

1 Answers1

2

You should be able to use an output buffer. This is just a stripped down example from the manual with the aforementioned output buffer:

function makeGif()
    {
        # Start capturing the content
        ob_start();
        # -------- Just do your image script starting here --------- #
        $im = imagecreatetruecolor(100, 100);
        imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
        imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);
        imagegif($im);
        imagedestroy($im);
        # -------- stopping your image here -------- #
        # This assigns the content of the output
        $img    =   ob_get_contents();
        # Stop capturing the output
        ob_end_clean();
        # Send back the content
        return $img;
    }

# $image should now contain the gif data
$image = makeGif();
Rasclatt
  • 12,498
  • 3
  • 25
  • 33