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());
}
?>
imagegd2
(PHP 4 >= 4.0.7, PHP 5)
imagegd2 — Imprime una imagen GD2 a un navegador o archivo
Descripción
$image
[, string $filename
[, int $chunk_size
[, int $type = IMG_GD2_RAW
]]] )
Imprime una imagen GD2 en filename.
Parámetros
-
image -
Un recurso image, es devuelto por una de las funciones de creación de imágenes, como imagecreatetruecolor().
-
filename -
Ruta en la que guardar el fichero. Si no se establece, o su valor es
NULL, se mostrará directamente en la salida el flujo de imagen. -
chunk_size -
Tamaño del trozo.
-
type -
Puede ser
IMG_GD2_RAWoIMG_GD2_COMPRESSED. Por defecto esIMG_GD2_RAW.
Valores devueltos
Devuelve TRUE en caso de éxito o FALSE en caso de error.
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 archivo para imágenes GD2 es .gd2, véase http://www.libgd.org/GdFileFormats
imagegd2($im, 'simple.gd2');
// Liberar memoria
imagedestroy($im);
?>
Notas
Nota: Esta función require GD 2.0.1 o superior (se recomienda 2.0.28 o superior).
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.
Historial de cambios
| Versión | Descripción |
|---|---|
| 4.3.2 |
Se añadieron chunk_size y type.
|
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
