PHP imagegif() Function: Output GIF Image to Browser or File
The article explains PHP's imagegif() function, detailing its parameters, return values, and providing a complete example that creates a true‑color image, adds text, sets the appropriate header, outputs the GIF directly to the browser, and cleans up the resource.
imagegif() creates a GIF image from an image resource, optionally saving it to a file or outputting the raw image data directly to the browser.
Parameters: $image – an image resource returned by functions such as imagecreate() or imagecreatefrom* ; $filename – optional file path to save the GIF, or NULL to send the image stream to the output buffer.
Return value: on success the function returns TRUE (or the image resource), and on failure it returns FALSE .
Example usage demonstrates creating a 100 × 100 true‑color image, filling it with white, drawing a string, sending the Content-Type: image/gif header, outputting the image with imagegif() , and finally destroying the image resource.
<?php
// Create a new image instance
$im = imagecreatetruecolor(100, 100);
// Set background to white
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
// Write text on the image
imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);
// Output image to browser
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
?>Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.