Ver Mensaje Individual
  #6 (permalink)  
Antiguo 23/01/2007, 13:10
Avatar de deet
deet
 
Fecha de Ingreso: mayo-2005
Ubicación: Córdoba - Argentina
Mensajes: 269
Antigüedad: 19 años, 10 meses
Puntos: 1
Re: Redimencionar imagenes

yo siempre uso esta clase que utiliza GD y me va muy bien:

Código PHP:
<?
/**
* Crea una imagen nueva de un ancho y alto de terminado a partir de una imagen dada
*
*/
class CropImage {

    var 
$file;

    
    
/**
    * Constrructor de la clase
    *
    * @param string $file Nombre completo del fichero de imagen incluida la ruta al lugar temporal de existencia
    * @param string $origname Nombre original del fichero
    * @param string $targetPath Ruta completa donde será grabado el fichero
    * @param string $saveName Nombre del fichero con el que será grabado el archivo generado 
    * @param integer $newWidth Nuevo ancho del fichero
    * @param integer $newHeight Nuevo alto del fichero 
    * @param string $saveNameThumb Nombre del fichero thumb con el que será grabado el archivo generado
    * @param string $pathThumb Ruta completa donde será grabado el fichero thumb
    */
    
function CropImage($pathFile,$file$origname$saveName$targetPath$newWidth$newHeight,$saveNameThumb,$pathThumb,$pathSinMarca) {
        
        
$this->file $pathFile.$file;
        
$this->origname $origname;
        
$this->targName $saveName;
        
$this->targetPath $targetPath;
        
$this->targetPath1 $pathSinMarca;
        
$this->nw $newWidth;
        
$this->nh $newHeight;
        
$this->targNameThumb $saveNameThumb;
        
$this->targetPathThumb $pathThumb;
        
        
//Determinar el tipo de archivo en base a u extensión
    
if (!isset($type)) 
    { 
        
$ext explode('.'$origname); 
        
$ext $ext[count($ext)-1];
 
        switch(
strtolower($ext)) 
        { 
            case 
'jpg':
              
$this->type 'jpg'
            break; 
            case 
'jpeg':   
              
$this->type 'jpg'
            break; 
                
            default : 
              
$this->type strtolower($ext); 
            break; 
        } 
    }
        
//Crear la imagen de acuerdo al tipo de archivo de imagen
        
$this->im2 imagecreatetruecolor($this->nw$this->nh);
        
$this->im3 imagecreatetruecolor($this->nw$this->nh);
        switch (
$this->type)
    { 
      case 
'jpg':
        
$this->tmpImage = @imagecreatefromjpeg($this->file);     
        
$this->tmpImage1 = @imagecreatefromjpeg($this->file);     
      break; 
  
      case 
'gif'
          
$this->tmpImage = @imagecreatefromgif($this->file);    
          
$this->tmpImage1 = @imagecreatefromgif($this->file);     
      break; 
       
      case 
'png':
          
$this->tmpImage = @imagecreatefrompng($this->file);  
          
$this->tmpImage1 = @imagecreatefrompng($this->file);   
      break; 
      
    }
        
    
$this->ow  imagesx ($this->tmpImage);    // Original image width 
    
$this->oh  imagesy ($this->tmpImage);    // Original image height
    
    
}

    
    
/**
    * Crea la imagen de salida de acuerdo a un ancho, y alto
    *
    */
    
function outPutImage() {
        
imagecopyresampled($this->im2$this->tmpImage0000$this->nw$this->nh$this->ow$this->oh);    
        
imagecopyresampled($this->im3$this->tmpImage10000$this->nw$this->nh$this->ow$this->oh);
        
$mark=imagecreatefrompng("images/logoWatermark.png");
        
imagecopy($this->im2,$mark,(465-150)/2,(310-99)/2,0,0,150,99);
        
$this->saveImage();
    }
    
    
    
/**
    * Escribe la imagen físicamente
    *
    * @param integer $imagen Puntero a la imagen creada
    */
    
function saveImage() {
    
     switch (
$this->type
     { 
        case 
'jpg':
             
imagejpeg($this->im2$this->targetPath $this->targName100);
             
imagejpeg($this->im3$this->targetPath1 $this->targName100);
      break; 
      
      case 
'gif'
          
imagegif($this->im2$this->targetPath $this->targName);
          
imagegif($this->im3$this->targetPath1 $this->targName);
      break; 
      
      case 
'png'
             
imagepng($this->im2$this->targetPath $this->targName);
             
imagepng($this->im3$this->targetPath1 $this->targName);
      break;
      
     } 

    
imagedestroy($this->im2); 
    
imagedestroy($this->im3); 
        
    }

    function 
thumbnail(){
        
$file_orig=$this->file;
        
$file_dest=$this->targNameThumb;
        
//Dimensions
        
$outWidth=206$outHeight=131;
        
//Do it
        
switch ($this->type
         { 
            case 
'jpg':
                
$imgSrc=imagecreatefromjpeg($file_orig);
          break; 
          
          case 
'gif'
                
$imgSrc=imagecreatefromgif($file_orig);
          break; 
          
          case 
'png'
                
$imgSrc=imagecreatefrompng($file_orig);
          break;
          
         } 
        
$srcWidth=imagesx($imgSrc);
        
$srcHeight=imagesy($imgSrc);
        
//Make thumbnails
        
$imgOut=imagecreatetruecolor($outWidth,$outHeight);
        
imagerectangle($imgOut,0,0,$outWidth,$outHeight,imagecolorallocate($imgOut,0,0,0));
        
//Copy them proportionatly
        
$dx=0$dy=0$dw=$outWidth$dh=$outHeight;
        if (
$outWidth*$srcHeight!=$outHeight*$srcWidth) { //Non-proportional, cut-off
            //Which dimensions is larger
            
if ($srcWidth>$srcHeight) { //Empty space on top and bottom
                
$dw=$outWidth;
                
$dh=($dw*$srcHeight)/$srcWidth;
                
$dy=($outHeight-$dh)/2;
            }else { 
//Empty space on left and right
                
$dh=$outHeight;
                
$dw=($dh*$srcWidth)/$srcHeight;
                
$dx=($outWidth-$dw)/2;
            }
        }
        
imagecopyresampled($imgOut,$imgSrc,$dx,$dy,0,0,
        
$dw,$dh,$srcWidth,$srcHeight);//imagecopyresized for lower quality
        //Create the thumbnail and destroy everything else
        //$markThumb=imagecreatefrompng("images/logoWatermarkThumb.png"); 
        //imagecopy($imgOut,$markThumb,($outWidth-102)/2,($outHeight-68)/2,0,0,102,68);
        
switch ($this->type
        { 
        case 
'jpg':
            
imagejpeg($imgOut$this->targetPathThumb $this->targNameThumb100);
        break; 
        
        case 
'gif'
            
imagegif($imgOut$this->targetPathThumb $this->targNameThumb);
        break; 
        
        case 
'png'
            
imagepng($imgOut$this->targetPathThumb $this->targNameThumb);
        break;
        
        } 
        
imagedestroy($imgSrc);
        
imagedestroy($imgOut);
    }        
    
}
?>
Código PHP:
include("cls.CropImage.php");
    
$path="../ruta/del/original/";
    
$file=$image;//nombre tomado de $_FILES
    
$file_orig=$image;//nombre tomado de $_FILES
    
$tamanyo=getimagesize($path.$file);
        
//en este caso tengo un ancho fijo para lo cual hago proporcional la altura
        //pero se pueden poner los valores que quieran
    
$ancho=465;
    
$alto=($tamanyo[1]*465)/$tamanyo[0];
        
//como verán en la próxima línea tengo tres directorios diferentes
        //eso es porque necesitaba crear tres tipos de fotos mas la original
        //una es tamaño medio, la otra tamaño medio pero con marca de agua
        //y la última de tamaño pequeño (thumbnail)
    
$new_image= new CropImage($path,$file$file$file"../fotos/media/"$ancho."px"$alto."px",$file,"../fotos/thumb/","../fotos/media1/");
       
//esto me guarda la imágenes media
    
$new_image->outPutImage();
        
//esto me crea los thumbnails
    
$new_image->thumbnail(); 
esta clase me la han pasado no me acuerdo quién, yo le realicé las modificaciones de foto con marca de agua y el thumbnail, así que les dejos a ustedes, la posibilidad de cambiarlo a gusto, si lo pueden mejorar me gustaría que lo posteen.