Requiere GD 2.0.1 or superior.
Para probarlo localmente pone:
http://localhost/portfolio/index.php?dir=misfotos
Espero que te sirva
Se puede mejorar agregándole paginación.
Código PHP:
<html>
<head>
<title>Portfolio</title>
</head>
<body>
<?php
require 'DirectoryItems.php';
$directory = $_GET["dir"];
$di =& new DirectoryItems($directory);
$di->imagesOnly();
$di->naturalCaseInsensitiveOrder();
echo "<div style=\"text-align:center;\">";
echo "Haga click sobre el título para ver la imagen en tamaño completo.<br />";
$filearray = $di->getFileArray();
$path = "";
$size = 100; // Tamaño thumbnail
foreach ($filearray as $key => $value){
$path = "$directory/".$key;
echo "<img src=\"getthumb.php?path=$path&size=$size\" ".
"style=\"border:1px solid black;margin-top:20px;\" ".
"alt= \"$value\" /><br />\n";
echo "<a href=\"$path\" target=\"_blank\" >";
echo "Título: $value</a> <br />\n";
}
echo "</div><br />";
?>
</body>
</html>
Código PHP:
<?php
class DirectoryItems{
var $filearray = array();
var $replacechar;
var $directory;
function DirectoryItems($directory, $replacechar = "_"){
$this->directory = $directory;
$this->replacechar = $replacechar;
$d = "";
if(is_dir($directory)){
$d = opendir($directory) or die("Falló al abrir directorio.");
while(false !== ($f = readdir($d))){
if(is_file("$directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}else{
die("Debe pasar un directorio.");
}
}
function __destruct(){
unset($this->filearray);
}
function getDirectoryName(){
return $this->directory;
}
function indexOrder(){
sort($this->filearray);
}
function naturalCaseInsensitiveOrder(){
natcasesort($this->filearray);
}
function checkAllImages(){
$bln = true;
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".")+1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
$bln = false;
break;
}
}
return $bln;
}
function checkAllSpecificType($extension){
$extension = strtolower($extension);
$bln = true;
$ext = "";
foreach ($this->filearray as $key => $value){
$ext = substr($key,(strpos($key, ".")+1));
$ext = strtolower($ext);
if($extension != $ext){
$bln=false;
break;
}
}
return $bln;
}
function getCount(){
return count($this->filearray);
}
function getFileArray(){
return $this->filearray;
}
function getFileArraySlice($start, $length){
return array_slice($this->filearray, $start, $length);
}
function filter($extension){
$extension = strtolower($extension);
foreach ($this->filearray as $key => $value){
$ext = substr($key,(strpos($key, ".") + 1));
$ext = strtolower($ext);
if($ext != $extension){
unset($this->filearray[$key]);
}
}
}
function imagesOnly(){
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".") + 1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
unset($this->filearray[$key]);
}
}
}
function removeFilter(){
unset($this->filearray);
$d = "";
$d = opendir($this->directory) or die("Falló al abrir directorio.");
while(false!==($f=readdir($d))){
if(is_file("$this->directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}
function createTitle($title){
$title = substr($title,0,strrpos($title, "."));
$title = str_replace($this->replacechar, " ", $title);
return $title;
}
}
?>
Código PHP:
<?php
class ThumbnailImage{
var $image;
var $maxdimension;
var $quality=100;
var $fileextension;
var $mimetype;
var $imageproperties;
var $types= array("jpg", "jpeg", "gif", "png");
function ThumbnailImage($path, $maxdimension=100){
$this->maxdimension=$maxdimension;
is_file($path) or die ("Archivo: $path no existe.");
$extension=substr($path,(strpos($path, ".")+1));
$extension= strtolower($extension);
in_array($extension, $this->types) or die ("Tipo de archivo incorrector.");
$this->fileextension=$extension;
$this->setMimeType($extension);
$this->imageproperties = GetImageSize($path);
if($extension=="jpeg" || $extension=="jpg"){
$this->image=imagecreatefromJPEG($path);
}elseif($extension=="gif"){
$this->image=imagecreatefromGIF($path);
}elseif($extension=="png"){
$this->image=imagecreatefromPNG($path);
}else{
die("No se pudo crear la images.");
}
$this->createThumb();
}
function getImage(){
header("Content-type: $this->mimetype");
if($this->fileextension=="jpeg" || $this->fileextension=="jpg"){
imagejpeg($this->image,"",$this->quality);
}elseif($this->fileextension=="gif"){
$image=imagegif($this->image);
}elseif($this->fileextension=="png"){
$image=imagepng($this->image);
}else{
die("No se pudo crear la imagen.");
}
}
function getMimeType(){
return $this->mimetype;
}
function getQuality(){
if($this->fileextension=="jpeg" || $this->fileextension=="jpg"){
$quality=$this->quality;
}else{
$quality=-1;
}
return quality;
}
function setQuality($quality){
if($quality > 100 || $quality < 1)
$quality=75;
if($this->mimetype=="image/jpeg"){
$this->quality=$quality;
}
}
function destroy(){
imagedestroy($this->image);
}
function setMimeType($extension){
if($extension=="jpeg" || $extension=="jpg"){
$this->mimetype="image/jpeg";
}elseif($extension=="png"){
$this->mimetype="image/png";
}elseif($extension=="gif"){
$this->mimetype="image/gif";
}else{
die ("Tipo no reconocido.");
}
}
function createThumb(){
$srcW=$this->imageproperties[0];
$srcH=$this->imageproperties[1];
if($srcW>$this->maxdimension || $srcH>$this->maxdimension){
$reduction=$this->calculateReduction($srcW,$srcH);
$desW=round($srcW/$reduction);
$desH=round($srcH/$reduction);
if($this->mimetype=="image/gif"){
$copy=imagecreate($desW, $desH);
}else{
$copy=imagecreatetruecolor($desW, $desH);
}
imagecopyresampled($copy,$this->image,0,0,0,0,$desW, $desH, $srcW, $srcH)
or die ("Falló al copiar imagen.");
imagedestroy($this->image);
$this->image=$copy;
}
}
function calculateReduction($srcW, $srcH){
if($srcW<$srcH){
$reduction=$srcH/$this->maxdimension;
}else{
$reduction=$srcW/$this->maxdimension;
}
return $reduction;
}
}
?>
Código PHP:
<?php
require 'ThumbnailImage.php';
$path = @$_GET["path"];
$maxsize = @$_GET["size"];
if(!isset($maxsize)){
$maxsize=100;
}
if(isset($path)){
$thumb =& new ThumbNailImage($path, 100);
$thumb->getImage();
}
?>