Foros del Web » Programando para Internet » PHP »

Upload de imagenes me las deja cuadradas!

Estas en el tema de Upload de imagenes me las deja cuadradas! en el foro de PHP en Foros del Web. Implementé el uploader de Snd234, tomandolo desde aquí: http://www.forosdelweb.com/f18/redim...ad-php-654167/ La cuestion es que me deja las fotos de 750x750. Es decir, no me redimensiona respetando ...
  #1 (permalink)  
Antiguo 09/05/2010, 11:21
Avatar de mayid
Colaborador
 
Fecha de Ingreso: marzo-2009
Ubicación: BsAs
Mensajes: 4.014
Antigüedad: 15 años
Puntos: 101
Upload de imagenes me las deja cuadradas!

Implementé el uploader de Snd234, tomandolo desde aquí:

http://www.forosdelweb.com/f18/redim...ad-php-654167/

La cuestion es que me deja las fotos de 750x750. Es decir, no me redimensiona respetando las proporciones. Que puede ser? Yo veo en el ejemplo que hay que pasarle un solo parámetro.

Pongo mi implementación:

Código PHP:
<?php

    
/**
     * Thumbnail
     * 
     * Crea una miniatura de una imagen y la guarda en un formato especifico
     * 
     * @package 
     * @author Snd234
     * @copyright 2008
     * @version 1.0
     * @access public
     */
    
class Thumbnail {
        
// informacion de la miniatura
        
private $thumbnail;
        private 
$thumbnail_width;
        private 
$thumbnail_height;
        
        
// informacion de la imagen original
        
private $image;
        private 
$image_width;
        private 
$image_height;
        private 
$image_type;
        
        public 
$error;
        
        
/**
         * Thumbnail::__construct()
         * 
         * @param mixed $source
         * @return
         */
        
public function __construct($source) {
            
$image_info getimagesize($source);
            
            if(
$image_info) {
                
$this->image_width $image_info[0];
                
$this->image_height $image_info[1];
                
$this->image_type $image_info[2];
                
                switch(
$this->image_type) {
                    case 
IMAGETYPE_JPEG: {
                        
$this->image imagecreatefromjpeg($source);
                        break;
                    }
                    
                    case 
IMAGETYPE_GIF: {
                        
$this->image imagecreatefromgif($source);
                        break;
                    }
                    
                    case 
IMAGETYPE_PNG: {
                        
$this->image imagecreatefrompng($source);
                        break;
                    }
                    
                    default: {
                        
$this->error "Formato no soportado";
                        break;
                    }
                }
            } 
            else {
                
$this->error "Formato invalido";
            }
        }
        
        
/**
         * Thumbnail::resize()
         * 
         * @param mixed $width
         * @param integer $height
         * @return void
         */
        
public function resize($width$height 0) {
            
$this->thumbnail_width $width;
            
            if(
$height == 0) {
                
$this->thumbnail_height $width;
            } else {
                
$this->thumbnail_height $height;
            }
            
            
$this->thumbnail imagecreatetruecolor($this->thumbnail_width$this->thumbnail_height);
            
            
imagecopyresampled(
                
$this->thumbnail$this->image0000,
                
$this->thumbnail_width$this->thumbnail_height,
                
$this->image_width$this->image_height
            
);
        }
        
        
/**
         * Thumbnail::save_jpg()
         * 
         * @param mixed $dir
         * @param mixed $name
         * @param integer $quality
         * @return
         */
        
public function save_jpg($dir$name$quality 80) {
            
$path $dir $name ".jpg";
            
imagejpeg($this->thumbnail$path$quality);
            
            
imagedestroy($this->thumbnail);
            return 
true;
        }
        
        
/**
         * Thumbnail::save_gif()
         * 
         * @param mixed $dir
         * @param mixed $name
         * @return
         */
        
public function save_gif($dir$name) {
            
$path $dir $name image_type_to_extension(IMAGETYPE_GIF);
            
imagegif($this->thumbnail$path);
            
            
imagedestroy($this->thumbnail);
            return 
true;
        }
        
        
/**
         * Thumbnail::save_png()
         * 
         * @param mixed $dir
         * @param mixed $name
         * @return
         */
        
public function save_png($dir$name) {
            
$path $dir $name image_type_to_extension(IMAGETYPE_PNG);
            
imagegif($this->thumbnail$path);
            
            
imagedestroy($this->thumbnail);
            return 
true;
        }
    }
    
?>
Código PHP:
$thumb = new Thumbnail($temp);
        if(
$thumb->error) {
            echo 
$thumb->error;
        } else {
            
$thumb->resize(750);
... ... ... 
  #2 (permalink)  
Antiguo 09/05/2010, 11:23
Avatar de mayid
Colaborador
 
Fecha de Ingreso: marzo-2009
Ubicación: BsAs
Mensajes: 4.014
Antigüedad: 15 años
Puntos: 101
Respuesta: Upload de imagenes me las deja cuadradas!

Ah! Ya veo. Si no se le pasa el parametro altura, se usa la misma altura que el ancho.

Se puede definir manualmente la altura. Pero yo quisiera una redimension proporcional, respetando el alto en relación al ancho. No forzar la altura a un valor manualmente digitado:

Cita:
public function resize($width, $height = 0) {
$this->thumbnail_width = $width;

if($height == 0) {
$this->thumbnail_height = $width;
} else {
$this->thumbnail_height = $height;
}
  #3 (permalink)  
Antiguo 09/05/2010, 12:40
Avatar de mayid
Colaborador
 
Fecha de Ingreso: marzo-2009
Ubicación: BsAs
Mensajes: 4.014
Antigüedad: 15 años
Puntos: 101
Respuesta: Upload de imagenes me las deja cuadradas!

Ok. Encontré la formula:

Cita:
if($height == "auto"){

$this->thumbnail_height = (($this->image_height)*$width)/($this->image_width) ;

}
Ahora lo que me queda es prevenir que me agrande las imagenes! No se por qué me redimensiona todo aun cuando la foto es más chica que el valor que le doy de maximo...

Listo!

Cita:
$proportions = getimagesize($_FILES["file"]["tmp_name"]);
$ImageWidth = $proportions[1];


if ($ImageWidth > 750) {

$thumb->resize(750,"auto");
..
..

} elseif (($ImageWidth <= 750) and (move_uploaded_file($_FILES["file"]["tmp_name"], "../".$folder.$file))) {

....
}

Última edición por mayid; 09/05/2010 a las 13:07

Etiquetas: imagenes, upload
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 17:15.