Tema: FAQ's de PHP
Ver Mensaje Individual
  #73 (permalink)  
Antiguo 23/07/2003, 23:30
Avatar de MSDark
MSDark
 
Fecha de Ingreso: noviembre-2002
Ubicación: Talca VII región
Mensajes: 337
Antigüedad: 21 años, 5 meses
Puntos: 1
Thumbnails

Tema: Imagenes
Pregunta: Como creo thumbnails de mis imagenes?
Respuesta Con las funciones de imagen de la libreria GD en esta simple funcion

Código PHP:
function thumbnail($file,$dir,$ancho=100,$alto=100,$gif_support=false)
{
    if(!
file_exists($file)){
        return 
false;
    }
    if(!
is_dir($dir)){
        return 
false;
    }
    
$final = (int) strlen($file) - 4;
    
$nombre strtolower(substr($file,0,$final));
    
$ext strtolower(substr($file,-4,4));
    
$nombre $nombre.$ext;
    
$mini $dir."/mini_".$nombre;
    if(
$ext==".jpg" || $ext==".jpeg"){
        
$origen  imageCreateFromJPEG($file);
        
$imgAnchoimageSx($origen);
        
$imgAlto imageSy($origen);
        
$imagen  imageCreate($ancho,$alto);
        
ImageCopyResized($imagen,$origen,0,0,0,0,$ancho,$alto,$imgAncho,$imgAlto);
        
imageJPEG($imagen,$mini);
    }else if(
$ext==".png"){
        
$origen  imageCreateFromPNG($file);
        
$imgAnchoimageSx($origen);
        
$imgAlto imageSy($origen);
        
$imagen  imageCreate($ancho,$alto);
        
ImageCopyResized($imagen,$origen,0,0,0,0,$ancho,$alto,$imgAncho,$imgAlto);
        
imagePNG($imagen,$mini);
    }else if((
$gif_support==true) && ($ext==".gif")){
         
$origen  imageCreateFromGIF($file);
        
$imgAnchoimageSx($origen);
        
$imgAlto imageSy($origen);
        
$imagen  imageCreate($ancho,$alto);
        
ImageCopyResized($imagen,$origen,0,0,0,0,$ancho,$alto,$imgAncho,$imgAlto);
        
imageGIF($imagen,$mini);
    }else{
        return 
false;
    }
    return 
true;

Como usarla?

Sencillo, le pasas a la funcion el nombre del archivo y el directorio en que quieres que queden creadas las thumbnails.
En forma de opcion esta que le pases el alto y ancho que por defecto es 100 de ancho y de alto.

Tambien si tu libreria GD soporta gifs le pasas true a la opcion de gif.
Código PHP:
thumbnail$file $dir , [$ancho 100] , [$alto 100] , [$gif_support=false] ); 

Ejemplo de uso

Código PHP:
if(!thumbnail("imagen.png","./thumbnails",80,80)){
    echo 
"error en creacion de thumbnails";
}else{
    echo 
"Creacion exitosa";

Recordar que esta funcion crea los thumbnails en un directorio especificado.

Saludos
__________________
DelirioLinux.. Software Libre y tecnología.. php también..
Saludos :arriba:
Usuario Linux #198486 y Ubuntu #12791
Gutsy Gibbon