Foros del Web » Programando para Internet » PHP »

redimencionar imagenes

Estas en el tema de redimencionar imagenes en el foro de PHP en Foros del Web. Hola amigos, bueno resulta que quiero redomecionar una imagen de 1600x1200 a 640x480 he intentando de todo he visto muchos ejemplos y trato de adaptarlos ...
  #1 (permalink)  
Antiguo 04/04/2010, 14:52
 
Fecha de Ingreso: diciembre-2009
Ubicación: Monterrey
Mensajes: 61
Antigüedad: 16 años, 9 meses
Puntos: 0
redimencionar imagenes

Hola amigos, bueno resulta que quiero redomecionar una imagen de 1600x1200 a 640x480 he intentando de todo he visto muchos ejemplos y trato de adaptarlos y nada que puedo, mi servidor soporta GD y al parecer todo esta bien.

Si alguien tiene un ejemplo se lo agradecere y Gracias por su tiempo y ayuda.
  #2 (permalink)  
Antiguo 04/04/2010, 15:34
Avatar de mauroave  
Fecha de Ingreso: julio-2009
Mensajes: 50
Antigüedad: 17 años, 2 meses
Puntos: 0
Respuesta: redimencionar imagenes

Hola amigo, te voy a pasar una clase para redimensionar que a mi me sirvio mucho

Esta es la clase:

Código PHP:
<?php 
class thumb { 
 
var 
$image; 
var 
$type; 
var 
$width; 
var 
$height; 
 
//---Método de leer la imagen 
function loadImage($name) { 
 
//---Tomar las dimensiones de la imagen 
$info = getimagesize($name); 
 
$this->width = $info[0]; 
$this->height = $info[1]; 
$this->type = $info[2]; 
 
//---Dependiendo del tipo de imagen crear una nueva imagen 
switch($this->type){ 
case 
IMAGETYPE_JPEG: 
$this->image = imagecreatefromjpeg($name); 
break; 
case 
IMAGETYPE_GIF: 
$this->image = imagecreatefromgif($name); 
break; 
case 
IMAGETYPE_PNG: 
$this->image = imagecreatefrompng($name); 
break; 
} 
} 
 
//---Método de guardar la imagen 
function save($name, $quality = 100) { 
 
//---Guardar la imagen en el tipo de archivo correcto 
switch($this->type){ 
case 
IMAGETYPE_JPEG: 
imagejpeg($this->image, $name, $quality); 
break; 
case 
IMAGETYPE_GIF: 
imagegif($this->image, $name); 
break; 
case 
IMAGETYPE_PNG: 
$pngquality = floor(($quality - 10) / 10); 
imagepng($this->image, $name, $pngquality); 
break; 
} 
} 
 
//---Método de mostrar la imagen sin salvarla 
function show() { 
 
//---Mostrar la imagen dependiendo del tipo de archivo 
switch($this->type){ 
case 
IMAGETYPE_JPEG: 
imagejpeg($this->image); 
break; 
case 
IMAGETYPE_GIF: 
imagegif($this->image); 
break; 
case 
IMAGETYPE_PNG: 
imagepng($this->image); 
break; 
} 
} 
 
//---Método de redimensionar la imagen sin deformarla 
function resize($value, $prop){ 
 
//---Determinar la propiedad a redimensionar y la propiedad opuesta 
$prop_value = ($prop == 'width') ? $this->width : $this->height; 
$prop_versus = ($prop == 'width') ? $this->height : $this->width; 
 
//---Determinar el valor opuesto a la propiedad a redimensionar 
$pcent = $value / $prop_value; 
$value_versus = $prop_versus * $pcent; 
 
//---Crear la imagen dependiendo de la propiedad a variar 
$image = ($prop == 'width') ? imagecreatetruecolor($value, $value_versus) : imagecreatetruecolor($value_versus, $value); 
 
//---Hacer una copia de la imagen dependiendo de la propiedad a variar 
switch($prop){ 
 
case 
'width': 
imagecopyresampled($image, $this->image, 0, 0, 0, 0, $value, $value_versus, $this->width, $this->height); 
break; 
 
case 
'height': 
imagecopyresampled($image, $this->image, 0, 0, 0, 0, $value_versus, $value, $this->width, $this->height); 
break; 
 
} 
 
//---Actualizar la imagen y sus dimensiones 
$info = getimagesize($name); 
 
$this->width = imagesx($image); 
$this->height = imagesy($image); 
$this->image = $image; 
 
} 
 
//---Método de extraer una sección de la imagen sin deformarla 
function crop($cwidth, $cheight, $pos = 'center') { 
 
//---Dependiendo del tamaño deseado redimensionar primero la imagen a uno de los valores 
if($cwidth > $cheight){ 
$this->resize($cwidth, 'width'); 
}else{ 
$this->resize($cheight, 'height'); 
} 
 
//---Crear la imagen tomando la porción del centro de la imagen redimensionada con las dimensiones deseadas 
$image = imagecreatetruecolor($cwidth, $cheight); 
 
switch(
$pos){ 
 
case 
'center': 
imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight); 
break; 
 
case 
'left': 
imagecopyresampled($image, $this->image, 0, 0, 0, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight); 
break; 
 
case 
'right': 
imagecopyresampled($image, $this->image, 0, 0, $this->width - $cwidth, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight); 
break; 
 
case 
'top': 
imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), 0, $cwidth, $cheight, $cwidth, $cheight); 
break; 
 
case 
'bottom': 
imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), $this->height - $cheight, $cwidth, $cheight, $cwidth, $cheight); 
break; 
 
} 
 
$this->image = $image; 
} 
 
} 
?>
Por ejemplo si quieres redimensionar seria algo asi

Código PHP:
$mythumb = new thumb();
$mythumb->loadImage("..Ruta hacia tu imagen original"); 
$mythumb->resize(300, "width"); 
$mythumb->save("ruta hacia la nueva imagen",100); 
Es un ejemplo el anterior, fijate y estudia bien la clase para ver si sirve a tus propósitos. Espero haber sido de ayuda. Saludos cordiales
  #3 (permalink)  
Antiguo 04/04/2010, 16:08
 
Fecha de Ingreso: diciembre-2009
Ubicación: Monterrey
Mensajes: 61
Antigüedad: 16 años, 9 meses
Puntos: 0
Respuesta: redimencionar imagenes

mauroave Gracias por tu ayuda, lo estoy probando y ya te comentare cual fue el resultado

Etiquetas: imagenes, redimencionar
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 23:18.