 |
imagegif (PHP 3, PHP 4, PHP 5) imagegif -- 以 GIF 格式将图像输出到浏览器或文件 说明bool imagegif ( resource image [, string filename] )
imagegif() 从 image
图像以 filename 为文件名创建一个
GIF 图像。image
参数是 imagecreatetruecolor() 函数的返回值。
图像格式为 GIF87a。如果用了
imagecolortransparent() 使图像为透明,则其格式为
GIF89a。
filename 参数为可选,如果省略,则原始图像流将被直接输出。通过
header() 发送 Content-type: image/gif 可以使 PHP
脚本直接输出 GIF 图像。
注:
不过从 GD 库 1.6 起所有的 GIF 支持都移除了,如果使用这些 GD
库时本函数不可用。希望在 2004 年中期能够发布支持
GIF 的 GD 库。更多信息见
GD Project 站点。
以下代码段通过自动检测 GD 支持的图像类型来写出移植性更好的
PHP 程序。用更灵活的代码替代了原来的
header("Content-type: image/gif");
imagegif($im);:
参见 imagepng(),imagewbmp(),imagejpeg()
和 imagetypes()。
fieldy
31-Jan-2006 07:54
For using the new animated gif functions of gd you can build in some functions in php. I took the patch from http://hyvatti.iki.fi/~jaakko/sw/. But it's a patch on php 5.0.2. I made it on php 5.1.2.
On http://www.linuxforhomies.be/view_artikel.jsp?id=10 you can see how to do it. Sorry that it's in dutch, but I think it's understandable.
It adds following functions:
int imagegifanimbegin(int im [, string filename [, int GlobalColormap [, int Loops]]]);
int imagegifanimadd(int im [, string filename [, int LocalColormap [, LeftOfs [, int TopOfs [, int Delay [, int Disposal [, int previm]]]]]]]);
int imagegifanimend([string filename]);
cascade at mylifesucks dot de
29-Nov-2005 08:02
I would rather use gifsicle ( http://www.lcdf.org/gifsicle/ ) instead of convert. It runs much faster with big animations. I got large animations with over 500 frames rendered where convert was killed by my system because of memory consumption. And it contains pretty well optimization.
A sample gifsicle usage (not much optimized):
<?
// Sample gifsicle usage to create a scrolling text
$gifsicle = "/usr/local/bin/gifsicle"; # http://www.lcdf.org/gifsicle/
$time = time();
for ($rend_frame = 0; $rend_frame <= 26; $rend_frame++) {
$im = @imagecreatetruecolor(300, 20);
$background = imagecolorallocate($im, 0, 0, 100);
$red = imagecolorallocate($im, 255, 0, 0);
$filename = "/tmp/giftest_".$time."_".str_pad($rend_frame, 10, "0", STR_PAD_LEFT).".gif";
$framefiles[] = $filename;
imagestring($im, 3, ($rend_frame * 15)-80, 3, "Animation rox", $red);
imagegif($im,$filename);
imagedestroy($im);
};
$outfile = "/tmp/giftest_".$time.".gif";
$cmd = "$gifsicle --loop -O1 --delay 25 /tmp/giftest_".$time."*> $outfile";
exec($cmd, $out, $err);
foreach ($framefiles as $framefile) {
if (file_exists($framefile)) unlink($framefile);
};
header('Content-type: image/gif');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: inline; filename=test.gif');
header('Content-Length: '.@filesize($outfile));
readfile($outfile);
unlink($outfile);
?>
Or get patches from http://hyvatti.iki.fi/~jaakko/sw/ to be able to do it all inside php...
Leigh Purdie
04-Jul-2005 08:17
Simple animated-gif hack (requires ImageMagick):
<html><body>
<?php
$icount=0;
for($count=0;$count<40;$count++) {
$im=imagecreate(200,200);
imagecolorallocate($im,0,0,255);
$white=imagecolorallocate($im,255,255,255);
imagerectangle($im,$count,$count,200-$count,200-$count,$white);
$icount++;
$tcount=sprintf("%04d",$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");
?>
<img src="/Tests/Test-Anim.gif">
</body>
</html>
Lauri Harpf
24-Jun-2005 12:25
Using <IMG SRC="image.php"> 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.
jemore at nospam dot m6net dot fr
23-Nov-2003 02:24
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.
tom et lenderlab dit com
06-Dec-2002 05:08
The rgb2gif utility included in giflib can be used for fast and easy gif output with any version of GD
http://ietpd1.sowi.uni-mainz.de/cgi-bin/man2html?rgb2gif
use either the rgboutput or the rgboutput_truecolor functions, depending on your GD version/style of image you're working with:
<?
$x=40;
$y=40;
//make a simple image with some text
$im = imagecreate($x, $y);
$bg = imagecolorallocate($im, 0,0,0);
imagefill($im, 0,0, $bg);
$red=imagecolorallocate($im, 255, 0, 0);
imagestring($im, 1, 2, 8, "testing", $red);
//output the 24 bit rgb-formatted image to file
$f = fopen("image.rgb", "w");
fwrite($f, rgboutput($im));
imagedestroy($im);
//load that file into rgb2gif and capture the output
$status = `rgb2gif -1 -s $x $y image.rgb`;
$f = fopen("test.gif", "w");
fwrite($f, $status);
function rgboutput($im) {
$x = imagesx($im);
$y = imagesy($im);
$output="";
for($i=0;$i<$y;$i++) {
for($j=0;$j<$x;$j++) {
$c=ImageColorAt($im, $j, $i);
$c = imagecolorsforindex($im, $c);
$c = array_map("chr", $c);
$output.=$c['red'].$c['green'].$c['blue'];
}
}
return $output;
}
function rgboutput_truecolor($im) {
$x = imagesx($im);
$y = imagesy($im);
$output="";
for($i=0;$i<$y;$i++) {
for($j=0;$j<$x;$j++) {
$rgb=ImageColorAt($im, $j, $i);
$r = chr(($rgb >> 16) & 0xFF);
$g = chr(($rgb >> 8) & 0xFF);
$b = chr($rgb & 0xFF);
$output.=$r.$g.$b;
}
}
return $output;
}
?><img src="test.gif">
polone at townnews dot com
04-Apr-2002 03:40
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:
<IMG SRC="/-/data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH hhx4dbgYKAAA7" ALT="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.
kremlin at home dot com
27-Feb-2001 01:45
Animated GIFs as well as transparent GIFs qualify as GIF89a's and you should use ImageColorTransparent().
danny at arbitrary dot com
23-Aug-2000 05:31
If you want to have a page with dynamic text and a dynamic image based on one form, use two php's. One is the page which has a call to the other in an img tag:
<img src="myImage.php?name=value...
david at hooshla dot com
29-Apr-2000 10:45
This is how you load and display an image file:
<?
Header("Content-Type: image/gif");
$fn=fopen("./imagefile.gif","r");
fpassthru($fn);
?>
Note that there are no new-lines in the content type header.
till at comets dot de
04-Nov-1999 12:48
ImageGIF() outputs an image created by PHP. Since php cannot create animated or multiple images in a single file
it cannot handle animated GIFs.
To read GIF images use the function ImageCreateFromGIF.
--marcus
imagegif doesn't seem to work on "animated" gifs. .-(
|  |