Foros del Web » Programando para Internet » PHP »

Redimensionar imágenes de un directorio

Estas en el tema de Redimensionar imágenes de un directorio en el foro de PHP en Foros del Web. 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" /> ...
  #1 (permalink)  
Antiguo 20/08/2010, 13:33
 
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
  #2 (permalink)  
Antiguo 20/08/2010, 16:28
Avatar de ebe
ebe
 
Fecha de Ingreso: marzo-2004
Ubicación: Guatemala
Mensajes: 363
Antigüedad: 20 años, 1 mes
Puntos: 11
Respuesta: Redimensionar imágenes de un directorio

Hola

con opendir() y readdir() puedes obtener el puntero del directorio y empezar a leer su contenido. Luego para redimensionar podrias usar algo como esto

Código PHP:
Ver original
  1. function resizeImage($file,$scale="",$width="",$height="")
  2.  {
  3.         // If they wish to scale the image.
  4.         if (isset($scale))
  5.         {
  6.                // Create our image object from the image.
  7.                $fullImage = imagecreatefromjpeg($file);
  8.                // Get the image size, used in calculations later.
  9.                $fullSize = getimagesize($file);
  10.                // If there is NOT a thumbnail for this image, make one.
  11.                if (!file_exists("tn_".$file))
  12.                {
  13.                       // Create our thumbnail size, so we can resize to this, and save it.
  14.                       $tnImage = imagecreatetruecolor($fullSize[0]/$scale, $fullSize[1]/$scale);
  15.                       // Resize the image.
  16.                       imagecopyresampled($tnImage,$fullImage,0,0,0,0,$fullSize[0]/$scale,$fullSize[1]/$scale,$fullSize[0],$fullSize[1]);
  17.                       // Create a new image thumbnail.
  18.                       imagejpeg($tnImage, "tn_".$file);
  19.                      
  20.                       // Clean Up.
  21.                       imagedestroy($fullImage);
  22.                       imagedestroy($tnImage);
  23.                       // Return our new image.
  24.                       return "tn_".$file;
  25.                }
  26.                // If there is a thumbnail file, lets just load it.  
  27.                else
  28.                       return "tn_".$file;
  29.         }
  30.         // If they want to force whatever size they want.
  31.         elseif (isset($width) && isset($height))
  32.         {
  33.                return "tn_".$file;
  34.         }
  35.         else
  36.         {
  37.                return false;
  38.         }
  39.  }


con GD activado.

saludos
__________________
http://dev.wsnetcorp.com

Etiquetas: directorio, redimensionar
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 06:50.