Plugin to render thumbs as squares
authorMarc MAURICE <marc-bizou@pub.positon.org>
Sat, 17 Aug 2013 17:17:32 +0000 (19:17 +0200)
committerMarc MAURICE <marc-bizou@pub.positon.org>
Sat, 17 Aug 2013 17:17:32 +0000 (19:17 +0200)
plugins/_disabled/squares/functions.php [new file with mode: 0644]
plugins/_disabled/squares/style.css [new file with mode: 0644]
template.php

diff --git a/plugins/_disabled/squares/functions.php b/plugins/_disabled/squares/functions.php
new file mode 100644 (file)
index 0000000..0c58481
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+
+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))
+        {
+                # 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);
+
+                $ext = strtolower(substr($imgFile, -4));
+                if ($ext == ".jpg")
+                        $img = imagecreatefromjpeg($imgFile);
+                else
+                        $img = imagecreatefrompng($imgFile);
+
+                $w = imagesx($img);
+                $h = imagesy($img);
+                # if the image is already small and squared, make a symlink and return it
+                if ($w <= $maxSize and $h <= $maxSize and $w == $h) {
+                        imagedestroy($img);
+                        symlink($imgFile, $newImgFile);
+                        return $imgFile;
+                }
+
+                # config to allow group writable files
+                umask(DATA_UMASK);
+                # create the thumbs directory recursively
+                if (! is_dir(dirname($newImgFile))) mkdir(dirname($newImgFile), 0777, true);
+
+                if ($w > $h) {
+                     $srcSize = $h;
+                     $srcY = 0;
+                     $srcX = ($w - $srcSize)/2;
+                } elseif ($w < $h) {
+                     $srcSize = $w;
+                     $srcX = 0;
+                     $srcY = ($h - $srcSize)/2;
+               } else {
+                     $srcSize = $w;
+                     $srcX = $srcY = 0;
+                }
+
+                $newImg = imagecreatetruecolor(THUMB_SIZE, THUMB_SIZE);
+
+                imagecopyresampled($newImg, $img, 0, 0, $srcX, $srcY, THUMB_SIZE, THUMB_SIZE, $srcSize, $srcSize);
+
+                if ($ext == ".jpg")
+                        imagejpeg($newImg, $newImgFile);
+                else
+                        imagepng($newImg, $newImgFile);
+
+                imagedestroy($img);
+                imagedestroy($newImg);
+        }
+
+        return $newImgFile;
+}
+
+
+?>
diff --git a/plugins/_disabled/squares/style.css b/plugins/_disabled/squares/style.css
new file mode 100644 (file)
index 0000000..4fbf695
--- /dev/null
@@ -0,0 +1,13 @@
+#images .square {
+        display: block;
+        float: left;
+        margin: 1px;
+}
+#images .image {
+        display: block;
+}
+#images {
+        overflow: hidden;
+       clear: both;
+       margin-bottom: 1ex;
+}
index 1d0b4bc..1508f8b 100644 (file)
@@ -56,6 +56,7 @@ a {
 
 <?php plugins_include("before_content.php") ?>
 
+<div id="folders">
 <?php foreach($folders as $folder) { $preview = getAlbumPreview($folder["file"]); ?>
        <div class="folder">
        <?php if ($preview === "") { ?>
@@ -68,15 +69,20 @@ a {
        <?php } ?>
        </div>
 <?php } ?>
+</div>
 
+<div id="images">
 <?php foreach ($imageFiles as $file) { ?>
        <div class="square"><div class="image"><a href="<?php echo $file["link"] ?>"><img src="<?php echo $rootUrl.getPreview($file["file"]) ?>" alt="<?php echo $file["name"] ?>" /></a></div></div>
        <?php if (isset($generating)) { ob_flush(); flush(); } ?>
 <?php } ?>
+</div>
 
+<div id="miscfiles">
 <?php foreach ($otherFiles as $file) { ?>
        <div class="miscfile"><a href="<?php echo $file["link"] ?>"><?php echo $file["name"] ?></a></div>
 <?php } ?>
+</div>
 
 <?php plugins_include("after_content.php") ?>