Foros del Web » Programando para Internet » PHP »

Reducir tamaño de imagen

Estas en el tema de Reducir tamaño de imagen en el foro de PHP en Foros del Web. Hola: Me gustaria saber cómo se hace para: leer el ancho y el alto de una imagen, y reducirlo a un tamaño especifico, o a ...
  #1 (permalink)  
Antiguo 17/01/2005, 07:55
Avatar de Chichoauza  
Fecha de Ingreso: enero-2005
Mensajes: 106
Antigüedad: 19 años, 3 meses
Puntos: 0
Reducir tamaño de imagen

Hola:
Me gustaria saber cómo se hace para:

leer el ancho y el alto de una imagen, y reducirlo a un tamaño especifico, o a una escala, he leido que esto es posible mediante la libreria GD, pero no encontre nada sobre la forma de reducir las imagenes , pues bue, aqui si alguen sabe como hacerlo, estara muy agradecido

saludos a todos.
  #2 (permalink)  
Antiguo 17/01/2005, 08:21
O_O
 
Fecha de Ingreso: enero-2002
Ubicación: Santiago - Chile
Mensajes: 34.417
Antigüedad: 22 años, 3 meses
Puntos: 129
Si miras las FAQ's de este foro PHP o buscas en el foro PHP por el término "Thumbnail" veras muchos ejemplos para tal fin.

Un saludo,
  #3 (permalink)  
Antiguo 17/01/2005, 09:36
 
Fecha de Ingreso: enero-2005
Ubicación: Barcelona (España)
Mensajes: 134
Antigüedad: 19 años, 3 meses
Puntos: 0
Te envío un par de funciones. La primera sólo "fuerza" la imagen a un width o height máximo. La segunda la reduce realmente, es decir, reemplaza la original por una copia con un width y height determinado.
Espero que te sirva.

function reduce_imagen($imagen, $max_width, $max_height) {

//if (!$max_width)
// $max_width = 100;
//if (!$max_height)
// $max_height = 80;


$size = GetImageSize($imagen);
$width = $size[0];
$height = $size[1];



//if ($width == 0)
//$width = 1;
//if ($height == 0)
//$height = 1;

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

global $tn_width, $tn_height;

if ( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

return $tn_width; $tn_height;

}


function recorta_imagen($imagen, $max_width, $max_height) {

//if (!$max_width)
// $max_width = 100;
//if (!$max_height)
// $max_height = 80;


$size = GetImageSize($imagen);
$width = $size[0];
$height = $size[1];



//if ($width == 0)
//$width = 1;
//if ($height == 0)
//$height = 1;

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if ( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

$src = ImageCreateFromJpeg($imagen);
$dst = ImageCreateTrueColor($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,$tn_width,$tn_height,$width,$height);
header('Content-type: image/jpeg');
ImageJpeg($dst, $imagen, 100);
ImageDestroy($src);
ImageDestroy($dst);



}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:13.