PHP Doku:: Erzeugt ein neues Bild im GIF-Format, welches aus einer Datei oder von einer URL gelesen wird - function.imagecreatefromgif.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzBildverarbeitung und -generierungBildbearbeitung und GDGD- und Image-Funktionenimagecreatefromgif

Ein Service von Reinhard Neidl - Webprogrammierung.

GD- und Image-Funktionen

<<imagecreatefromgd

imagecreatefromjpeg>>

imagecreatefromgif

(PHP 4, PHP 5)

imagecreatefromgif Erzeugt ein neues Bild im GIF-Format, welches aus einer Datei oder von einer URL gelesen wird

Beschreibung:

resource imagecreatefromgif ( string $filename )

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

Im Fehlerfall gibt ImageCreateFromGIF() eine leere Zeichenkette zurück. Zudem wird eine Fehlermeldung erzeugt, die im Browser als Verbindungsabbruch dargestellt wird. Zum besseren Debuggen wird das folgende Beispiel einen GIF-Fehler erzeugen:

Beispiel #1 Beispiel, um die Handhabung eines Fehlers bei der Bilderzeugung zu sehen (Dank an vic@zymsys.com):

function LoadGif ($imgname) {
    $im = @ImageCreateFromGIF ($imgname); /* Versuch, Datei zu öffnen */
    if (!$im) {                           /* Prüfen, ob fehlgeschlagen */
        $im = ImageCreate (150, 30);      /* Erzeugen eines leeren Bildes */
        $bgc = ImageColorAllocate ($im, 255, 255, 255);
        $tc  = ImageColorAllocate ($im, 0, 0, 0);
        ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc); 
        /* Ausgabe einer Fehlermeldung */
        ImageString($im, 1, 5, 5, "Fehler beim Öffnen von: $imgname", $tc); 
    }
    return $im;
}

Hinweis:

ACHTUNG: Ab Version 1.6 wurde die komplette GIF-Unterstützung aus der GD-Bibliothek entfernt. Diese Funktion ist also nur verfügbar, wenn Sie eine Version vor 1.6 verwenden.


17 BenutzerBeiträge:
- Beiträge aktualisieren...
frank at huddler dot com
6.01.2009 0:11
I wrote two alternate versions of ZeBadger's is_ani() function, for determining if a gif file is animated

Original:
http://us.php.net/manual/en/function.imagecreatefromgif.php#59787

The first alternative version is just as memory intensive as the original, and more CPU intensive, but far simpler:

<?php
function is_ani($filename) {
    return (bool)
preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', file_get_contents($filename));
}
?>

The second alternative is about as CPU intensive as the original function, but uses less memory (and may also result in less disk activity)

<?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)
   
    // 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#s', $chunk, $matches);
   
   
fclose($fh);
    return
$count > 1;
}
?>
max lloyd
1.05.2008 18:08
Someone wanted to display only the first frame of an animated .gif
I thought maybe if the animated .gif was displayed
as a .png, it would only display the first frame.
This worked very good:

The HTML used to display image:
<img src="giftopng.php?image=mypic.gif" alt="" />

The test script called:  "giftopng.php"
<?php

//Get image name from URL
$image=$_GET['image'];

//Load the image
$img = ImageCreateFromGif($image);

//Convert to PNG and send it to browser
if($img) {
header("Content-Type: image/png");
ImagePNG($img);

//Clean-up memory
ImageDestroy($img);
}
?>
moxley at moxleydata dot com
10.10.2006 2:40
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;
}
?>
razonklnbd at yahoo dot com
18.12.2005 8:35
this function will convert gif image to jpeg. it take three parameter as argument. two argument are optional
first will take the gif file name. second for file name to save the converted file. third argument is an color array

function gif2jpeg($p_fl, $p_new_fl='', $bgcolor=false){
  list($wd, $ht, $tp, $at)=getimagesize($p_fl);
  $img_src=imagecreatefromgif($p_fl);
  $img_dst=imagecreatetruecolor($wd,$ht);
  $clr['red']=255;
  $clr['green']=255;
  $clr['blue']=255;
  if(is_array($bgcolor)) $clr=$bgcolor;
  $kek=imagecolorallocate($img_dst,
                  $clr['red'],$clr['green'],$clr['blue']);
  imagefill($img_dst,0,0,$kek);
  imagecopyresampled($img_dst, $img_src, 0, 0,
                  0, 0, $wd, $ht, $wd, $ht);
  $draw=true;
  if(strlen($p_new_fl)>0){
    if($hnd=fopen($p_new_fl,'w')){
      $draw=false;
      fclose($hnd);
    }
  }
  if(true==$draw){
    header("Content-type: image/jpeg");
    imagejpeg($img_dst);
  }else imagejpeg($img_dst, $p_new_fl);
  imagedestroy($img_dst);
  imagedestroy($img_src);
}

EXAMPLE:
$fl='http://www.suchona.com/test/testgif.gif';
$c['red']=255;
$c['green']=0;
$c['blue']=0;
gif2jpeg($fl, '', $c);

Thanks
Md. Shahadat Hossain Khan Razon
razon.phpxperts.com
ZeBadger
15.12.2005 18:15
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.
steve at stevedix dot de
25.10.2005 9:20
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
jason at null dot zzz
6.04.2004 11:13
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
josh [ a t ] OnlineComics [ d o t ] net
8.10.2003 5:36
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.

29.06.2003 13:26
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;
}

18.04.2003 18:34
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)
geoffrey at poulet dot org
27.02.2003 16:22
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
fezber at yamasoft dot com
13.02.2003 16:27
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
Ady at freebsd dot ady dot ro
11.01.2003 20:20
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!
unknown at hotmail dot com
15.11.2002 3:00
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
senbei at terra dot es
9.05.2002 19:31
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.
chuckg at infinite9 dot com
22.03.2001 9:05
I just thought I'd let you guys know of a workaround for the lack of Gif support in the latest GD...aside from adding the patch.  It works just as good, from what I can tell, and is quite nice ;)

Here goes:
<?
//Just grab some general image info
$img = "path/to/image.gif";
$imageInfo = GetImagesize($img);

if (
$imageInfo[2] == 1 ) {
  
//it's a gif
  
$file = fread(fopen($path, "r"), filesize($path));

  
$fileHeader = "image/gif";
  
Header("Content-Type: ".$fileHeader);
   echo
$file;
  
fclose($file);
}
?>

basically what that does is reads the file..through the "GetImageSize", which still returns if it's a GIF or not, (thank god), then if it's a gif, opens the file through normal filestream, sets the header to image type "gif" then prints it out.  Same as doing "ImageCreateFromGif($img)" and "ImageGif($img)", except that it doesn't require GD Gif support.  If you have any questions just ask me via email (chuckg@infinite9.com)
anthony dot atkins at vt dot edu
17.05.2000 3:05
<?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);
}

?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)