Hi.
To display a gallery of picutres on my page , i use this :
<?
// largeur
$sizex = 110;
// hauteur
$sizey = 90;
// nombre d'images par lignes
$nbx = 4;
// préfixe pour les petites images
$prefix = "_s_";
// couleur de fond des images
// composante rouge
$bgred = 0;
// composante verte
$bggreen = 51;
// composante bleue
$bgblue = 102;
// fichier contenant l'image par défaut pour ce qui n'est pas géré
$unknown = "unknown.png";
////////////////////////////////
// NE RIEN CHANGER CI-DESSOUS //
////////////////////////////////
@set_time_limit (30); // change timeout value for script. no effect if running in safe_mode (see php.ini)
if (@$_GET['infos']) {
phpinfo();
}
if (@$_GET['dir']) {
$thedir = @$_GET['dir'];
} else {
$thedir = "gallery";
}
// Remplacer les "<" par "<" (et tous les caractères comme ça)
$thedir = preg_replace ("/&#(\d+)/e", "chr(\\1)", $thedir);
// Effacer tous les "<" pour supprimer les tags HTML et le cross site scripting
$thedir = preg_replace ("/</", "", $thedir);
if (preg_match ("/\.\./", $thedir)) {
user_error (".. est interdit dans le nom des fichiers<P>");
exit;
}
// Vérification de la présence du répertoire d'images
if (!is_dir ($thedir)) {
user_error ("<P>Album \"<I>$thedir</I>\" cannot be accessed!<P>");
exit;
// If &refresh is added, delete all thumbnails
if (@$_GET['refresh']) {
$dir = opendir ($thedir);
while ($file = readdir($dir)) {
if (preg_match ("/^$prefix/", $file)) {
unlink ($thedir."/".$file);
}
}
echo "Refreshed!<br/>";
}
// returns the extension that will be used to output thumbnails
function get_imagetype () {
$ext = "";
if (imagetypes() & IMG_JPG)
$ext = "jpg";
else if (imagetypes() & IMG_PNG)
$ext = "png";
else if (imagetypes() & IMG_GIF)
$ext = "gif";
else {
echo "No compatible image type supported. This script cannot function on this web server.";
exit;
}
return $ext;
}
// return the thumbnail name of the filename passed in parameter
function get_thumbnail_name ($dir, $file) {
global $prefix;
$ext = get_imagetype(); // if no compatible type, will stop the script.
$small = "$dir/$prefix$file";
$small = preg_replace ("/(.*)\.[^\.]+$/", "\\1.$ext", $small);
return $small;
}
// save image $id in file $file (should be thumbnail name);
function write_thumbnail ($id, $file) {
global $prefix;
$ext = get_imagetype();
switch ($ext) {
case "jpg":
imageJPEG ($id, $file);
break;
case "png":
imagePNG ($id, $file);
break;
case "gif":
imageGIF ($id, $file);
break;
default:
echo "Unsupported image type in get_thumbnail_name(): script not finished! (FATAL)";
exit;
break;
}
}
function create_thumbnail ($dir, $file) {
global $sizex, $sizey, $bgred, $bggreen, $bgblue;
$s = getimagesize("$dir/$file");
$n = @ImageCreateTrueColor ($sizex, $sizey);
if (!$n) { $n = ImageCreate ($sizex, $sizey); } // in case imageCreateTrueColor does not exist (GD<2)
$background = ImageColorClosest ($n, $bgred, $bggreen, $bgblue);
ImageFill ($n, 0, 0, $background);
$small = get_thumbnail_name ($dir, $file);
switch ($s[2]) {
//GIF -- supporté que sur certaines plateformes (GD<= 1.5), Free notamment.
case 1:
if (imagetypes() & IMG_GIF)
$i = ImageCreateFromGIF("$dir/$file");
break;
//JPEG
case 2:
if (imagetypes() & IMG_JPG)
$i = ImageCreateFromJPEG("$dir/$file");
break;
//PNG
case 3:
if (imagetypes() & IMG_PNG)
$i = ImageCreateFromPNG("$dir/$file");
break;
}
if ($i) {
// image dest size $destSize[0] = width, $destSize[1] = height
$srcRatio = $s[0]/$s[1]; // width/height ratio
$destRatio = $sizex/$sizey;
if ($destRatio > $srcRatio) {
$destSize[1] = $sizey;
$destSize[0] = $sizey*$srcRatio;
$dy=0;
$dx=($sizex-$destSize[0])/2;
}
else {
$destSize[0] = $sizex;
$destSize[1] = $sizex/$srcRatio;
$dx=0;
$dy=($sizey-$destSize[1])/2;
}
ImagePaletteCopy ($n, $i);
ImageCopyResized ($n, $i, $dx, $dy, 0, 0, $destSize[0], $destSize[1], $s[0], $s[1]);
write_thumbnail ($n, $small);
return $small;
}
return "";
}
function get_unknown() {
global $unknown;
$small = get_thumbnail_name ("gallery/", $unknown);
if (is_readable ($small)) {
return $small;
} else {
return create_thumbnail ("gallery/", $unknown);
}
}
function get_thumbnail ($dir, $file){
$small = get_thumbnail_name ($dir, $file);
if (is_readable ($small)) {
return $small;
} else {
$img = create_thumbnail ($dir, $file);
if (! strlen($img)) {
return get_unknown();
} else {
return $img;
}
}
}
?>
<?
if (@$_GET['refresh']) {
$r = "&refresh=1";
} else {
$r = "";
}
// DIRECTORY LISTING
$dir=opendir($thedir);
while ($file = readdir ($dir)) {
if ($file == ".")
continue;
if ($file == "..") {
$goto = preg_replace ("/(.*)\/[^\/]+\/?$/", "\\1", $thedir);
echo "--> <a href=\"$PHP_SELF?dir=$goto$r\"><i>previous directory</i></a><br />";
continue;
}
if (is_dir ($thedir . "/" . $file))
echo "--> <a href=\"$PHP_SELF?dir=$thedir/$file$r\">$file</a><br />";
continue;
}
closedir($dir);
?>
<?
// IMAGES
if ($thedir == ".") {
$dirstring = "./";
} else {
$dirstring = $thedir;
}
echo "<br /><h2>: [ $dirstring ]: </h2>\n";
$dir = opendir ($thedir);
?>
<?
$nb = 0;
while ($file = readdir ($dir)) {
// avoid PHP files (including this index.php !!
if (preg_match ("/\.php.?$/", $file))
continue;
// avoid HTML files (including this index.php !!
if (preg_match ("/\.html.?$/", $file))
continue;
// avoid ICONs images
if (preg_match ("/^$prefix/", $file))
continue;
// avoid UNKNOWN image
if (preg_match ("/^$unknown$/", $file))
continue;
if (@is_file($thedir . "/" . $file)) {
if ($nb == 0) {
echo "<TR>";
}
$nb++;
// get name of thumbnail and create it if does not exist.
$small = get_thumbnail ($thedir, $file);
$width = floor(100/$nbx);
echo "<TD WIDTH=\"".$width."\"><A HREF=\"$thedir/$file\"><IMG BORDER=\"0\" SRC=\"$small\" ></A></TD>\n";
if ($nb == $nbx) {
$nb = 0;
echo "</TR>\n";
}
}
}
closedir ($dir);
?>
But for some weirdass reason i cannot figure out, the sorting order is messed up. (see http://juerges.net/gallery.php)
First i though it was linux dirextory access times, but thats not it (i think), as they seem to be the same for all the files (extract time from tar archive)
drwxrwxrwx 2 web1 ftponly 8.0K Sep 5 17:18 [2002-01] Old Pictures
drwxrwxrwx 8 web1 ftponly 4.0K Sep 5 17:18 [2002-01] People
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-01] Brunch at Nadines
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-01] Chilling at Jannes
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-01] Chilling at Michis and Evas
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-01] Chilling at Nadines
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-02] Busstop
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-02] Chilling at Kiwis
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-02] Christening Lasse
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-02] Home with Family
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-02] Luzifers
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-02] Tobi's Birthday
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-03] Chilling at Jannes
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-04] Engagement Becci and Maler
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-07] Chilling at Phillips
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-07] Chilling at Sebels
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-08] Chilling at Konstantins
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-08] Hansapark
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-09] Holiday Turkey
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-11] Kiwi's Birthday
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-12] Christmas at Christines
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2003-12] New Years at Karos
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-01] My farewell party
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-02] Party at EUH
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-02] The Sebel Hotel Perth
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-04] Cocktail Party EUH
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-04] EUH with Emily
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-05] City with Lil and Anthony
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-05] Party at Daviss
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-05] Random
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-05] Rivercruise
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-06] Michi, Eva, Butzel, Janne and Phillip
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-06] Party with Manjit, Nibbi and Ahmed
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-07] EFLan
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-07] Lachlans 21st
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-08] Party Flat 51
drwxrwxrwx 2 web1 ftponly 4.0K Sep 5 17:18 [2004-08] System
anyone has any ideas ?
Seb