PHP 8.3.4 Released!

imagegd2

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

imagegd2Imprime una imagen GD2 a un navegador o fichero

Descripción

imagegd2(
    resource $image,
    mixed $to = NULL,
    int $chunk_size = 128,
    int $type = IMG_GD2_RAW
): bool

Genera una imagen GD2 en el parámetro to.

Parámetros

image

Un recurso image, es devuelto por una de las funciones de creación de imágenes, como imagecreatetruecolor().

to

La ruta o un recurso de flujo de apertura (el cual se cierra automáticamente después de que devuelva esta función) donde guardar el fichero. Si no se establece, o su valor es null, se mostrará directamente en la salida el flujo de imagen sin tratar.

chunk_size

Tamaño del trozo.

type

Puede ser IMG_GD2_RAW o IMG_GD2_COMPRESSED. Por defecto es IMG_GD2_RAW.

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.

Historial de cambios

Versión Descripción
5.4.0 Se añadió soporte para pasar un resource de flujo a to.

Ejemplos

Ejemplo #1 Imprimir una imagen GD2

<?php
// Crear una imagen en blanco y añadir algún texto
$im = imagecreatetruecolor(120, 20);
$color_texto = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Una Sencilla Cadena De Texto", $color_texto);

// Imprimir la imagen
imagegd2($im);

// Liberar memoria
imagedestroy($im);
?>

Ejemplo #2 Guardar una imagen GD2

<?php
// Crear una imagen en blanco y añadir algún texto
$im = imagecreatetruecolor(120, 20);
$color_texto = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Una Sencilla Cadena De Texto", $color_texto);

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

// Liberar memoria
imagedestroy($im);
?>

Notas

Nota:

El formato GD2 se usa comúnmente para permitir una carga rápida de partes de imágenes. Observe que el formato GD2 sólo es utilizable en aplicaciones compatibles con GD2.

Ver también

  • imagegd() - Imprime una imagen GD2 a un navegador o archivo

add a note

User Contributed Notes 2 notes

up
2
Nick
12 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
mark at teckis dot com
20 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