PHP: Hypertext Preprocessor (original) (raw)

imagegif

(PHP 4, PHP 5, PHP 7, PHP 8)

imagegif — Output image to browser or file

Description

The image format will be GIF87a unless the image has been made transparent withimagecolortransparent(), in which case the image format will be GIF89a.

Parameters

image

A GdImage object, returned by one of the image creation functions, such as imagecreatetruecolor().

file

The path or an open stream resource (which is automatically closed after this function returns) to save the file to. If not set or [null](reserved.constants.php#constant.null), the raw image stream will be output directly.

Return Values

Returns [true](reserved.constants.php#constant.true) on success or [false](reserved.constants.php#constant.false) on failure.

Caution

However, if libgd fails to output the image, this function returns [true](reserved.constants.php#constant.true).

Changelog

Version Description
8.0.0 image expects a GdImage instance now; previously, a valid gd resource was expected.

Examples

Example #1 Outputting an image using imagegif()

<?php // Create a new image instance $im = imagecreatetruecolor(100, 100);// Make the background white imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);// Draw a text string on the image imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);// Output the image to browser header('Content-Type: image/gif');imagegif($im); ?>

Example #2 Converting a PNG image to GIF using imagegif()

<?php// Load the PNG $png = imagecreatefrompng('./php.png');// Save the image as a GIF imagegif($png, './php.gif');// We're done echo 'Converted PNG image to GIF with success!'; ?>

Notes

Note:

The following code snippet allows you to write more portable PHP applications by auto-detecting the type of GD support which is available. Replace the sequence header ("Content-Type: image/gif"); imagegif ($im); by the more flexible sequence:

`<?php
// Create a new image instance
$im = imagecreatetruecolor(100, 100);// Do some image operations here

// Handle output

if(function_exists('imagegif'))
{
// For GIF
header('Content-Type: image/gif');imagegif($im);
}
elseif(function_exists('imagejpeg'))
{
// For JPEG
header('Content-Type: image/jpeg');imagejpeg($im, NULL, 100);
}
elseif(function_exists('imagepng'))
{
// For PNG
header('Content-Type: image/png');imagepng($im);
}
elseif(function_exists('imagewbmp'))
{
// For WBMP
header('Content-Type: image/vnd.wap.wbmp');imagewbmp($im);
}
else
{
die('No image support in this PHP server');
}
?>`

Note:

You can use the functionimagetypes() for checking the presence of the various supported image formats:

<?php if(imagetypes() & IMG_GIF) { header('Content-Type: image/gif'); imagegif($im); } elseif(imagetypes() & IMG_JPG) { /* ... etc. */ } ?>

See Also

Found A Problem?

polone at townnews dot com

23 years ago

`read also RFC2557: http://www.ietf.org/rfc/rfc2557.txt
For handling inline images in email.

I've been playing around with the "data" URL scheme as proposed by RFC 2397 which states how to perform inline, bas64 encoded images. A number of browsers support this format from some of my tests and would be an interesting way of removing overhead from multiple HTTP connections. Basically, the IMG tag would be:

Larry

Something like that. Note also that I start the URI with "/-/" before the rest of the data scheme spec. If you don't start it with this, it won't work in a lot of the different browsers I tested (such as IE). Note this is useful for very small images only (as most browsers appear to have a limitation on the size of HTML element data of 1024). Browsers where this syntax worked that I tested are the following:

IE 6.x (windows)
Mozilla 0.97+ (linux)
Opera 5, 6 (windows)
Netscape 4.7+ (mac, windows)
IE 5 (macintosh)

This should work for other image types as well, such as PNG. JPEG files aren't really suggested (usually, these files are too large). BTW - there is no advantage to this method if the image will appear more than ONCE in the page because you will be transmitting the same data multiple times as opposed to just once (most browsers realize that already downloaded data that has multiple references only requires one HTTP call).

Consider using this method if you want to make a single PHP program that outputs both text and an image AND you want to make only on HTTP call. Cheers.

`

mail at ignore dot area dot nospam dot joshho dot com

13 years ago

`apparently GD does not support animated GIFs.

instead, we're stuck with the old fashioned way:

`

stefan at colulus dot com

16 years ago

`I worked out a script that allows the transfer of alphanumeric data to be placed on an image. The HTML feature is img src and the php feature is imagettftext. This simple code will increment from 1 to 3 on images.

code:

"; print ""; print "

"; } ?>color1=ImageColorAllocate(color1 = ImageColorAllocate(color1=ImageColorAllocate(im,0x66,0xCC,0x00); color2=ImageColorAllocate(color2 = ImageColorAllocate(color2=ImageColorAllocate(im,0x33,0x66,0x00); color3=ImageColorAllocate(color3 = ImageColorAllocate(color3=ImageColorAllocate(im,0x00,0x99,0x00); color4=ImageColorAllocate(color4 = ImageColorAllocate(color4=ImageColorAllocate(im,0x3D,0x3D,0x3D);// image creation ImageFilledRectangle($im,1,1,76,76,$color1); ImageFilledpolygon($im, array (76,1,1,76,76,76),3,$color2); ImageFilledRectangle($im,5,5,72,72,$color3);// determine numeric center of image size=ImageTTFBBox(45,0,′impact′,size = ImageTTFBBox(45,0,'impact',size=ImageTTFBBox(45,0,impact,_GET['$text']); X=(77−(abs(X = (77 - (abs(X=(77(abs(size[2]- size[0])))/2;<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Y</mi><mo>=</mo><mostretchy="false">(</mo><mostretchy="false">(</mo><mn>77</mn><mo>−</mo><mostretchy="false">(</mo><mi>a</mi><mi>b</mi><mi>s</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">Y=((77−(abs(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6833em;"></span><spanclass="mordmathnormal"style="margin−right:0.22222em;">Y</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mopen">((</span><spanclass="mord">77</span><spanclass="mspace"style="margin−right:0.2222em;"></span><spanclass="mbin">−</span><spanclass="mspace"style="margin−right:0.2222em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mopen">(</span><spanclass="mordmathnormal">ab</span><spanclass="mordmathnormal">s</span><spanclass="mopen">(</span></span></span></span>size[5]−size[0])))/2; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Y</mi><mo>=</mo><mo stretchy="false">(</mo><mo stretchy="false">(</mo><mn>77</mn><mo>−</mo><mo stretchy="false">(</mo><mi>a</mi><mi>b</mi><mi>s</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">Y = ((77 - (abs(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">Y</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">((</span><span class="mord">77</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span><span class="mord mathnormal">ab</span><span class="mord mathnormal">s</span><span class="mopen">(</span></span></span></span>size[5] - size[0])))/2;<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>Y</mi><mo>=</mo><mostretchy="false">(</mo><mostretchy="false">(</mo><mn>77</mn><mo></mo><mostretchy="false">(</mo><mi>a</mi><mi>b</mi><mi>s</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">Y=((77(abs(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.6833em;"></span><spanclass="mordmathnormal"style="marginright:0.22222em;">Y</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mopen">((</span><spanclass="mord">77</span><spanclass="mspace"style="marginright:0.2222em;"></span><spanclass="mbin"></span><spanclass="mspace"style="marginright:0.2222em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mopen">(</span><spanclass="mordmathnormal">ab</span><spanclass="mordmathnormal">s</span><spanclass="mopen">(</span></span></span></span>size[5]size[3])))/2 + (abs($size[5] - $size[3])));//places numeric information on image ImageTTFText($im,45,0,($X-1),$Y,$color4,'impact',$_GET['$text']);//returns completed image to calling script Header('Content-Type: image/png'); Imagegif($im);?>

`

rokfaith at gmail dot com

18 years ago

`to create an animated gif with gifsicle, but without storing temporary images on disk:

cmd=′gifsicle−−loop−O1−−multifile−−delay25−>′.cmd = 'gifsicle --loop -O1 --multifile --delay 25 - > '.cmd=gifsicleloopO1multifiledelay25>.outfile; desc=array(0=>array("pipe","r"),1=>array("pipe","w"),2=>array("pipe","w"));<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>r</mi><mi>o</mi><mi>c</mi><mo>=</mo><mi>p</mi><mi>r</mi><mi>o</mi><msub><mi>c</mi><mi>o</mi></msub><mi>p</mi><mi>e</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">proc=procopen(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.625em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">p</span><spanclass="mordmathnormal">roc</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">p</span><spanclass="mordmathnormal">ro</span><spanclass="mord"><spanclass="mordmathnormal">c</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">o</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">p</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>cmd,desc = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w")); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>r</mi><mi>o</mi><mi>c</mi><mo>=</mo><mi>p</mi><mi>r</mi><mi>o</mi><msub><mi>c</mi><mi>o</mi></msub><mi>p</mi><mi>e</mi><mi>n</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">proc = proc_open(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">roc</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">p</span><span class="mord mathnormal">ro</span><span class="mord"><span class="mord mathnormal">c</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">o</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">p</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mopen">(</span></span></span></span>cmd, desc=array(0=>array("pipe","r"),1=>array("pipe","w"),2=>array("pipe","w"));<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>p</mi><mi>r</mi><mi>o</mi><mi>c</mi><mo>=</mo><mi>p</mi><mi>r</mi><mi>o</mi><msub><mi>c</mi><mi>o</mi></msub><mi>p</mi><mi>e</mi><mi>n</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">proc=procopen(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.625em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal">p</span><spanclass="mordmathnormal">roc</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">p</span><spanclass="mordmathnormal">ro</span><spanclass="mord"><spanclass="mordmathnormal">c</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.1514em;"><spanstyle="top:2.55em;marginleft:0em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">o</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">p</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mopen">(</span></span></span></span>cmd,desc, $pipes); if (!is_resource($proc)) { die('Unable to start gifsicle'); } for ($frame=0; frame<frame<frame<total_frames; $frame++) { image=RenderFrame(image = RenderFrame(image=RenderFrame(frame); ob_start(); imagegif($image); fwrite($pipes[0], ob_get_contents()); ob_end_clean(); imagedestroy($image); } fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); proc_close($proc); ?>

just define $outfile and RenderFrame(), and that's it.

`

jemore at nospam dot m6net dot fr

21 years ago

If you open a truecolor image (with imageCreateFromPng for example), and you save it directly with imagegif, you can have a 500 internal server error. You must use imageTrueColorToPalette to reduce to 256 colors before saving the image in GIF format.

Lauri Harpf

19 years ago

`Using to dynamically generate images is a bit problematic regarding cache. Unless caching is activated, IE seems to get confused about the type of the image when attempting to save it. A .GIF created in the above way causes the browser to suggest saving the image with .BMP, not .GIF.

A solution is to activate cache with session_cache_limiter('public'); in "image.php", after which IE will correctly save as .GIF. If you do not want the cache to block any changes in the dynamic image, make sure that the SRC keeps changing with every reload. Something like "image.php/" . mt_rand(1,100000) . ".gif" seems to work well.

Might be trivial to some, but I spent a couple of hours figuring out why IE always wants to save my dynamic .GIF's as .BMP's.

`

kremlin at home dot com

24 years ago

Animated GIFs as well as transparent GIFs qualify as GIF89a's and you should use ImageColorTransparent().

grant k.

14 years ago

`It should be noted that if you only want to "save" the file, and not display it to the browser, you should catch the imagegif into a variable.

Example:

savefiletopath);//Savesfileandattemptstodisplayit,butwillthrowanerrormessageimagegif(save_file_to_path);//Saves file and attempts to display it, but will throw an error messageimagegif(savefiletopath);//Savesfileandattemptstodisplayit,butwillthrowanerrormessageimagegif(image, $save_file_to_path);//Only displays, never saves as a file imagegif($image);//Note: In [my] third example, for displaying only, it is probably good to use the "header('Content-type: image/gif'); declaration, but it's not needed in the first example for saving as a .gif file. ?>

`

Leigh Purdie

19 years ago

`Simple animated-gif hack (requires ImageMagick):

white=imagecolorallocate(white=imagecolorallocate(white=imagecolorallocate(im,255,255,255); imagerectangle($im,$count,$count,200-$count,200-$count,$white); $icount++; tcount=sprintf("tcount=sprintf("%04d",tcount=sprintf("icount); imagegif($im,"/tmp/test-$tcount.gif"); imagedestroy($im); } exec("/usr/bin/convert -delay 2 -loop 10 /tmp/test*.gif /var/www/html/Tests/Test-Anim.gif"); ?>

`

david at hooshla dot com

25 years ago

`This is how you load and display an image file:

Note that there are no new-lines in the content type header.

`