Ver Mensaje Individual
  #11 (permalink)  
Antiguo 13/07/2010, 09:34
buthas
 
Fecha de Ingreso: marzo-2009
Mensajes: 13
Antigüedad: 15 años, 1 mes
Puntos: 0
Respuesta: Guardar thumbnails

Cita:
Iniciado por jackson666 Ver Mensaje
Te dejo una clase que tenia yo hace tiempo que te la guarda despues de redimensionarla

Código PHP:
class ImgHandler{

    var 
$imagen;//nombre de la imagen cargada
    
var $ext;//extension nueva imagen cargada
    
    
var $ancho;//ancho de nueva imagen
    
var $alto;//alto de nueva imagen
    
    
function __construct($ruta){
        if(
file_exists($ruta)){
        
$this->imagen=$ruta;
    }else{
        echo 
"Ruta de Imagen Incorrecta";
    }
        return 
true;
    }
    
    function 
resizeImg($newName,$width,$height){
        
        
$tamano=getimagesize($this->imagen);
        
$this->ancho=$tamano[0];
        
$this->alto=$tamano[1];
                    
        
$imagenRedim=imagecreatetruecolor($width,$height);      
       
        list(
$nombre,$extension)=explode(".",$this->imagen);
        
$this->ext=$extension;
        
        if(empty(
$newName)){
            
$newName=$nombre;
        }        
        
        echo 
"<br /><b>Nombre de la Imagen Original:</b> ".$this->imagen."<br /><br />";
        echo 
"<b>Nombre de la Imagen Transformada:</b> ".$newName.".".$this->ext."<br /><br />";
        
        echo 
"<u>Tamaño de la Imagen Original:</u><br />Ancho--> ".$tamano[0]."<br />Alto---> ".$tamano[1]."<br /><br />";
        echo 
"<u>Tamaño de la Imagen Transformada:</u><br />Ancho--> ".$width."<br />Alto---> ".$height."<br /><br />";
       
        if(
strtolower($this->ext)=="jpg" or strtolower($this->ext)=="jpeg"){
            
$crear=imagecreatefromjpeg($this->imagen);
            
imagecopyresampled($imagenRedim,$crear,0,0,0,0,$width,$height,$this->ancho,$this->alto);
            
imagejpeg($imagenRedim,$newName.".jpg",100);
            echo 
"Formato---> jpg<br />";
            
imagedestroy($crear);
       
        }else if(
strtolower($this->ext)=="png"){
            echo 
"Formato---> png<br />";
            
$crear=imagecreatefrompng($this->imagen);
            
imagecopyresampled($imagenRedim,$crear,0,0,0,0,$width,$height,$this->ancho,$this->alto);
            
imagepng($imagenRedim,$newName.".png");
            
imagedestroy($crear);
                
        }else if(
strtolower($this->ext)=="gif"){
            echo 
"Formato---> gif<br />";
            
$crear=imagecreatefromgif($this->imagen);
            
imagecopyresampled($imagenRedim,$crear,0,0,0,0,$width,$height,$this->ancho,$this->alto);
            
imagegif($imagenRedim,$newName.".gif");
            
imagedestroy($crear);
        
        }else{
            echo 
"<b><font color='red'>El nombre o extension de la imagen es Incorrecto</font></b>";
        }
       
    }
   
    function 
scaleImg($newName,$percent){
        
       
#echo "<br /><b>Nombre de Imagen:</b> ".$this->imagen."<br />";
        
        
$tamano=getimagesize($this->imagen);
        
$this->ancho=$tamano[0];
        
$this->alto=$tamano[1];
        
        
#Redimension
        
$finalWidth=$this->ancho*$percent;
        
$finalHeight=$this->alto*$percent;
        
        
$imgEscalada=imagecreatetruecolor($finalWidth,$finalHeight);
               
        list(
$noSirve,$sirve)=explode(".",$this->imagen);
        
$this->ext=$sirve;        
       
        if(
strtolower($this->ext)=="jpg" or strtolower($this->ext)=="jpeg"){
            
#echo "Formato---> jpg<br />";
            
$crear=imagecreatefromjpeg($this->imagen);
            
imagecopyresampled($imgEscalada,$crear,0,0,0,0,$finalWidth,$finalHeight,$this->ancho,$this->alto);
            
imagejpeg($imgEscalada,$newName.".jpg",100);
                  
        }else if(
strtolower($this->ext)=="png"){
            
#echo "Formato---> png<br />";
            
$crear=imagecreatefrompng($this->imagen);
            
imagecopyresampled($imgEscalada,$crear,0,0,0,0,$finalWidth,$finalHeight,$this->ancho,$this->alto);
            
imagepng($imgEscalada,$newName.".png");
       
        }else if(
strtolower($this->ext)=="gif"){
            
#echo "Formato---> gif<br />";
            
$crear=imagecreatefromgif($this->imagen);
            
imagecopyresampled($imgEscalada,$crear,0,0,0,0,$finalWidth,$finalHeight,$this->ancho,$this->alto);
            
imagegif($imgEscalada,$newName.".gif");
        }
        
    }

Para usarlo:

Ejemplo de escala

Código PHP:
<?php
include("imghandler.class.php");

$thumb=new ImgHandler("img/1.jpg");

$thumb->scaleImg("1",0.35);
?>
Ejemplo de redimension

Código PHP:
<?php
include("imghandler.class.php");

$thumb=new ImgHandler("img/1.jpg");

$thumb->resizeImg("algo"100100);
?>
Modificalo a gusto para que te lo guarde donde quieras...
No me funciona con imagenes externas, Alguien sabe los cambios que hay que haber para que funcione con imagenes externas al servidor.

el erro que me da es este, pero solo con imagnes externas:
Código:
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home/..../public_html/..../imghandler.class.php  on line 21
gracias por su tiempo.