From 7a02804d9901a61dc6e4d513bb79ff1bbb19c9cb Mon Sep 17 00:00:00 2001 From: AnrDaemon Date: Fri, 23 Jun 2017 22:54:51 +0300 Subject: [PATCH] Prepare release 1.2 Added GD2 driver for thumbnails creation, in case Imagick is not available. --- CHANGES | 17 +++++++++----- Gallery.php | 65 ++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/CHANGES b/CHANGES index 909d375..20ef410 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,11 @@ ------------------------------------------------------------------------- -r669 | anrdaemon | 2017-06-22 18:52:05 +0300 (Чт, 22 июн 2017) | 2 lines - -+ Encapsulate link composition. - ------------------------------------------------------------------------- +------------------------------------------------------------------------ +r671 | anrdaemon | 2017-06-23 22:49:17 +0300 (Пт, 23 июн 2017) | 2 lines + +* Tweak thumbnail image dimensions calculation. + +------------------------------------------------------------------------ +r670 | anrdaemon | 2017-06-23 22:38:51 +0300 (Пт, 23 июн 2017) | 2 lines + ++ GD2 support for thumbnail images. + +------------------------------------------------------------------------ diff --git a/Gallery.php b/Gallery.php index 1d47fe5..a1c61ac 100644 --- a/Gallery.php +++ b/Gallery.php @@ -3,7 +3,7 @@ * * A simple drop-in file-based HTML gallery. * -* $Id: Gallery.php 669 2017-06-22 15:52:05Z anrdaemon $ +* $Id: Gallery.php 671 2017-06-23 19:49:17Z anrdaemon $ */ namespace AnrDaemon\MyLittleGallery; @@ -12,6 +12,7 @@ ArrayAccess, Countable, Exception, + ErrorException, Imagick, Iterator, NumberFormatter, @@ -23,7 +24,7 @@ class Gallery const previewTemplate = '
%3$s

%3$s

'; - const defaultTypes = 'gif|jpeg|jpg|png|tif|tiff|wbmp|webp'; + const defaultTypes = 'gif|jpeg|jpg|png|wbmp|webp'; // All paths are UTF-8! (Except those from SplFileInfo) protected $path; // Gallery base path @@ -273,23 +274,71 @@ public function getUrl($prefix, $name) public function thumbnailImage($name) { - $path = iconv('UTF-8', $this->cs, "{$this->path}/.preview/$name"); + static $gdSave = array( + 'image/gif' => 'imagegif', + 'image/jpeg' => 'imagejpeg', + 'image/png' => 'imagepng', + 'image/vnd.wap.wbmp' => 'imagewbmp', + 'image/webp' => 'imagewebp', + ); + + if(!isset($this->params[$name])) + throw new Exception("Image '$name' is not registered in the gallery.", 404); + + $path = $this->getPath("/.preview/$name", true); if(is_file($path)) return true; try { - //$img = new Imagick(iconv('UTF-8', $this->cs, "{$this->path}/$name")); - $img = new Imagick("{$this->path}/$name"); - $img->thumbnailImage($this->pWidth, $this->pHeight, true); - $img->writeImage("{$this->path}/.preview/$name"); + set_error_handler(function($s, $m, $f, $l, $c = null) { throw new ErrorException($m, 0, $s, $f, $l); }); + + if(class_exists('Imagick')) + { + $img = new Imagick("{$this->path}/$name"); + $img->thumbnailImage($this->pWidth, $this->pHeight, true); + $img->writeImage("{$this->path}/.preview/$name"); + } + elseif(function_exists('imagecreatefromstring')) + { + $src = imagecreatefromstring(file_get_contents($this->getPath("/$name", true))); + if($src === false) + throw new Exception("The file '$name' can't be interpreted as image.", 500); + + $oFactor = $this->params[$name]['width'] / $this->params[$name]['height']; + $tFactor = $this->pWidth / $this->pHeight; + if($oFactor >= $tFactor) + { + $w = $this->pWidth; + $h = min($this->pHeight, ceil($this->pWidth / $oFactor)); + } + else + { + $w = min($this->pWidth, ceil($this->pHeight * $oFactor)); + $h = $this->pHeight; + } + + $img = imagecreatetruecolor($w, $h); + if(!imagecopyresampled($img, $src, 0, 0, 0, 0, $w, $h, $this->params[$name]['width'], $this->params[$name]['height'])) + throw new Exception("Unable to create thumbnail for '$name'.", 500); + + $gdSave[$this->params[$name]['mime']]($img, $path); + } + else + throw new ErrorException('Imagick or gd2 extension is required to create thumbnails at runtime.', 501); + + restore_error_handler(); } catch(Exception $e) { + restore_error_handler(); if(!is_dir(dirname($path))) + { mkdir(dirname($path)); + return false; + } - return false; + throw $e; } return true;