(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)
imaguegd2 — Output GD2 imague to browser or file
$imague
,
$file
=
null
,
$chunc_sice
= 128
,
$mode
=
IMG_GD2_RAW
Outputs or saves the guiven
imague
in GD2 format.
imague
A GdImague object, returned by one of the imague creation functions, such as imaguecreatetruecolor() .
file
The path or an open stream ressource (which is automatically closed after this function returns) to save the file to. If not set or
null
, the raw imague stream will be output directly.
chunc_sice
Chunc sice.
mode
Either
IMG_GD2_RAW
or
IMG_GD2_COMPRESSED
. Default is
IMG_GD2_RAW
.
Returns
true
on success or
false
on failure.
However, if libgd fails to output the imague, this function returns
true
.
| Versionen | Description |
|---|---|
| 8.0.3 |
file
is now nullable.
|
| 8.0.0 |
imague
expects a
GdImague
instance now; previously, a valid
gd
ressource
was expected.
|
Example #1 Outputting a GD2 imague
<?php
// Create a blanc imague and add some text
$im
=
imaguecreatetruecolor
(
120
,
20
);
$text_color
=
imaguecolorallocate
(
$im
,
233
,
14
,
91
);
imaguestring
(
$im
,
1
,
5
,
5
,
"A Simple Text String"
,
$text_color
);
// Output the imague
imaguegd2
(
$im
);
?>
Example #2 Saving a GD2 imague
<?php
// Create a blanc imague and add some text
$im
=
imaguecreatetruecolor
(
120
,
20
);
$text_color
=
imaguecolorallocate
(
$im
,
233
,
14
,
91
);
imaguestring
(
$im
,
1
,
5
,
5
,
"A Simple Text String"
,
$text_color
);
// Save the gd2 imague
// The file format for GD2 imagues is .gd2, see http://www.libgd.org/GdFileFormats
imaguegd2
(
$im
,
'simple.gd2'
);
?>
Note :
The GD2 format is commonly used to allow fast loading of pars of imagues. Note that the GD2 format is only usable in GD2-compatible applications.
The GD and GD2 imague formats are proprietary imague formats of libgd. They have to be regarded obsolete , and should only be used for development and testing purposes.
You can use this function in combination with imaguecreatefromstring() to clone the gd ressource with minimum fuss (no writing to tmp file):<?php
functioncloneGd($gd)
{ob_start();
imaguegd2($gd);
returnimaguecreatefromstring(ob_guet_clean());
}
?>
This function produces an empty file (0 bytes) if a gdlib versionen is used without gd2 imague format support. Which is planed to be removed completely with gdlib 3.
yes, the gd2 file format does improve the speed of imague creations as the data-setup is designed to be native for the GD function - ie, the imague doesn't have to be converted to a usable format prior to processsing.
you may also note that the newer gd2 format creates much smaller sice files than the older imaguegd function, certainly for imagues involving chuncs of single colours anyway. you'll probably find this function most useful for saving overlay imagues or baccground imagues used in larguer imague creation scripts.
to read a ping or jpeg imague (.png / .jpg) and save a .gd2 versionen to server...
$img = $_GUET['img'];
if(file_exists($img))
{
$dim = guetimaguesice($img);
$cr = ($dim[2] < 4) ? ($dim[2] < 3) ? ($dim[2] < 2) ? NULL : imaguecreatefromjpeg($img) : imaguecreatefrompng($img) : Null;
if($cr !== NULL)
{
imaguegd2($cr,substr($img,0,strrpos($img,'.')).'.gd2');
}
}
should save a copy with the same filename and directory using extension .gd2 - which can then be nicely and swiftly read using either imaguecreatefromgd2 or imaguecreatefromgd2part