Foros del Web » Programando para Internet » PHP »

Autodimensionar imagenes

Estas en el tema de Autodimensionar imagenes en el foro de PHP en Foros del Web. hola, tengo una tareita media dificil y es que tengo un directorio con bastantes imagenes con un tamaño bien grande, y quiero hacer una aplicacion ...
  #1 (permalink)  
Antiguo 05/07/2004, 21:39
 
Fecha de Ingreso: septiembre-2003
Mensajes: 384
Antigüedad: 20 años, 7 meses
Puntos: 0
Pregunta Autodimensionar imagenes

hola, tengo una tareita media dificil y es que tengo un directorio con bastantes imagenes con un tamaño bien grande, y quiero hacer una aplicacion php que cuando le especifique el directorio de las imagenes el le ponga el tamaño que yo le determine a traves de variables.

Cualquier ayuda sera buena Thz de ante mano
  #2 (permalink)  
Antiguo 05/07/2004, 21:51
Avatar de xcorpion  
Fecha de Ingreso: octubre-2003
Ubicación: m é x i c o
Mensajes: 676
Antigüedad: 20 años, 6 meses
Puntos: 4
mira yo uso esta clase para cambiar el tamaño de las imagenes
si tienes las imagenes nombradas numericamente y en forma continua es facil solo tendras que utilizar la estructura de control while para repetir la operacion tantas veces como sea necesario. Tambien puedes crear una lista de archivos que estan en la carpeta por medio del comando dir *.* /b -> listaarchivos.txt del interprete de comandos de windows.

Código PHP:
<?
##############################################
# Shiege Iseng Resize Class
# 11 March 2003
# [email][email protected][/email]
# [url]http://kentung.f2o.org/scripts/thumbnail/[/url]
################
# Thanks to :
# Dian Suryandari <[email protected]>
class thumbnail
{
    var 
$img;

    function 
thumbnail($imgfile)
    {
        
//detect image format
        
$this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
        
$this->img["format"]=strtoupper($this->img["format"]);
        if (
$this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
            
//JPEG
            
$this->img["format"]="JPEG";
            
$this->img["src"] = ImageCreateFromJPEG ($imgfile);
        } elseif (
$this->img["format"]=="PNG") {
            
//PNG
            
$this->img["format"]="PNG";
            
$this->img["src"] = ImageCreateFromPNG ($imgfile);
        } elseif (
$this->img["format"]=="GIF") {
            
//GIF
            
$this->img["format"]="GIF";
            
$this->img["src"] = ImageCreateFromGIF ($imgfile);
        } elseif (
$this->img["format"]=="WBMP") {
            
//WBMP
            
$this->img["format"]="WBMP";
            
$this->img["src"] = ImageCreateFromWBMP ($imgfile);
        } else {
            
//DEFAULT
            
echo "Not Supported File";
            exit();
        }
        @
$this->img["lebar"] = imagesx($this->img["src"]);
        @
$this->img["tinggi"] = imagesy($this->img["src"]);
        
//default quality jpeg
        
$this->img["quality"]=75;
    }

    function 
size_height($size=100)
    {
        
//height
        
$this->img["tinggi_thumb"]=$size;
        @
$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
    }

    function 
size_width($size=100)
    {
        
//width
        
$this->img["lebar_thumb"]=$size;
        @
$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
    }

    function 
size_auto($size=75)
    {
        
//size
        
if ($this->img["lebar"]>=$this->img["tinggi"]) {
            
$this->img["lebar_thumb"]=$size;
            @
$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
        } else {
            
$this->img["tinggi_thumb"]=$size;
            @
$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
         }
    }

    function 
jpeg_quality($quality=75)
    {
        
//jpeg quality
        
$this->img["quality"]=$quality;
    }

    function 
show()
    {
        
//show thumb
        
@Header("Content-Type: image/".$this->img["format"]);

        
/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
        
$this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
            @
imagecopyresized ($this->img["des"], $this->img["src"], 0000$this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

        if (
$this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
            
//JPEG
            
imageJPEG($this->img["des"],"",$this->img["quality"]);
        } elseif (
$this->img["format"]=="PNG") {
            
//PNG
            
imagePNG($this->img["des"]);
        } elseif (
$this->img["format"]=="GIF") {
            
//GIF
            
imageGIF($this->img["des"]);
        } elseif (
$this->img["format"]=="WBMP") {
            
//WBMP
            
imageWBMP($this->img["des"]);
        }
    }

    function 
save($save="")
    {
        
//save thumb
        
if (empty($save)) $save=strtolower("./thumb"$imgfile $this->img["format"]);
        
/* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
        
$this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
            @
imagecopyresized ($this->img["des"], $this->img["src"], 0000$this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

        if (
$this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
            
//JPEG
            
imageJPEG($this->img["des"],"$save",$this->img["quality"]);
        } elseif (
$this->img["format"]=="PNG") {
            
//PNG
            
imagePNG($this->img["des"],"$save");
        } elseif (
$this->img["format"]=="GIF") {
            
//GIF
            
imageGIF($this->img["des"],"$save");
        } elseif (
$this->img["format"]=="WBMP") {
            
//WBMP
            
imageWBMP($this->img["des"],"$save");
        }
    }
}
 
?>
  #3 (permalink)  
Antiguo 12/07/2004, 20:56
 
Fecha de Ingreso: septiembre-2003
Mensajes: 384
Antigüedad: 20 años, 7 meses
Puntos: 0
gracias, la verdad me podrias dar un ejemplo redimensionando el contenido de fotos de un folder a n tamaño que yo le especifique
  #4 (permalink)  
Antiguo 13/07/2004, 10:12
Avatar de xcorpion  
Fecha de Ingreso: octubre-2003
Ubicación: m é x i c o
Mensajes: 676
Antigüedad: 20 años, 6 meses
Puntos: 4
bien, supongamos que tienes un array con el listado de los archivos a redimencionar en una carpata y que el array se llama archivos, ademas tienes una subcarpeta que se llama thumbs y tiene los permisos adecuados, y tambien que el archivo que contiene la clase para redimencionar imagenes(el codigo que te puse arriba)esta en un archivo llamado thumbs.php en la misma carpeta seria algo como:

redimenciona.php (en la carpeta de imagenes a redimencionar,no thumbs)
Código PHP:
$archivos = array("imagen1.jpg","imgdos.jpg");
$i=0;
$calidad 90;    //calidad de la imagen de salida en porcentaje(%)
while($archivos[$i]){
    
$fileo $archivos[$i];            //archivo origen
    
$filed "thumbs/" $archivos[$i];        //archivo final
    
$thumb=new thumbnail($fileo);        // generate image_file, set filename to resize
    
$thumb->size_width($dim);            // set width for thumbnail, or
    //$thumb->size_height(30);            // set height for thumbnail, or
    //$thumb->size_auto($xauto);        // set the biggest width or height for thumbnail
    
$thumb->jpeg_quality($calidad);            // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
    
$thumb->save($filed);            // save your thumbnail to file
    
$i++;

ten en cuenta que algunos servidores no manejan imagenes gif, puedes probar si funciona en el tuyo y si no funciona omites las imagenes gif. Tambien puedes usar otros programas para redimencionar imagenes, es cuestion de buscar.
  #5 (permalink)  
Antiguo 01/08/2004, 22:00
 
Fecha de Ingreso: septiembre-2003
Mensajes: 384
Antigüedad: 20 años, 7 meses
Puntos: 0
bien lo he probado en distintos servidores y me da este error:

Fatal error: Call to undefined function: imagecreatefromjpeg() in /home/conecta2/public_html/fotos/fotos.php on line 39

a que se debe?
  #6 (permalink)  
Antiguo 02/08/2004, 22:36
 
Fecha de Ingreso: septiembre-2003
Mensajes: 384
Antigüedad: 20 años, 7 meses
Puntos: 0
Ayuda pls a que se debe ese error????

Fatal error: Call to undefined function: imagecreatefromjpeg() in /home/conecta2/public_html/fotos/fotos.php on line 39
  #7 (permalink)  
Antiguo 03/08/2004, 00:01
Avatar de xcorpion  
Fecha de Ingreso: octubre-2003
Ubicación: m é x i c o
Mensajes: 676
Antigüedad: 20 años, 6 meses
Puntos: 4
a sorry, mira puede ser que te falte cargar la libreria GD. checa que este bien configurado el php.ini para que la cargue. ademas tienes que checar tambien que el path de las librerias sea el correcto.
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 22:48.