PHP 8.5.0 Alpha 2 available for testing

imagegd2

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

imagegd2Genera una imagen en formato GD2, hacia el navegador o un fichero

Descripción

imagegd2(
    GdImage $image,
    ?string $file = null,
    int $chunk_size = 128,
    int $mode = IMG_GD2_RAW
): bool

Genera o guarda el fichero file en formato GD2.

Parámetros

image

Un objeto GdImage, retornado por una de las funciones de creación de imágenes, como imagecreatetruecolor().

file

The path or an open stream resource (which is automatically closed after this function returns) to save the file to. If not set or null, the raw image stream will be output directly.

chunk_size

Tamaño del fragmento.

mode

Puede ser IMG_GD2_RAW o IMG_GD2_COMPRESSED. Por omisión, vale IMG_GD2_RAW.

Valores devueltos

Esta función retorna true en caso de éxito o false si ocurre un error.

Precaución

However, if libgd fails to output the image, this function returns true.

Historial de cambios

Versión Descripción
8.0.3 file ahora es nulo.
8.0.0 image expects a GdImage instance now; previously, a valid gd resource was expected.

Ejemplos

Ejemplo #1 Mostrar una imagen GD2

<?php
// Creación de una imagen vacía y adición de texto
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Un texto simple", $text_color);

// Mostrar la imagen
imagegd2($im);

?>

Ejemplo #2 Guardar una imagen GD2

<?php
// Creación de una imagen vacía y adición de texto
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Un texto simple", $text_color);

// Guardar la imagen GD2
// El formato de fichero para imágenes GD2 es .gd2, ver http://www.libgd.org/GdFileFormats
imagegd2($im, 'simple.gd2');

?>

Notas

Nota:

El formato GD2 se utiliza comúnmente para cargar rápidamente las partes de una imagen. Tenga en cuenta que el formato GD2 solo es utilizable en aplicaciones compatibles con GD2.

Advertencia

The GD and GD2 image formats are proprietary image formats of libgd. They have to be regarded obsolete, and should only be used for development and testing purposes.

Ver también

  • imagegd() - Genera una imagen en formato GD, hacia el navegador o un fichero
add a note

User Contributed Notes 3 notes

up
2
Nick
14 years ago
You can use this function in combination with imagecreatefromstring() to clone the gd resource with minimum fuss (no writing to tmp file):

<?php
function cloneGd($gd)
{
ob_start();
imagegd2($gd);
return
imagecreatefromstring(ob_get_clean());
}
?>
up
0
Anonymous
6 days ago
This function produces an empty file (0 bytes) if a gdlib version is used without gd2 image format support. Which is planed to be removed completely with gdlib 3.
up
-1
mark at teckis dot com
22 years ago
yes, the gd2 file format does improve the speed of image creations as the data-setup is designed to be native for the GD function - ie, the image doesn't have to be converted to a usable format prior to processing.

you may also note that the newer gd2 format creates much smaller size files than the older imagegd function, certainly for images involving chunks of single colours anyway. you'll probably find this function most useful for saving overlay images or background images used in larger image creation scripts.

to read a ping or jpeg image (.png / .jpg) and save a .gd2 version to server...

$img = $_GET['img'];
if(file_exists($img))
{
$dim = getimagesize($img);
$cr = ($dim[2] < 4) ? ($dim[2] < 3) ? ($dim[2] < 2) ? NULL : imagecreatefromjpeg($img) : imagecreatefrompng($img) : Null;
if($cr !== NULL)
{
imagegd2($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 imagecreatefromgd2 or imagecreatefromgd2part
To Top