[Php-avanzado] Redimension de imagenes
Gabriel Cappellano
gabrielcappellano en gmail.com
Mie Jun 10 18:56:00 ART 2009
Hola lucas! Te paso una funcion que uso yo, y me funciona!
function generareThumb($file, $savePath, $thumbD=100){
//Obtenemos la informacion de la imagen, el array info tendra los
siguientes indices:
// 0: ancho de la imagen
// 1: alto de la imagen
// mime: el mime_type de la imagen
$info = getimagesize($file);
//Dependiendo del mime type, creamos una imagen a partir del archivo
original:
switch($info['mime']){
case 'image/jpeg':
$image = imagecreatefromjpeg($file);
break;
case 'image/gif';
$image = imagecreatefromgif($file);
break;
case 'image/png':
$image = imagecreatefrompng($file);
break;
}
//Si el ancho es igual al alto, la imagen ya es cuadrada, por lo que
podemos ahorrarnos unos pasos:
if($info[0] == $info[1]){
$xpos = 0;
$ypos = 0;
$width = $info[1];
$height = $info[1];
}
//Si la imagen no es cuadrada, hay que hacer un par de averiguaciones:
else{
if($info[0] > $info[1]){
//imagen horizontal
$xpos = ceil(($info[0] - $info[1]) /2);
$ypos = 0;
$width = $info[1];
$height = $info[1];
}else{
//imagen vertical
$ypos = ceil(($info[1] - $info[0]) /2);
$xpos = 0;
$width = $info[0];
$height = $info[0];
}
}
//Creamos una nueva imagen cuadrada con las dimensiones que queremos:
$image_new = imagecreatetruecolor($thumbD, $thumbD);
$bgcolor = imagecolorallocate($image_new, 255, 255, 255);
imagefilledrectangle($image_new, 0, 0, $thumbD, $thumbD, $bgcolor);
imagealphablending($image_new, true);
//Copiamos la imagen original con las nuevas dimensiones
imagecopyresampled($image_new, $image, 0, 0, $xpos, $ypos, $thumbD,
$thumbD, $width, $height);
//Guardamos la nueva imagen como jpg con una calidad del 85%
imagejpeg($image_new, $savePath, 90);
}
----- Original Message -----
From: "Lucas Calviño" <thelookus en gmail.com>
To: "Lista del curso de PHP Avanzado" <php-avanzado en pato2.fi.mdp.edu.ar>
Sent: Wednesday, June 10, 2009 5:56 PM
Subject: [Php-avanzado] Redimension de imagenes
Hola, tengo el siguiente problema. Traigo una imagen cargada desde un
formulario, todo bien, la guardo correctamente, pero luego quiero
generar la miniatura, así que chusmeando por mil páginas, armé esto y no
logro hacerlo funcionar, no se donde está el error.
Si alguien conoce alguna manera más simple para generar miniaturas, se
agradece mucho. Lucas.
$thumbpath='../../imgs/descargas/thumbs/';
$thumbname = "descarga".$id.'.'.$ext[1];
//REDIMENSIONAR
$anchura=180;
$hmax=180;
$nombre=$_FILES['foto1']['name'];
$datos = getimagesize($nombre);
if($datos[2]==1){$img = @imagecreatefromgif($nombre);}
if($datos[2]==2){$img = @imagecreatefromjpeg($nombre);}
if($datos[2]==3){$img = @imagecreatefrompng($nombre);}
$ratio = ($datos[0] / $anchura);
$altura = ($datos[1] / $ratio);
if($altura>$hmax){$anchura2=$hmax*$anchura/$altura;$altura=$hmax;$anchura=$anchura2;} $thumb = imagecreatetruecolor($anchura,$altura); imagecopyresampled($thumb, $img, 0, 0, 0, 0, $anchura,$altura, $datos[0], $datos[1]); if($datos[2]==1){header("Content-type: image/gif");imagegif($thumb, $thumbpath.$thumbname);} if($datos[2]==2){header("Content-type:image/jpeg");imagejpeg($thumb, $thumbpath.$thumbname, 75);} if($datos[2]==3){header("Content-type:image/png");imagepng($thumb, $thumbpath.$thumbname); } imagedestroy($thumb);_______________________________________________Php-avanzado mailing listPhp-avanzado en pato2.fi.mdp.edu.arhttp://www3.fi.mdp.edu.ar/cgi-bin/mailman/listinfo/php-avanzado
Más información sobre la lista de distribución Php-avanzado