Apuntes de PHP GD
GD Library, es una librería de código abierto (escrita en C) que permite la creación y manipulación de imágenes en forma dinámica, disponible en algunos lenguajes como Perl, PHP, etc.
A continuación un par de pequeños apuntes y tips útiles (o no xD) sobre el uso de la librería GD en la generación de imágenes dinámicas en el lenguaje PHP.
1.- ¿Como generar una grilla entre dos colores?
<?php
// Generate a grid between two colors with PHP
// by deerme.org
$w = 1280;
$h = 1280;
$img = imagecreatetruecolor( $w , $h);
$c1 = imagecolorallocate( $img , 255 , 0 , 255 );
$c2 = imagecolorallocate( $img , 255 , 255 , 255 );
imagefilledrectangle($img , 0,0,$w,$h,$c2);
for( $i = 0 ; $i <= $w ; $i=$i+8 )
{
for( $j=0;$j<=$h;$j=$j+8 )
{
imagefilledrectangle($img , $i,$j,$i+8,$j+8, ( ( $count%2 == 0 ) ? $c1 : $c2 ) );
$count++;
}
}
imagepng($img , "grilla.png");
?>
2.- ¿Comó realizar un thumb (miniatura) de una imágen?
<?php
// Function by deerme.org
function imagethumb($img,$width_new=100,$savepath = false)
{
if ( !is_file( $img ) )
{
throw new Exception('Image ('.$img.') not found.');
return false;
}
// Max Height
$height_max=1024;
$imginfo = getimagesize($img);
if ( !$imginfo )
{
throw new Exception('The file '.$img.' it is not a valid image.');
return false;
}
switch( $imginfo )
{
case $imginfo[2] == 1 :
$imggd = imagecreatefromgif($img);
break;
case $imginfo[2] == 2 :
$imggd = imagecreatefromjpeg($img);
break;
case $imginfo[2] == 3 :
$imggd = imagecreatefrompng($img);
break;
}
$ratio = ($imginfo[0]/$width_new);
$height_new = (int)($imginfo[1] / $ratio);
if($height_new>$height_max)
{
$width_new = (int) ($height_max*$width_new/$height_new);
$height_new = $height_max;
}
$imggdthumb = imagecreatetruecolor($width_new,$height_new);
imagecopyresampled($imggdthumb, $imggd, 0, 0, 0, 0, $width_new, $height_new, $imginfo[0], $imginfo[1]);
if ( $savepath )
{
if ( is_writable( dirname($savepath )) )
{
imagejpeg($imggdthumb,$savepath,100);
imagedestroy($imggdthumb);
return $savepath;
}
else
{
throw new Exception('The path ('.$savepath.') is not writable.');
return false;
}
}
else
{
header("Content-type: image/jpeg");
imagejpeg($imggdthumb,false,100);
imagedestroy($imggdthumb);
return true;
}
}
imagethumb("d:/tmp/cbitmap.png",128,"d:/tmp/cbitmap_thumb.jpg");
Pronto mas material


Articulos
Tips
This site uses valid HTML and CSS.
If you like what we do, please don't hestitate and subscribe to our