From d94ee6e57197450a295e0c72be8712b813104a62 Mon Sep 17 00:00:00 2001 From: Marc MAURICE Date: Sun, 24 Jul 2011 19:11:19 +0200 Subject: [PATCH] No more progress page: page is displayed directly with flushes Album preview look recursively for pics in folders, and keep previews in cache Symlinks are created when the image is already good sized --- index.php | 83 +++++++++++++++++++++++++++++++--------------------------- template.php | 10 ++++--- 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/index.php b/index.php index 846c7ae..f028451 100644 --- a/index.php +++ b/index.php @@ -48,37 +48,18 @@ function getImageLink($imageSimplePath) } } -// functions to display a "progress page" when thumbs are generating -function beginGenerating () -{ - if (! isset($GLOBALS["generating"])) { - echo "

If you get: \"Fatal error: Maximum execution time exceeded\", refresh this page.
Please wait while generating thumbnails:

\n"; - ob_flush(); flush(); - $GLOBALS["generating"] = true; - } -} -function displayGenerated($thumbFile) -{ - if (isset($GLOBALS["generating"])) { - echo basename($thumbFile)."\n"; - ob_flush(); flush(); - } -} -function endGenerating() { - if (isset($GLOBALS["generating"])) { - echo "

Finished. This page will be refreshed.

\n"; - exit(); - } -} - function getPreview($imgFile, $maxSize = THUMB_SIZE) { # example: data/myalbum/100.mypic.jpg $newImgFile = DATA_DIR."/".dirname($imgFile)."/".$maxSize.".".basename($imgFile); + # if the preview is a symlink, image is already good sized + if (is_link($newImgFile)) return $imgFile; + if (! is_file($newImgFile)) { - beginGenerating(); + # this tels the template to flush output after displaying previews + $GLOBALS["generating"] = true; # reset script time limit to 20s (wont work in safe mode) set_time_limit(20); @@ -91,15 +72,15 @@ function getPreview($imgFile, $maxSize = THUMB_SIZE) $w = imagesx($img); $h = imagesy($img); - # don't do anything if the image is already small + # if the image is already small, make a symlink, and return it if ($w <= $maxSize and $h <= $maxSize) { imagedestroy($img); + symlink($imgFile, $newImgFile); return $imgFile; } # uncomment this if you need group writable files #umask(0002); - # create the thumbs directory recursively if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true); @@ -122,22 +103,48 @@ function getPreview($imgFile, $maxSize = THUMB_SIZE) imagedestroy($img); imagedestroy($newImg); - - displayGenerated($newImgFile); } - return $GLOBALS['rootUrl'].$newImgFile; + return $newImgFile; } function getAlbumPreview($dir) { - foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') { - $ext = strtolower(substr($file, -4)); - if ($ext == ".jpg" or $ext == ".png") - return getPreview("$dir/$file"); - } + $previewFile = DATA_DIR."/$dir/albumpreview"; + + if (is_file("$previewFile.jpg")) { + return "$previewFile.jpg"; + } else if (is_file("$previewFile.empty")) { + return ""; + } else if (is_file("$previewFile.png")) { + return "$previewFile.png"; + } else { + # uncomment this if you need group writable files + #umask(0002); + # create the thumbs directory recursively + if (! is_dir(dirname($previewFile))) mkdir(dirname($previewFile), 0777, true); + + // no preview: look for a preview in current dir, write it, return it + foreach (scandir($dir) as $file) if ($file != '.' and $file != '..') { + $ext = strtolower(substr($file, -4)); + if ($ext == ".jpg" or $ext == ".png") { + $thumb = getPreview("$dir/$file"); + copy($thumb, $previewFile.$ext); + return $previewFile.$ext; + } else if (is_dir("$dir/$file")) { + $subPreview = getAlbumPreview("$dir/$file"); + if ($subPreview) { + $myPreview = dirname($previewFile)."/".basename($subPreview); + copy($subPreview, $myPreview); + return $myPreview; + } + } + } - return ''; + // nothing found. create empty file + touch("$previewFile.empty"); + return ""; + } } // if url == http://localhost/photos/index.php/toto/titi, path_info == /toto/titi @@ -173,21 +180,19 @@ foreach (scandir($realDir) as $file) if ($file != '.' and $file != '..') { if (is_dir("$realDir/$file")) { - $folders[] = array( "name" => $file, "link" => "$scriptUrl$simplePath/$file", "preview" => getAlbumPreview("$realDir/$file") ); + $folders[] = array( "name" => $file, "file" => "$realDir/$file", "link" => "$scriptUrl$simplePath/$file" ); } else { $ext = strtolower(substr($file, -4)); if ($ext == ".jpg" or $ext == ".png") { - $imageFiles[] = array( "name" => $file, "url" => getPreview("$realDir/$file"), "link" => getImageLink("$simplePath/$file") ); + $imageFiles[] = array( "name" => $file, "file" => "$realDir/$file", "link" => getImageLink("$simplePath/$file") ); } else { $otherFiles[] = array( "name" => $file, "link" => "$rootUrl$realDir/$file" ); } } } -endGenerating(); - if (dirname($simplePath) !== '') $parentLink = $scriptUrl.dirname($simplePath); else diff --git a/template.php b/template.php index 34443c2..ef94f88 100644 --- a/template.php +++ b/template.php @@ -54,20 +54,22 @@ a { - +
- +
-
">
- +
">
">
+
-
+
+ -- 1.7.10.4