Foros del Web » Programando para Internet » PHP »

Clase en PHP para galería dinámica

Estas en el tema de Clase en PHP para galería dinámica en el foro de PHP en Foros del Web. Buenas noches, he indagado mucho en internet sobre cómo hacer galerías que carguen las imágenes de acuerdo a una carpeta. Encontré en algún antiguo foro ...
  #1 (permalink)  
Antiguo 16/04/2015, 19:52
Avatar de paula23andrea  
Fecha de Ingreso: noviembre-2012
Mensajes: 38
Antigüedad: 11 años, 5 meses
Puntos: 1
Exclamación Clase en PHP para galería dinámica

Buenas noches, he indagado mucho en internet sobre cómo hacer galerías que carguen las imágenes de acuerdo a una carpeta. Encontré en algún antiguo foro un código muy interesante, que permite la creación de thumbnail para hacer funcionar la galería.
Resulta que funciona, pero las vistas previas en miniatura no se ven... es decir, cargan los cuadritos con icono roto, aunque al dar click en ellos, se abre la imagen correctamente.
He revisado, y parece estar todo bien, pero tengo nada de experiencia en clases de php.
Comparto el código por si algún experto puede encontrar la falla.



Código de Galeria.php

Código PHP:
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. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>PHP Gallery Example</title>
  6.  
  7. <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" />
  8.  
  9. <script src="scripts/jquery-1.3.2.js" language="javascript"></script>
  10. <script src="scripts/jquery.lightbox-0.5.js" language="javascript"></script>
  11.  
  12. </head>
  13.  
  14. <body>
  15.  
  16.     <div align="center">
  17.    
  18.         <?php
  19.            
  20.             include_once('gallery.php');
  21.            
  22.             $mygallery = new gallery();
  23.             $mygallery->loadFolder('galley_images');
  24.             $mygallery->show(500, 100, 10);
  25.        
  26.         ?>
  27.    
  28.     </div>
  29.  
  30. </body>
  31. </html>

Código de gallery.php donde se llama a show_thumb.php que es el que no carga los cuadritos :/

Código PHP:
Ver original
  1. <?php
  2. class gallery {
  3.    
  4.     var $files = array();
  5.     var $path;
  6.    
  7.     function loadFolder($path){
  8.        
  9.         $this->path = $path;
  10.        
  11.         //---Guardar en un arreglo todos los archivos en el directorio 
  12.         $folder = opendir($this->path);
  13.            
  14.         while ($fil = readdir($folder)) {
  15.            
  16.             //---Si no es un directorio
  17.             if(!is_dir($fil)){
  18.                
  19.                 $arr = explode('.', $fil);
  20.                
  21.                 if(count($arr) > 1){
  22.                    
  23.                     //---Ir guardando los nombres en un arreglo
  24.                     $this->files[] = $fil;
  25.                    
  26.                 }
  27.                
  28.             }
  29.            
  30.         }
  31.        
  32.         //---Cerrar el directorio
  33.         closedir($folder);
  34.        
  35.         //---Ordenar alfabeticamente el arreglo
  36.         sort($this->files);
  37.    
  38.     }
  39.    
  40.     function show($area = 500, $width = 100, $space = 10){
  41.    
  42.         //---Crear la galería con los nombres de todos los archivos
  43.         $total = count($this->files);
  44.         $cont = 0;
  45.        
  46.         echo '<div name="xx" style="width:'.$area.'px">';
  47.        
  48.             //---Situar los thumbnails
  49.             for($i = 0; $i < $total; $i++){    
  50.                
  51.                 echo '<div style="width:'.$width.'px; float:left; padding-right:'.$space.'px; padding-bottom:'.$space.'px;"><a href="'.$this->path.'/'.$this->files[$i].'" rel="lightbox"><img src="show_thumb.php?src='.$this->path.'/'.$this->files[$i].'&width='.$width.'" width="'.$width.'" height="'.$width.'" border="0"></img></a></div>';
  52.                
  53.             }
  54.            
  55.             ?>
  56.        
  57.             <script language="javascript">
  58.    
  59.                 $(document).ready(function(){
  60.                    
  61.                     $("a[rel = 'lightbox']").lightBox();                       
  62.                                            
  63.                 });
  64.    
  65.             </script>
  66.        
  67.             <?php
  68.        
  69.         echo '</div>';
  70.    
  71.     }
  72.  
  73. }
  74. ?>

Código de thumb.php donde se maneja la imagen para redimensionarla.

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($name, $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 == 'height') ? $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.       $info = getimagesize($name);
  99.        
  100.       $this->width = imagesx($image);
  101.       $this->height = imagesy($image);
  102.       $this->image = $image;
  103.        
  104.    }    
  105.    
  106.    //---Método de extraer una sección de la imagen sin deformarla
  107.    function crop($name, $cwidth, $cheight, $pos = 'center') {
  108.        
  109.       //---Hallar los valores a redimensionar
  110.       $new_w = $cwidth;
  111.       $new_h = ($cwidth / $this->width) * $this->height;
  112.      
  113.       //---Si la altura es menor recalcular por la altura
  114.       if($new_h < $cheight){
  115.          
  116.          $new_h = $cheight;
  117.          $new_w = ($cheight / $this->height) * $this->width;
  118.      
  119.       }
  120.      
  121.       $this->resize($new_w, 'width');
  122.        
  123.       //---Crear la imagen tomando la porción del centro de la imagen redimensionada con las dimensiones deseadas
  124.       $image = imagecreatetruecolor($cwidth, $cheight);
  125.        
  126.       switch($pos){
  127.          
  128.          case 'center':
  129.             imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight);
  130.          break;
  131.          
  132.          case 'left':
  133.             imagecopyresampled($image, $this->image, 0, 0, 0, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight);
  134.          break;
  135.          
  136.          case 'right':
  137.             imagecopyresampled($image, $this->image, 0, 0, $this->width - $cwidth, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight);
  138.          break;
  139.          
  140.          case 'top':
  141.             imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), 0, $cwidth, $cheight, $cwidth, $cheight);
  142.          break;
  143.          
  144.          case 'bottom':
  145.             imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), $this->height - $cheight, $cwidth, $cheight, $cwidth, $cheight);
  146.          break;
  147.        
  148.       }
  149.        
  150.       $this->image = $image;
  151.    }
  152.    
  153. }
  154. ?>

Y código de show_thumb.php que es la que debería mostrar las miniaturas bien.
Código PHP:
Ver original
  1. <?php
  2.  
  3.     $src = $_GET['src'];
  4.     $width = $_GET['width'];
  5.     include_once('thumb.php');
  6.  
  7.     $image = new thumb();
  8.     $image->loadImage($src);
  9.     $image->crop($src, $width, $width);
  10.     $image->show();
  11.  
  12. ?>

Gracias por su tiempo, espero alguien pueda darme respuestas :(
  #2 (permalink)  
Antiguo 16/04/2015, 20:07
Avatar de ArturoGallegos
Moderador
 
Fecha de Ingreso: febrero-2008
Ubicación: Morelia, México
Mensajes: 6.774
Antigüedad: 16 años, 2 meses
Puntos: 1146
Respuesta: Clase en PHP para galería dinámica

revisa en el html que url se esta imprimiendo si es correcta revisa los permisos en el servidor.

si es incorrecta revisa el codigo que te devuelve la url y corrigelo....

El que tengas poco o nada de conocimiento no es motivo para nosotros te hagamos el trabajo.

Revisa el código, paso por paso.... te recomiendo leer sobre OOP

Revisa que contiene esta linea
$this->files[] = $fil;

y revisa la funcion show

Etiquetas: clase, dinamica, galeria, thumb
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 19:28.