Skip to content

Commit

Permalink
Prepare release 1.2
Browse files Browse the repository at this point in the history
Added GD2 driver for thumbnails creation, in case Imagick is not available.
  • Loading branch information
AnrDaemon committed Jun 23, 2017
1 parent 8a12519 commit 7a02804
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
17 changes: 11 additions & 6 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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.

------------------------------------------------------------------------
65 changes: 57 additions & 8 deletions Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +12,7 @@
ArrayAccess,
Countable,
Exception,
ErrorException,
Imagick,
Iterator,
NumberFormatter,
Expand All @@ -23,7 +24,7 @@ class Gallery
const previewTemplate =
'<div><a href="%1$s" target="_blank"><img src="%2$s" alt="%3$s"/></a><p><a href="%1$s" target="_blank">%3$s</a></p></div>';

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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7a02804

Please sign in to comment.