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<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/jquery.lightbox-0.5.css" rel="stylesheet" type="text/css" /> <script src="scripts/jquery-1.3.2.js" language="javascript"></script> <script src="scripts/jquery.lightbox-0.5.js" language="javascript"></script> <?php
include_once('gallery.php');
$mygallery = new gallery();
$mygallery->loadFolder('images/');
$mygallery->show(500, 100, true);
?>
thumb.php:
Código PHP:
Ver original<?php
class thumb {
var $image;
var $type;
var $width;
var $height;
//---Método de leer la imagen
function loadImage($name) {
//---Tomar las dimensiones de la imagen
$this->width = $info[0];
$this->height = $info[1];
$this->type = $info[2];
//---Dependiendo del tipo de imagen crear una nueva imagen
switch($this->type){
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_GIF:
break;
case IMAGETYPE_PNG:
break;
}
}
//---Método de guardar la imagen
function save($name, $quality = 100) {
//---Guardar la imagen en el tipo de archivo correcto
switch($this->type){
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_GIF:
break;
case IMAGETYPE_PNG:
$pngquality = floor(($quality - 10) / 10); imagepng($this->image, $name, $pngquality); break;
}
}
//---Método de mostrar la imagen sin salvarla
function show() {
//---Mostrar la imagen dependiendo del tipo de archivo
switch($this->type){
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_GIF:
break;
case IMAGETYPE_PNG:
break;
}
}
//---Método de redimensionar la imagen sin deformarla
function resize($value, $prop){
//---Determinar la propiedad a redimensionar y la propiedad opuesta
$prop_value = ($prop == 'width') ? $this->width : $this->height;
$prop_versus = ($prop == 'width') ? $this->height : $this->width;
//---Determinar el valor opuesto a la propiedad a redimensionar
$pcent = $value / $prop_value;
$value_versus = $prop_versus * $pcent;
//---Crear la imagen dependiendo de la propiedad a variar
//---Hacer una copia de la imagen dependiendo de la propiedad a variar
switch($prop){
case 'width':
imagecopyresampled($image, $this->image, 0, 0, 0, 0, $value, $value_versus, $this->width, $this->height); break;
case 'height':
imagecopyresampled($image, $this->image, 0, 0, 0, 0, $value_versus, $value, $this->width, $this->height); break;
}
//---Actualizar la imagen y sus dimensiones
$this->image = $image;
}
//---Método de extraer una sección de la imagen sin deformarla
function crop($cwidth, $cheight, $pos = 'center') {
//---Hallar los valores a redimensionar
$new_w = $cwidth;
$new_h = ($cwidth / $this->width) * $this->height;
//---Si la altura es menor recalcular por la altura
if($new_h < $cheight){
$new_h = $cheight;
$new_w = ($cheight / $this->height) * $this->width;
}
$this->resize($new_w, 'width');
//---Crear la imagen tomando la porción del centro de la imagen redimensionada con las dimensiones deseadas
switch($pos){
case 'center':
imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight); break;
case 'left':
imagecopyresampled($image, $this->image, 0, 0, 0, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight); break;
case 'right':
imagecopyresampled($image, $this->image, 0, 0, $this->width - $cwidth, abs(($this->height - $cheight) / 2), $cwidth, $cheight, $cwidth, $cheight); break;
case 'top':
imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), 0, $cwidth, $cheight, $cwidth, $cheight); break;
case 'bottom':
imagecopyresampled($image, $this->image, 0, 0, abs(($this->width - $cwidth) / 2), $this->height - $cheight, $cwidth, $cheight, $cwidth, $cheight); break;
}
$this->image = $image;
}
}
?>
show_thumb.php:
Código PHP:
Ver original<?php
header("Content-type: image/jpeg"); $src = $_GET['src'];
$width = $_GET['width'];
$height = $_GET['height'];
include_once('thumb.php');
$image = new thumb();
$image->loadImage($src);
$image->crop($width, $height);
$image->show();
?>
gallery.php:
Código PHP:
Ver original<?php
class gallery {
var $path;
//---Método de leer una carpeta con imágenes
function loadFolder($path){
$this->path = $path;
//---Guardar en un arreglo todos los archivos en el directorio
//---Si no es un directorio
//---Ir guardando los nombres en un arreglo
$this->files[] = $fil;
}
}
}
//---Cerrar el directorio
//---Ordenar alfabeticamente el arreglo (inverso)
}
//---Método de mostrar todas las imágenes contenidas en la carpeta
function show($area = 500, $width = 100, $shownames = false){
//---Calcular la cantidad de imágenes en un tramo de ancho
$cant = floor(($area + 5) / ($width + 5));
//---Calcular un nuevo espacio para las imágenes
$space = floor(($area - $width * $cant) / ($cant - 1));
//---Crear la galería con los nombres de todos los archivos
$total = count($this->files); $cont = 0;
echo '<div style="width:'.$area.'px">';
//---Situar los thumbnails
for($i = 0; $i < $total; $i++){
//---Determinar si se trata de la última imagen de la fila o no
$margin = (($i + 1) % $cant == 0) ? 0 : $space;
if($shownames){
echo '<div style="width:'.$width.'px; float:left; margin-right:'.$margin.'px; margin-bottom:'.$space.'px;">';
echo '<a href="'.$this->path.'/'.$this->files[$i].'" rel="lightbox[galeria1]" title="'.$this->getName($this->files[$i]).'">';
echo '<img src="show_thumb.php?src='.$this->path.'/'.$this->files[$i].'&width='.$width.'&height='.$width.'" width="'.$width.'" height="'.$width.'" border="0"></img>';
echo '</a>';
echo '</div>';
}else{
echo '<div style="width:'.$width.'px; float:left; margin-right:'.$margin.'px; margin-bottom:'.$space.'px;">';
echo '<a href="'.$this->path.'/'.$this->files[$i].'" rel="lightbox[galeria1]">';
echo '<img src="show_thumb.php?src='.$this->path.'/'.$this->files[$i].'&width='.$width.'&height='.$width.'" width="'.$width.'" height="'.$width.'" border="0"></img>';
echo '</a>';
echo '</div>';
}
}
?>
<script language="javascript">
$(document).ready(function(){
$("a[rel = 'lightbox[galeria1]']").lightBox();
});
</script>
<?php
echo '</div>';
}
//---Función de convertir el nombre de archivo a un nombre descriptivo
function getName($name){
$reg = array('/\[\d*\]/', '/_/', '/\.+jpg|gif|png+$/', '/@A@/', '/@E@/', '/@I@/', '/@O@/', '/@U@/', '/@N@/', '/@a@/', '/@e@/', '/@i@/', '/@o@/', '/@u@/', '/@n@/'); $out = array('', ' ', '', 'Á', 'É', 'Í', 'Ó', 'Ú', 'Ñ', 'á', 'é', 'í', 'ó', 'ú', 'ñ');
return $ret;
}
}
?>
Bueno, eso es todo. Espero que me puedan ayudar. Saludos.