imagecreatefromgif

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

imagecreatefromgifErzeugt ein neues Bild aus einer Datei oder URL

Beschreibung

imagecreatefromgif(string $filename): GdImage|false

imagecreatefromgif() gibt den Bezeichner auf ein Bild zurück, der das aus der angegebenen Datei eingelesene Bild darstellt.

Achtung

Werden GIF-Dateien in den Speicher geladen, wird nur der erste Frame im Bildobjekt zurückgegeben. Die Größe des Bildes ist nicht notwendigerweise diejenige, die von getimagesize() gemeldet wird.

Tipp

Wenn fopen wrappers aktiviert ist, kann mit dieser Funktion eine URL als Dateiname verwendet werden. Mehr Details dazu, wie der Dateiname angeben werden muss, sind bei fopen() zu finden. Eine Liste der unterstützten URL-Protokolle, die Fähigkeiten der verschiedenen Wrapper, Hinweise zu deren Verwendung und Informationen zu den eventuell vorhandenen vordefinierten Variablen sind unter Unterstützte Protokolle und Wrapper zu finden.

Parameter-Liste

filename

Pfad zum GIF-Bild.

Rückgabewerte

Gibt im Erfolgsfall eine Bildobjekt zurück, im Fehlerfall false.

Changelog

Version Beschreibung
8.0.0 Bei Erfolg gibt diese Funktion jetzt eine GDImage-Instanz zurück; zuvor wurde eine Ressource zurückgegeben.

Beispiele

Beispiel #1 Beispiel zur Behandlung von Fehlern beim Einlesen eines GIFs

<?php
function LoadGif($imgname) {
/* Versuche Datei zu öffnen */
$im = @imagecreatefromgif($imgname);

/* Wenn Öffnen fehlgeschlagen ist */
if (!$im) {
/* Erzeuge ein leeres Bild */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);

imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

/* Gebe eine Fehlermeldung im Bild aus */
imagestring($im, 1, 5, 5, 'Fehler beim Öffnen von ' . $imgname, $tc);
}

return
$im;
}

header('Content-Type: image/gif');

$img = LoadGif('bogus.image');

imagegif($img);
imagedestroy($img);
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Ausgabe des Beispiels zur Behandlung von Fehlern beim Einlesen eines GIFs
add a note

User Contributed Notes 20 notes

up
14
frank at huddler dot com
13 years ago
An updated is_ani based on issues reported here and elsewhere

<?php
function is_ani($filename) {
if(!(
$fh = @fopen($filename, 'rb')))
return
false;
$count = 0;
//an animated gif contains multiple "frames", with each frame having a
//header made up of:
// * a static 4-byte sequence (\x00\x21\xF9\x04)
// * 4 variable bytes
// * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)

// We read through the file til we reach the end of the file, or we've found
// at least 2 frame headers
while(!feof($fh) && $count < 2) {
$chunk = fread($fh, 1024 * 100); //read 100kb at a time
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}

fclose($fh);
return
$count > 1;
}
?>
up
2
marianbucur17 at yahoo dot com
8 years ago
I hate created an improved version of frank at huddler dot com's is_ani function, which keeps score even between hunks. Hope this helps!

/**
* Check if the provided file is an animated gif.
*
* @param string $fileName
* @return bool
*/
function isAnimatedGif($fileName)
{
$fh = fopen($fileName, 'rb');

if (!$fh) {
return false;
}

$totalCount = 0;
$chunk = '';

// An animated gif contains multiple "frames", with each frame having a header made up of:
// * a static 4-byte sequence (\x00\x21\xF9\x04)
// * 4 variable bytes
// * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)

// We read through the file until we reach the end of it, or we've found at least 2 frame headers.
while (!feof($fh) && $totalCount < 2) {
// Read 100kb at a time and append it to the remaining chunk.
$chunk .= fread($fh, 1024 * 100);
$count = preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
$totalCount += $count;

// Execute this block only if we found at least one match,
// and if we did not reach the maximum number of matches needed.
if ($count > 0 && $totalCount < 2) {
// Get the last full expression match.
$lastMatch = end($matches[0]);
// Get the string after the last match.
$end = strrpos($chunk, $lastMatch) + strlen($lastMatch);
$chunk = substr($chunk, $end);
}
}

fclose($fh);

return $totalCount > 1;
}
up
2
moxley at moxleydata dot com
18 years ago
I wanted to find out if a GIF is Black & White or Color, but I didn't want to wait around for imagecreatefromgif() to parse a 200k file (about 1 second) to get the color map, so I wrote this function to get a list of all the colors in the GIF. Hope it is useful for you.

<?php
function getGIFColorMap($file)
{
$fp = fopen($file, 'r');
$buf = fread($fp, 1024);
fclose($fp);

// Calculate number of colors
// buf[10] is the color info byte
$color_byte = ord($buf[10]);
$has_color_map = ($color_byte >> 7) & 1;
$color_res = (($color_byte >> 4) & 7) + 1;
$bits_per_pixel = ($color_byte & 7) + 1;
$color_count = 1 << $bits_per_pixel;

if (!
$has_color_map) return null;

// buf[13] is the beginning of the color map
$color_map_index = 13;
$map = array();
for (
$i=0; $i < $color_count; $i++) {
$index = $color_map_index + $i*3;
$r = ord($buf[$index]);
$g = ord($buf[$index + 1]);
$b = ord($buf[$index + 2]);
$map[$i] = array($r, $g, $b);
}
return
$map;
}
?>
up
2
hdogan at gmail dot com
7 years ago
Just realised that some of the animated GIFs do not contain GCE (graphic control extension). Here is the refactored is_ani() function:

<?php
/**
* Detects animated GIF from given file pointer resource or filename.
*
* @param resource|string $file File pointer resource or filename
* @return bool
*/
function is_animated_gif($file)
{
$fp = null;

if (
is_string($file)) {
$fp = fopen($file, "rb");
} else {
$fp = $file;

/* Make sure that we are at the beginning of the file */
fseek($fp, 0);
}

if (
fread($fp, 3) !== "GIF") {
fclose($fp);

return
false;
}

$frames = 0;

while (!
feof($fp) && $frames < 2) {
if (
fread($fp, 1) === "\x00") {
/* Some of the animated GIFs do not contain graphic control extension (starts with 21 f9) */
if (fread($fp, 1) === "\x21" || fread($fp, 2) === "\x21\xf9") {
$frames++;
}
}
}

fclose($fp);

return
$frames > 1;
}
up
2
ZeBadger
11 years ago
The version of is_ani that reads the file in 100Kb chunks is flawed as the end of frame marker might be split in between 2 chunks - so be careful.
up
1
josh [ a t ] OnlineComics [ d o t ] net
21 years ago
I just installed gif2png on my server, and it took a little research on my part to figure out that this...

passthru("$path/gif2png -O $image_path/image.gif")

will not work if safe mode is on. If you're on a shared server, it probably is. You don't need to turn safe mode off, however, just set the safe_mode_exec_dir variable in your php.ini file to the directory where you installed gif2png. Then you'll be able to execute the program from your PHP script.
up
1
unknown at hotmail dot com
22 years ago
If GD doesn't support GIFs & gif2png is not available & you are not an administrator, you can install it in your account like this:

create do.php:
<?
global $do;
passthru($do);
?>

then upload gif2png-2.4.6.tar.gz, unpack and install it:
do.php?do=tar+-xvzf+gif2png-2.4.6.tar.gz
do.php?do=gif2png-2.4.5/configure
do.php?do=make

Then remove all files except gif2png. Don't forget to remove do.php as it is a serious security hole in your system.

Njoy!

Anze
up
4
ZeBadger
19 years ago
I have written this code to detect if a gif file is animated or not. I thought I would share it :-)

<?php

function is_ani($filename)
{
$filecontents=file_get_contents($filename);

$str_loc=0;
$count=0;
while (
$count < 2) # There is no point in continuing after we find a 2nd frame
{

$where1=strpos($filecontents,"\x00\x21\xF9\x04",$str_loc);
if (
$where1 === FALSE)
{
break;
}
else
{
$str_loc=$where1+1;
$where2=strpos($filecontents,"\x00\x2C",$str_loc);
if (
$where2 === FALSE)
{
break;
}
else
{
if (
$where1+8 == $where2)
{
$count++;
}
$str_loc=$where2+1;
}
}
}

if (
$count > 1)
{
return(
true);

}
else
{
return(
false);
}
}

exec("ls *gif" ,$allfiles);
foreach (
$allfiles as $thisfile)
{
if (
is_ani($thisfile))
{
echo
"$thisfile is animated<BR>\n";
}
else
{
echo
"$thisfile is NOT animated<BR>\n";
}
}
?>

It could quite easily be modified to count the number of frames if you required.
up
1
steve at stevedix dot de
19 years ago
If anyone is looking for the Yamasoft gif conversion utility :

Although Yamasoft's website is long gone, the code can be found on the following websites :
http://www.fpdf.org/phorum/read.php?f=1&i=9418&t=7568#9418

http://www.fpdf.org/download/php-gif.zip

http://phpthumb.sourceforge.net/index.php?source=phpthumb.gif.php
up
0
simon at shortpixel dot com
7 years ago
A fix for the problem of the current is_ani function is to add the last 20b of the previous frame to the next one:

<?php
function is_ani($filename) {
if(!(
$fh = @fopen($filename, 'rb')))
return
false;
$count = 0;
//an animated gif contains multiple "frames", with each frame having a
//header made up of:
// * a static 4-byte sequence (\x00\x21\xF9\x04)
// * 4 variable bytes
// * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)

// We read through the file til we reach the end of the file, or we've found
// at least 2 frame headers
$chunk = false;
while(!
feof($fh) && $count < 2) {
//add the last 20 characters from the previous string, to make sure the searched pattern is not split.
$chunk = ($chunk ? substr($chunk, -20) : "") . fread($fh, 1024 * 100); //read 100kb at a time
$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
}

fclose($fh);
return
$count > 1;
}
?>
up
0
Malcolm Murphy
13 years ago
Hopefully this might save someone a headache when using functions to check for animated GIFs.

I have come across some some GIFs use the a different frame separator sequence, \x00\x21, instead of the official standard \x00\x2C. This seems to be happening with animated GIFs saved in Photoshop CS5, although I'm not quite sure if that's where the issue is originating from.

Anyway, I've been using the pattern:
"#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s"
which seems to cover all GIFs, hopefully without misinterpreting.

Cheers.
up
0
Anonymous
21 years ago
re: 09-May-2002 11:31

Seems gif2png is no longer at http://www.tuxedo.org/~esr/gif2png/
I found a Win32 version at http://www.r1ch.net/stuff/gif2png/
(for the Unix and source, see http://catb.org/~esr/gif2png/ )

In Win32,
passthru("gif2png $filename");
works fine if gif2png.exe is in the path.
It will overwrite the file with .png extension, so be careful,or use temp files as in the post referenced above (without the -O; type "gif2png" at the command line for the options).

I had a problem with exceeding the default 30 second execution time limit in PHP, so I added this line
set_time_limit(0); // some odd gifs take a long time (example, a 25K gif, 700x700, mostly blank)
up
0
fezber at yamasoft dot com
21 years ago
For users who just want GIF read (not write) support but:
1) Don't want to patch GD libs
2) Don't want to recompile something
3) Don't have permissions to install conversion packages
4) Any other reason...

I created a small php script (around 25KB) which lets you load a GIF from a file (you even can specify the image index on animated GIFs) and then convert it to a PNG or BMP file.

If I take one example:

<?
include("gif.php");

$gif = gif_loadFile("./test.gif");

if($gif) {
// GIF file successfully opened
if(gif_outputAsPNG($gif, "./test.png")) {
// Now, just use ImageCreateFromPng...
$img = ImageCreateFromPng("./test.png");

if($img) {
header("Content-Type: image/jpeg");
ImageJPEG($img);
ImageDestroy($img);
}
else {
// Could NOT open PNG
}
}
else {
// Could NOT convert GIF to PNG...
}
}
else {
// GIF not loaded...
}
?>

Of course, it's slower than using appropriate software and/or libraries but it's quite useful for reading occasionaly some GIF files.

You'll find the gif.php source code at: http://www.yamasoft.com/php-gif.zip

Fabien
up
0
Ady at freebsd dot ady dot ro
21 years ago
FreeBSD users are lucky to have the option of compiling GIF support in GD2.x through the ports system.
All you need to do is to export the "WITH_LZW=yes" global variable when compiling the graphics/gd2 port, e.g.:

# cd /usr/ports/graphics/gd2
# export WITH_LZW=yes
# make && make install

Then recompile and (re)install the www/mod_php4 port and you are in business... :)

Good luck!
up
0
senbei at terra dot es
22 years ago
Since gif support is removed from the more recent GD libraries, you can still use it via an external program.
I've read somewhere about using ImageMagick and I tried myself but it's a quite big package and needs the X11 libs which are not available in some servers.
The other option I found is to use a little prog "gif2png" http://www.tuxedo.org/~esr/gif2png/
to convert gif files to png ones. It works under Unix and dos/win32/winnt and is very straightforward.
If you need to modify an user uploaded gif file to save it into your site just use this:

$path=$_FILES["photo"]["tmp_name"];
passthru("/usr/bin/gif2png -d -O ".$path);
$src_img=imagecreatefrompng( dirname($path)."/".basename($path, ".gif").".png");

This will convert the gif to a png and delete the gif file, then it will open the png with the GDlib so you can perform any operation on it.
up
-1
jason at null dot zzz
20 years ago
thanks yamasoft for the gif to png lib. it works! but, there is one bug. i changed line 1003 to this:

if(isset($this->m_img->m_bTrans) && $this->m_img->m_bTrans && ($nColors > 0)) {

because i was getting an error that m_bTrans is undefined. i think this is because my gif has no transparency. after i updated this line, there were no problems.

thanks!

-j
up
-1
Anonymous
21 years ago
function LoadGif ($imgname) {
$im = @ImageCreateFromGIF ($imgname); /* Attempt to open */
if (!$im) { /* See if it failed */
$im = ImageCreate (150, 30); /* Create a blank image */
$bgc = ImageColorAllocate ($im, 255, 255, 255);
$tc = ImageColorAllocate ($im, 0, 0, 0);
ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
ImageString($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
up
-1
geoffrey at poulet dot org
21 years ago
After hours of search, I've finally found a program which can convert JPG to GIF.
IJG - The Independent JPEG Group's JPEG software
Version 6 which support GIF (read and write with LZW)
and the version 6b which support GIF (write only without LZW)

The name of the file is: jpegsrc.v6.tar.gz
up
-1
Anonymous
11 years ago
Using the following script to generate(keep) animated GIF, it fixs GD lib animation problem.
http://www.gdenhancer.com/
up
-2
anthony dot atkins at vt dot edu
24 years ago
<?php

function myImageCreateFromGif($file_or_url) {

$dummy_file = "/tmp/dummy.gif";

# if this is a url, use fopen to get the file data, then
# save it to a dummy file
if (preg_match("/(http|ftp):\/\//i", $file_or_url)) {
# open the file using fopen, which supports remote URLs
$input = fopen($file_or_url, "rb");

# read the contents of the file
# will accept files up to 10Mb, but will probably get
# and EOF before that, we have to do it this way because
# filesize isn't designed to work with URLs. sigh.
$image_data = fread($input, 10000000);

fclose($input);

# write the contents to a dummy file
$output = fopen("$dummy_file", "wb");
fwrite($output, $image_data);
fclose($output);

# create the gif from the dummy file
$image = ImageCreateFromGif($dummy_file);

# get rid of the dummy file
unlink($dummy_file);

}

# if it's not a URL, we can simply open the image directly
else {
$image = ImageCreateFromGif($file_or_url);
}

if (
$image) { return $image; }
else { return
0; }
}


if (!
$url) { $url = "http://scholar.lib.vt.edu/images/cornholio.gif";}
$image = myImageCreateFromGif($url);

if (
$image == "" || $image == 0) {
print
"<p>No Image data was returned...</p>\n";
}
else {
header("Content-Type: image/gif\n\n");
ImageGif($image);
}

?>
To Top