| FAQ sobre PHP: Una guía básica | ||
|---|---|---|
| Anterior | ||
<?php
|
/* tomamos los parámetros del programa */
/* que son $nombre para el fichero y $maxwidth y $maxheight */
/* para los tamaños máximo (predeterminado 200) */
/* */
/* la llamada se hará como */
/* imag.php?nombre=ruta/imag.jpg&maxwidth=100&maxheight=150 */
$maxwidth=$maxheight=200;
|
$nombre=$_GET['nombre'];
|
if (isset($_GET['maxwidth'])) $maxwidth=$_GET['maxwidth'];
|
if (isset($_GET['maxheight'])) $maxheight=$_GET['maxheight'];
if(empty($nombre))
|
$nombre="images/search.gif";
|
/* determinamos el tipo de fichero */
$tipo=substr($nombre,-3);
|
/* creamos la imagen a partir del fichero */
switch ($tipo)
|
{
|
case "jpg":
|
case "JPG":
$im1=imagecreatefromjpeg($nombre);
|
break;
|
case "gif":
|
case "GIF":
$a=explode("/",$nombre);
|
$a1=$a[count($a)-1];
|
if (!is_file("$_SERVER[DOCUMENT_ROOT]/imagenes/tmp/$a1.jpg"))
|
passthru("/usr/bin/convert $nombre $_SERVER[DOCUMENT_ROOT]/imagenes/tmp/$a1.jpg");
|
$nombre="imagenes/tmp/$a1.jpg";
|
$tipo="jpg";
|
$im1=imagecreatefromjpeg( "$nombre");
|
//unlink($nombre);
|
break;
|
case "png":
|
case "PNG":
$im1=imagecreatefrompng($nombre);
|
break;
|
default:
|
return;
|
}
/* calculamos lo tamaños proporcionales */
$tam = GetImageSize($nombre);
|
$imagewidth = $tam[0];
|
$imageheight = $tam[1];
|
$imagepropv=$imageproph=1;
|
if ($imagewidth > $maxwidth)
|
$imagepropv=$maxwidth/$imagewidth;
|
if ($imageheight > $maxheight)
|
$imageproph=$maxheight/$imageheight;
|
if ( $imagepropv>$imageproph)
|
$imagepropv=$imageproph;
|
$imagehsize= ceil($imagewidth*$imagepropv) ;
|
$imagevsize=ceil($imageheight*$imagepropv) ;
$im=imagecreate($imagehsize, $imagevsize);
|
ImageInterlace($im,1);
|
imagecopyresized ($im, $im1, 0, 0, 0, 0, $imagehsize, $imagevsize, ImageSX($im1), ImageSY($im1));
|
switch ($tipo)
|
{
|
case "jpg":
|
Header("Content-type: image/jpg");
|
Imagejpeg($im);
break;
|
case "png":
|
Header("Content-type: image/png");
|
Imagepng($im);
break;
|
default:
|
return;
|
}
ImageDestroy($im);
?>
|