Plugin to render thumbs as squares

This commit is contained in:
Marc MAURICE 2013-08-17 19:17:32 +02:00
parent 26887007f8
commit 4e42aa333d
3 changed files with 88 additions and 0 deletions

View file

@ -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;
}
?>

View file

@ -0,0 +1,13 @@
#images .square {
display: block;
float: left;
margin: 1px;
}
#images .image {
display: block;
}
#images {
overflow: hidden;
clear: both;
margin-bottom: 1ex;
}

View 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") ?>