Ver Mensaje Individual
  #1 (permalink)  
Antiguo 20/08/2010, 13:33
fcopacheco
 
Fecha de Ingreso: enero-2008
Mensajes: 68
Antigüedad: 16 años, 3 meses
Puntos: 1
Redimensionar imágenes de un directorio

Aquí les dejo un código para redimensionar imágenes de un directorio con PHP.

Espero que les sirva de ayuda.

Código PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Redimensionador</title>
<script>

cont = 0;
function puntos(){    //Función para el aviso de espera
    if(cont>10){
        document.getElementById('puntitos').innerHTML = '';
    }
    document.getElementById('puntitos').innerHTML += '.&nbsp;&nbsp;';
    cont++;
    setTimeout("puntos()",500);
}

function avisar(){    //Muestra el aviso de espera mientras se redimensionan las imágenes
    puntos();
    document.getElementById('aviso').style.visibility = 'visible';
}

</script>
<style>
div#aviso{
    border:10px solid #000;
    width:450px;
    height:350px;
    position:absolute;
    left:50%;
    top:50%;
    margin-left:-225px;
    margin-top:-175px;
    visibility:hidden;
    text-align:center;
    vertical-align:middle;
}
</style>
</head>

<body>

<h1>Redimensionador, de directorio</h1>
<h3>Este archivo debe ser copiado en el directorio que contiene las imágenes a redimensionar</h3>
<?php
if(isset($_GET["width"])){
    
    
// Separa el nombre base de su extensión
    
function separar_extension($arch){
        
$ext pathinfo($archPATHINFO_EXTENSION);
        
$base basename($arch'.'.$ext);
        return array(
$base,$ext);
    }
    
    class 
resizer {
        
        public 
$nombre;
        public 
$retorno;
        
        function 
resizer($nombre){
            
$this->retorno NULL;
            
$this->nombre $nombre;
        }
        
        function 
base_extension(){
            return 
separar_extension($this->nombre);
        }
        
        function 
is_image(){
            list(
$base,$ext)=$this->base_extension();
            if(
$ext=="JPG" || $ext=="PNG" || $ext=="GIF" || $ext=="jpg" || $ext=="png" || $ext=="gif")
                return 
true;
            else
                return 
false;
        }
        
        function 
get_name(){
            list(
$base,$ext) = $this->base_extension();
            return 
"$base.$ext";
        }
        
        function 
set_retorno($ret){
            
$this->retorno $ret;
        }
        
        function 
resize($nw=400,$hmax=460){
            
            
$imagen=getimagesize($this->nombre);
            
            
$w $imagen[0];
            
$h $imagen[1];
            
$tip $imagen[2];
            
            if(
$tip==1)$img=imagecreatefromgif($this->nombre);
            if(
$tip==2)$img=imagecreatefromjpeg($this->nombre);
            if(
$tip==3)$img=imagecreatefrompng($this->nombre);
    
            
$nh = ($nw*$h/$w);
            if(
$nh>$hmax){
                
$nh $hmax;
                
$nw $w*$nh/$h;
            }
            
            
$thumb imagecreatetruecolor($nw,$nh);
            
$blanco imagecolorallocate($thumb,255,255,255);
            
imagefill($thumb,0,0,$blanco);
            
            
imagecopyresampled($thumb$img0000$nw$nh$w$h);
            
            list(
$base,$ext) = separar_extension($this->nombre);
            
            if(
$tip==1){
                if(
$this->retorno==NULL)$this->retorno "thumb_$base.gif";
                
imagegif($thumb,$this->retorno);
            }
            elseif(
$tip==2){
                if(
$this->retorno==NULL)$this->retorno "thumb_$base.jpg";
                
imagejpeg($thumb,$this->retorno);
            }
            elseif(
$tip==3){
                if(
$this->retorno==NULL)$this->retorno "thumb_$base.png";
                
imagepng($thumb,$this->retorno);
            }
            
            
imagedestroy($thumb);
            
            return 
$this->retorno;
        }
    }
    
    
    
//Redimensionamiento del directorio
    
$dir opendir(".");
    while(
$file=readdir($dir)){
        
$resizer = new resizer($file);
        if(!
$resizer->is_image())continue;
        
$resizer->set_retorno($resizer->get_name());
        
?>
        <p><a href="<?=$resizer->get_name()?>"><?=$resizer->get_name()?></a></p>
        <?php
        $resizer
->resize($_GET["width"],$_GET["hmax"]);
    }
}else{
    
?>
    <form method="get" onSubmit="avisar()">
    <p>width: <input name="width"/></p>
    <p>max-height: <input name="hmax" /></p>
    <p><input type="submit" value="Redimensionar" /></p>
    </form>
    
    <div id="aviso">
        <h3>Espere mientras se redimensionan las imágenes</h3>
        <p id="puntitos"></p>
    </div>
    
    <?php
}
?>

</body>
</html>
Si hay algún problema, no duden en comentarlo para así mejorar este código.

Última edición por fcopacheco; 20/08/2010 a las 14:05