imagegif

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

imagegif β€” Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€ ΠΈΠ»ΠΈ записываСт Π² Ρ„Π°ΠΉΠ»

ОписаниС

function imagegif(GdImage $image, resource|string|null $file = null): bool

Ѐункция imagegif() создаёт ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ GIF Π² Ρ„Π°ΠΉΠ»Π΅ ΠΈΠ»ΠΈ рСсурсС ΠΏΠΎΡ‚ΠΎΠΊΠ°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΡƒΠΊΠ°Π·Π°Π»ΠΈ Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π΅ file. Ѐункция Π±Π΅Ρ€Ρ‘Ρ‚ Π·Π° основу ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ image. АргумСнт image β€” ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ функция imagecreate() ΠΈΠ»ΠΈ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ сСмСйства imagecreatefrom*.

Ѐункция создаст Ρ„Π°ΠΉΠ» Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ GIF87a, Ссли Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π΅ сдСлали ΠΏΡ€ΠΎΠ·Ρ€Π°Ρ‡Π½Ρ‹ΠΌ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagecolortransparent(), Ρ‚ΠΎΠ³Π΄Π° функция создаст ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ GIF89a.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.
file

ΠŸΡƒΡ‚ΡŒ ΠΈΠ»ΠΈ рСсурс ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚ΠΎΠ³ΠΎ ΠΏΠΎΡ‚ΠΎΠΊΠ°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ функция автоматичСски Π·Π°ΠΊΡ€Ρ‹Π²Π°Π΅Ρ‚ послС Π²ΠΎΠ·Π²Ρ€Π°Ρ‚Π° значСния, для сохранСния Ρ„Π°ΠΉΠ»Π°. Ѐункция Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚ ΠΏΠΎΡ‚ΠΎΠΊ Π½Π΅ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Π°Π½Π½ΠΎΠ³ΠΎ изобраТСния Π½Π°ΠΏΡ€ΡΠΌΡƒΡŽ, Ссли ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ Π½Π΅ установили ΠΈΠ»ΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Π»ΠΈ Π² Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

ΠŸΡ€Π΅Π΄ΠΎΡΡ‚Π΅Ρ€Π΅ΠΆΠ΅Π½ΠΈΠ΅

Ѐункция Π²Π΅Ρ€Π½Ρ‘Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true, Ссли вывСсти ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ срСдствами Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ libgd Π½Π΅Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π» ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ gd-рСсурс (resource).

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ Π²Ρ‹Π²ΠΎΠ΄Π° изобраТСния Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagegif()

<?php

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ изобраТСния
$im = imagecreatetruecolor(100, 100);

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π±Π΅Π»ΠΎΠ³ΠΎ Ρ„ΠΎΠ½Π°
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);

// ΠžΡ‚Ρ€ΠΈΡΠΎΠ²ΠΊΠ° тСкста Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΈ
imagestring($im, 3, 40, 20, 'GD Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ°', 0xFFBA00);

// Π’Ρ‹Π²ΠΎΠ΄ изобраТСния Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€
header('Content-Type: image/gif');

imagegif($im);

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ прСобразования PNG-изобраТСния Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ GIF Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagegif()

<?php

// Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° PNG-Ρ„Π°ΠΉΠ»Π°
$png = imagecreatefrompng('./php.png');

// Π‘ΠΎΡ…Ρ€Π°Π½Π΅Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π° Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ GIF
imagegif($png, './php.gif');

// Π“ΠΎΡ‚ΠΎΠ²ΠΎ
echo 'ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Π°Π½ΠΈΠ΅ PNG-Ρ„Π°ΠΉΠ»Π° Π² GIF-Ρ„Π°ΠΉΠ» Π·Π°Π²Π΅Ρ€ΡˆΠΈΠ»ΠΎΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ!';

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π‘Π»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚ ΠΊΠΎΠ΄Π° ΠΏΠΎΠΌΠΎΠ³Π°Π΅Ρ‚ ΠΏΠΈΡΠ°Ρ‚ΡŒ пСрСносимыС PHP-прилоТСния ΠΈ автоматичСски опрСдСляСт доступный Ρ‚ΠΈΠΏ GD-изобраТСния. Π—Π°ΠΌΠ΅Π½ΠΈΡ‚Π΅ строки header ("Content-Type: image/gif"); imagegif ($im); Π½Π° Π±ΠΎΠ»Π΅Π΅ Π³ΠΈΠ±ΠΊΡƒΡŽ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ:

<?php

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π½ΠΎΠ²ΠΎΠ³ΠΎ изобраТСния
$im = imagecreatetruecolor(100, 100);

// КакиС-Π»ΠΈΠ±ΠΎ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ с ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ

// ΠžΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° Π²Ρ‹Π²ΠΎΠ΄Π°
if (function_exists('imagegif')) {
// Для GIF-Ρ„Π°ΠΉΠ»ΠΎΠ²
header('Content-Type: image/gif');

imagegif($im);
} elseif (
function_exists('imagejpeg')) {
// Для JPEG-Ρ„Π°ΠΉΠ»ΠΎΠ²
header('Content-Type: image/jpeg');

imagejpeg($im, NULL, 100);
} elseif (
function_exists('imagepng')) {
// Для PNG-Ρ„Π°ΠΉΠ»ΠΎΠ²
header('Content-Type: image/png');

imagepng($im);
} elseif (
function_exists('imagewbmp')) {
// Для WBMP-Ρ„Π°ΠΉΠ»ΠΎΠ²
header('Content-Type: image/vnd.wap.wbmp');

imagewbmp($im);
} else {
die(
'На этом PHP-сСрвСрС Π½Π΅Ρ‚ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ');
}

?>

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΡƒ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π° ΠΏΠΎΠΌΠΎΠ³Π°Π΅Ρ‚ функция imagetypes():

<?php

if (imagetypes() & IMG_GIF) {
header('Content-Type: image/gif');
imagegif($im);
} elseif (
imagetypes() & IMG_JPG) {
/* ... ΠΈ Ρ‚. Π΄. */
}

?>

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagepng() - Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ PNG-ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€ ΠΈΠ»ΠΈ Ρ„Π°ΠΉΠ»
  • imagewbmp() - Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€ ΠΈΠ»ΠΈ записываСт Π² Ρ„Π°ΠΉΠ»
  • imagejpeg() - Π’Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€ ΠΈΠ»ΠΈ записываСт Π² Ρ„Π°ΠΉΠ»
  • imagetypes() - Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ список Ρ‚ΠΈΠΏΠΎΠ² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ, ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅ΠΌΡ‹Ρ… PHP сборкой
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 12 notes

up
4
polone at townnews dot com ΒΆ
24 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:

<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.
up
2
mail at ignore dot area dot nospam dot joshho dot com ΒΆ
14 years ago
apparently GD does not support animated GIFs.

instead, we're stuck with the old fashioned way:
<?php
header('Content-Type: image/gif');
echo file_get_contents($destPathImage);
?>
up
0
stefan at colulus dot com ΒΆ
18 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:

<?php
//ImageCall.php -- This script will call a script to produce the image.
for($next = 1;$next < 4; $next++){
print "Image $next:<br>";
print "<img src = 'Image.php?\$text=$next'>";
print "<br><br>";
}
?>

<?php
//Image.php -- This script creates a square image and places the text on it.

// image size and color
$im = ImageCreate(77,77);
$color1 = ImageColorAllocate($im,0x66,0xCC,0x00);
$color2 = ImageColorAllocate($im,0x33,0x66,0x00);
$color3 = ImageColorAllocate($im,0x00,0x99,0x00);
$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',$_GET['$text']);
$X = (77 - (abs($size[2]- $size[0])))/2;
$Y = ((77 - (abs($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);

?>
up
0
rokfaith at gmail dot com ΒΆ
19 years ago
to create an animated gif with gifsicle, but without storing temporary images on disk:

<?php
$cmd = 'gifsicle --loop -O1 --multifile --delay 25 - > '.$outfile;
$desc = array(0 => array("pipe", "r"),1 => array("pipe", "w"),2 => array("pipe", "w"));
$proc = proc_open($cmd, $desc, $pipes);
if (!is_resource($proc)) {
  die('Unable to start gifsicle');
}
for ($frame=0; $frame<$total_frames; $frame++) {
  $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.
up
-2
jemore at nospam dot m6net dot fr ΒΆ
22 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.
up
-2
coldume ΒΆ
12 years ago
To keep GIF animation, you can try the class writing based on GD 
GD Enhancer http://www.gdenhancer.com/
up
-1
Lauri Harpf ΒΆ
20 years ago
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.
up
-1
kremlin at home dot com ΒΆ
25 years ago
Animated GIFs as well as transparent GIFs qualify as GIF89a's and you should use ImageColorTransparent().
up
-2
grant k. ΒΆ
15 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:

<?php
//Only saves the file to a destination, no display

$image_value = imagegif($image, $save_file_to_path);

//Saves file and attempts to display it, but will throw an error message

imagegif($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.
?>
up
-4
Leigh Purdie ΒΆ
20 years ago
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>
up
-5
david at hooshla dot com ΒΆ
26 years ago
This is how you load and display an image file:

<?php
Header("Content-Type: image/gif");
$fn=fopen("./imagefile.gif","r");
fpassthru($fn);
?>

Note that there are no new-lines in the content type header.
up
-5
alan hogan dot com slash contact ΒΆ
18 years ago
Note that you *can* save with a transparent color **and dither** using GD2.
For a useful example, see the png-to-gif function in my coment here:
http://www.php.net/manual/en/function.imagecolorat.php