Ver Mensaje Individual
  #4 (permalink)  
Antiguo 20/07/2009, 15:54
Avatar de ElJavista
ElJavista
Colaborador
 
Fecha de Ingreso: marzo-2007
Ubicación: Lima Perú
Mensajes: 2.231
Antigüedad: 17 años, 1 mes
Puntos: 67
Respuesta: Auto ajustar el tamaño de imagen

Bueno, podrías usar la clase que te estoy dejando, mediante esta se puede redimensionar las fotos de distintos modos: puedes crear una imagen definiendo solo el ancho o el alto y la otra dimensión se autoajusta proporcionalmente (método makeThumb). También puedes crear una imagen que no exceda unas medidas determinadas, el resutado también es proporcional a la imagen inicial (método makeCuadro). Asimismo puedes crear un corte, con esta opción puedes crear una imagen con unas medidas determinadas y no deformará la imagen.

Mi sugerencia es crear el nuevo archivo cuando el cliente haya cargado la foto, espero que te sirva.

Código PHP:

<?php
  
class Imagen {
        private 
$exist false;
        private 
$width 0;
        private 
$height 0;
        private 
$source null;
        private 
$thumb null;

        function 
Imagen($img) {
                 if (
file_exists($img)) {
                    list(
$wi$he$tip) = getimagesize($img);
                    
$this->width $wi;
                    
$this->height $he;

                    if (
$tip == 1$this->source imagecreatefromgif($img);
                    elseif (
$tip == 2$this->source imagecreatefromjpeg($img);
                    elseif (
$tip == 3$this->source imagecreatefrompng($img);
                    
$this->exist true;
                 }
        }

        private function 
createThumb($w$h) {
                
$this->thumb imagecreatetruecolor($w$h);
        }

        function 
makeThumb($ruta$w$h$opc false) {
                 if (
$this->exist) {
                    
$wi $this->width;
                    
$he $this->height;

                    if (
$w) {
                       
$wp $w;
                       
$hp round(($he $wi) * $wp);
                    }  elseif (
$h) {
                       
$hp $h;
                       
$wp round(($wi $he) * $hp);
                    }

                    
$this->createThumb($wp$hp);
                    
$this->reDim(0000$wp$hp$wi$he);

                    if (
$opc) return $this->thumb;
                    else return 
$this->setImg($ruta);
                 }  else return 
false;
        }

        function 
makeCuadro($ruta$w$h$opc false) {
                 if (
$this->exist) {
                    if (
$w && $h) {
                       
$wi $this->width;
                       
$he $this->height;

                       if (
$wi $w || $he $h) {
                          if (
$wi $w $he $h) {
                             
$wp $w;
                             
$hp round(($he $wi) * $wp);
                          }  else {
                             
$hp $h;
                             
$wp round(($wi $he) * $hp);                       
                          }
                       }  else {
                          
$wp $wi;
                          
$hp $he;
                       }

                       
$this->createThumb($wp$hp);
                       
$this->reDim(0000$wp$hp$wi$he);

                       if (
$opc) return $this->thumb;
                       else return 
$this->setImg($ruta);
                    }  else return 
false;
                 }  else return 
false;
        }

        function 
makeCorte($ruta$w$h$opc false) {
                 if (
$this->exist) {
                    
$wi $this->width;
                    
$he $this->height;

                    if (
$w && $h) {
                       if (
$wi $w $he $h) {
                          
$wp $wi;
                          
$hp round(($h $w) * $wp);
                       }  else {
                          
$hp $he;
                          
$wp round(($w $h) * $hp);
                       }
                       
$lf round(($wi $wp) / 2);
                       
$tp round(($he $hp) / 2);

                       
$this->createThumb($w$h);
                       
$this->reDim(00$lf$tp$w$h$wp$hp);

                       if (
$opc) return $this->thumb;
                       else return 
$this->setImg($ruta);
                    }  else return 
false;
                 }  else return 
false;
        }

        private function 
reDim($dstX$dstY$srcX$srcY$dstW$dstH$srcW$srcH) {
                
imagecopyresampled($this->thumb$this->source$dstX$dstY$srcX$srcY$dstW$dstH$srcW$srcH);
        }

        private function 
setImg($ruta) {
                return 
imagejpeg($this->thumb$ruta);
        }
}
if (isset(
$_GET["img"])) {
   
$img $_GET["img"];
   if (
file_exists($img)) {
      
$ima = new Imagen($img);
      
$tip $_GET["tip"];
      
$w $_GET["w"];
      
$h $_GET["h"];

      if (
$tip == 0$thumb $ima->makeThumb(""$w$htrue);
      if (
$tip == 1$thumb $ima->makeCuadro(""$w$htrue);
      if (
$tip == 2$thumb $ima->makeCorte(""$w$htrue);

      if (
$thumb) {
         
header('Content-type: image/jpeg');
         
imagejpeg($thumb);
      }  else echo 
"Error";
   }
}
?>