Foros del Web » Programando para Internet » PHP »

Galería de Imágenes PHP

Estas en el tema de Galería de Imágenes PHP en el foro de PHP en Foros del Web. Hola tengo este código de una galería de imágenes con php y ligthbox que anda muy bien localmente y en la red, pero mi problema ...
  #1 (permalink)  
Antiguo 24/05/2011, 15:54
 
Fecha de Ingreso: febrero-2011
Mensajes: 12
Antigüedad: 13 años, 2 meses
Puntos: 0
Galería de Imágenes PHP

Hola tengo este código de una galería de imágenes con php y ligthbox que anda muy bien localmente y en la red, pero mi problema es que no sé cómo hacer que los thumbails tengan un tamaño porcentual de las imágenes del directorio especificado:

index.php:
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <title>Ejemplo Galería PHP</title>
  5. <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
  6. <script src="scripts/jquery-1.3.2.js" language="javascript"></script>
  7. <script src="scripts/jquery.lightbox-0.5.js" language="javascript"></script>
  8. </head>
  9.   <div align="center">
  10.     <?php
  11.      include_once('gallery.php');
  12.            
  13.      $mygallery = new gallery();
  14.      $mygallery->loadFolder('images/');
  15.       $mygallery->show(500, 100, true);
  16.     ?>
  17.   </div>
  18. </body>
  19. </html>

thumb.php:
Código PHP:
Ver original
  1. <?php
  2. class thumb {
  3.    
  4.    var $image;
  5.    var $type;
  6.    var $width;
  7.    var $height;
  8.    
  9.    //---Método de leer la imagen
  10.    function loadImage($name) {
  11.        
  12.       //---Tomar las dimensiones de la imagen
  13.       $info = getimagesize($name);
  14.        
  15.       $this->width = $info[0];
  16.       $this->height = $info[1];
  17.       $this->type = $info[2];    
  18.        
  19.       //---Dependiendo del tipo de imagen crear una nueva imagen
  20.       switch($this->type){        
  21.          case IMAGETYPE_JPEG:
  22.             $this->image = imagecreatefromjpeg($name);
  23.          break;        
  24.          case IMAGETYPE_GIF:
  25.             $this->image = imagecreatefromgif($name);
  26.          break;        
  27.          case IMAGETYPE_PNG:
  28.             $this->image = imagecreatefrompng($name);
  29.          break;        
  30.       }      
  31.    }
  32.    
  33.    //---Método de guardar la imagen
  34.    function save($name, $quality = 100) {
  35.        
  36.       //---Guardar la imagen en el tipo de archivo correcto
  37.       switch($this->type){        
  38.          case IMAGETYPE_JPEG:
  39.             imagejpeg($this->image, $name, $quality);
  40.          break;        
  41.          case IMAGETYPE_GIF:
  42.              imagegif($this->image, $name);
  43.          break;        
  44.          case IMAGETYPE_PNG:
  45.             $pngquality = floor(($quality - 10) / 10);
  46.             imagepng($this->image, $name, $pngquality);
  47.          break;        
  48.       }
  49.       imagedestroy($this->image);
  50.    }
  51.    
  52.    //---Método de mostrar la imagen sin salvarla
  53.    function show() {
  54.        
  55.       //---Mostrar la imagen dependiendo del tipo de archivo
  56.       switch($this->type){        
  57.          case IMAGETYPE_JPEG:
  58.             imagejpeg($this->image);
  59.          break;        
  60.          case IMAGETYPE_GIF:
  61.             imagegif($this->image);
  62.          break;        
  63.          case IMAGETYPE_PNG:
  64.             imagepng($this->image);
  65.          break;
  66.       }
  67.       imagedestroy($this->image);
  68.    }
  69.    
  70.    //---Método de redimensionar la imagen sin deformarla
  71.    function resize($value, $prop){
  72.        
  73.       //---Determinar la propiedad a redimensionar y la propiedad opuesta
  74.       $prop_value = ($prop == 'width') ? $this->width : $this->height;
  75.       $prop_versus = ($prop == 'width') ? $this->height : $this->width;
  76.        
  77.       //---Determinar el valor opuesto a la propiedad a redimensionar
  78.       $pcent = $value / $prop_value;      
  79.       $value_versus = $prop_versus * $pcent;
  80.        
  81.       //---Crear la imagen dependiendo de la propiedad a variar
  82.       $image = ($prop == 'width') ? imagecreatetruecolor($value, $value_versus) : imagecreatetruecolor($value_versus, $value);
  83.        
  84.       //---Hacer una copia de la imagen dependiendo de la propiedad a variar
  85.       switch($prop){
  86.          
  87.          case 'width':
  88.             imagecopyresampled($image, $this->image, 0, 0, 0, 0, $value, $value_versus, $this->width, $this->height);
  89.          break;
  90.          
  91.          case 'height':
  92.             imagecopyresampled($image, $this->image, 0, 0, 0, 0, $value_versus, $value, $this->width, $this->height);
  93.          break;
  94.          
  95.       }
  96.        
  97.       //---Actualizar la imagen y sus dimensiones      
  98.       $this->width = imagesx($image);
  99.       $this->height = imagesy($image);
  100.       $this->image = $image;
  101.        
  102.    }    
  103.    
  104.    //---Método de extraer una sección de la imagen sin deformarla
  105.    function crop($cwidth, $cheight, $pos = 'center') {
  106.        
  107.        //---Hallar los valores a redimensionar
  108.        $new_w = $cwidth;
  109.        $new_h = ($cwidth / $this->width) * $this->height;
  110.        
  111.        //---Si la altura es menor recalcular por la altura
  112.        if($new_h < $cheight){
  113.            
  114.            $new_h = $cheight;
  115.            $new_w = ($cheight / $this->height) * $this->width;
  116.        
  117.        }
  118.        
  119.        $this->resize($new_w, 'width');
  120.        
  121.       //---Crear la imagen tomando la porción del centro de la imagen redimensionada con las dimensiones deseadas
  122.       $image = imagecreatetruecolor($cwidth, $cheight);
  123.        
  124.       switch($pos){
  125.          
  126.          case 'center':
  127.             imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight);
  128.          break;
  129.          
  130.          case 'left':
  131.             imagecopyresampled($image, $this->image, 0, 0, 0, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight);
  132.          break;
  133.          
  134.          case 'right':
  135.             imagecopyresampled($image, $this->image, 0, 0, $this->width - $cwidth, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight);
  136.          break;
  137.          
  138.          case 'top':
  139.             imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), 0, $cwidth, $cheight, $cwidth, $cheight);
  140.          break;
  141.          
  142.          case 'bottom':
  143.             imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), $this->height - $cheight, $cwidth, $cheight, $cwidth, $cheight);
  144.          break;
  145.        
  146.       }
  147.        
  148.       $this->image = $image;
  149.    }
  150.    
  151. }
  152. ?>

show_thumb.php:
Código PHP:
Ver original
  1. <?php
  2.   header("Content-type: image/jpeg");
  3.   $src = $_GET['src'];
  4.   $width = $_GET['width'];
  5.   $height = $_GET['height'];
  6.   include_once('thumb.php');
  7.  
  8.   $image = new thumb();
  9.   $image->loadImage($src);
  10.   $image->crop($width, $height);
  11.   $image->show();
  12. ?>

gallery.php:
Código PHP:
Ver original
  1. <?php
  2. class gallery {
  3.    
  4.   var $files = array();
  5.   var $path;
  6.    
  7.   //---Método de leer una carpeta con imágenes
  8.   function loadFolder($path){
  9.    
  10.     $this->path = $path;
  11.        
  12.     //---Guardar en un arreglo todos los archivos en el directorio 
  13.     $folder = opendir($this->path);
  14.            
  15.     while ($fil = readdir($folder)) {  
  16.       //---Si no es un directorio
  17.       if(!is_dir($fil)){   
  18.         $arr = explode('.', $fil);         
  19.         if(count($arr) > 1){               
  20.           //---Ir guardando los nombres en un arreglo
  21.           $this->files[] = $fil;
  22.         }
  23.       }
  24.     }      
  25.     //---Cerrar el directorio
  26.     closedir($folder);
  27.        
  28.     //---Ordenar alfabeticamente el arreglo (inverso)
  29.     rsort($this->files);
  30.   }
  31.    
  32.   //---Método de mostrar todas las imágenes contenidas en la carpeta
  33.   function show($area = 500, $width = 100, $shownames = false){
  34.        
  35.   //---Calcular la cantidad de imágenes en un tramo de ancho
  36.   $cant = floor(($area + 5) / ($width + 5));       
  37.        
  38.   //---Calcular un nuevo espacio para las imágenes
  39.   $space = floor(($area - $width * $cant) / ($cant - 1));
  40.    
  41.   //---Crear la galería con los nombres de todos los archivos
  42.   $total = count($this->files);
  43.   $cont = 0;
  44.        
  45.   echo '<div style="width:'.$area.'px">';
  46.        
  47.   //---Situar los thumbnails
  48.   for($i = 0; $i < $total; $i++){      
  49.     //---Determinar si se trata de la última imagen de la fila o no
  50.     $margin = (($i + 1) % $cant == 0) ? 0 : $space;
  51.       if($shownames){
  52.         echo '<div style="width:'.$width.'px; float:left; margin-right:'.$margin.'px; margin-bottom:'.$space.'px;">';
  53.         echo '<a href="'.$this->path.'/'.$this->files[$i].'" rel="lightbox[galeria1]" title="'.$this->getName($this->files[$i]).'">';
  54.         echo '<img src="show_thumb.php?src='.$this->path.'/'.$this->files[$i].'&width='.$width.'&height='.$width.'" width="'.$width.'" height="'.$width.'" border="0"></img>';
  55.         echo '</a>';
  56.         echo '</div>';         
  57.       }else{
  58.         echo '<div style="width:'.$width.'px; float:left; margin-right:'.$margin.'px; margin-bottom:'.$space.'px;">';
  59.         echo '<a href="'.$this->path.'/'.$this->files[$i].'" rel="lightbox[galeria1]">';
  60.         echo '<img src="show_thumb.php?src='.$this->path.'/'.$this->files[$i].'&width='.$width.'&height='.$width.'" width="'.$width.'" height="'.$width.'" border="0"></img>';
  61.         echo '</a>';
  62.         echo '</div>'; 
  63.       }
  64.   }
  65. ?>
  66. <script language="javascript">
  67.   $(document).ready(function(){
  68.     $("a[rel = 'lightbox[galeria1]']").lightBox();
  69.   });
  70. </script>  
  71.            
  72. <?php
  73.   echo '</div>';
  74. }
  75.    
  76.   //---Función de convertir el nombre de archivo a un nombre descriptivo
  77.   function getName($name){
  78.    
  79.     $reg = array('/\[\d*\]/', '/_/', '/\.+jpg|gif|png+$/', '/@A@/', '/@E@/', '/@I@/', '/@O@/', '/@U@/', '/@N@/', '/@a@/', '/@e@/', '/@i@/', '/@o@/', '/@u@/', '/@n@/');
  80.     $out = array('', ' ', '', '&Aacute;', '&Eacute;', '&Iacute;', '&Oacute;', '&Uacute;', '&Ntilde;', '&aacute;', '&eacute;', '&iacute;', '&oacute;', '&uacute;', '&ntilde;');
  81.     $ret = preg_replace($reg, $out, $name);
  82.        
  83.     return $ret;
  84.   }
  85. }
  86. ?>
Bueno, eso es todo. Espero que me puedan ayudar. Saludos.
  #2 (permalink)  
Antiguo 10/10/2011, 13:43
 
Fecha de Ingreso: marzo-2011
Ubicación: Córdoba
Mensajes: 11
Antigüedad: 13 años, 1 mes
Puntos: 0
Respuesta: Galería de Imágenes PHP

Hola lea como va? mira justamente estoy con el mismo caso jaja los mismos archivos y el mismo ejemplo pero a mi ni siquiera me anda al subirlo a la web no entiendo porque. Si lo pruebo del servidor local en mi compu anda bien pero cuando lo subo los tumbs o imágenes chiquitas no me las reconoce :( estoy perdido ja. Si podes adjuntar un archivo rar con el ejemplo tuyo que funciona taria bueno jeje. O bien si me das una mano que puede andar mal. Un saludo!

Etiquetas: Ninguno
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 20:38.