This repository has been archived by the owner on Jun 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageHelper.php
177 lines (151 loc) · 5.17 KB
/
ImageHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
use \Nette\Object;
use \Nette\Caching\Cache;
use \Nette\Image;
use \Nette\Utils\Strings;
use \Nette\Utils\Html;
use \Nette\InvalidStateException;
/**
* Image helper with automatic image resize and cache.
*
* @author Roman Ozana, [email protected]
* @link www.omdesign.cz
*
* Updated for new Nette 2.0 and for PHP 5.3+ by Martin Štekl
* @author Martin Štekl <[email protected]>
* @link www.steky.cz
* @license MIT
*
* add to presenter before render function
* $this->template->registerHelper('resize', callback($this->context->imageHelper, 'resize'));
* $this->template->registerHelper('gallery', callback($this->context->imageHelper, 'gallery'));
*
* in template
* {= 'media/image.jpg'|resize:40}
* {= 'media/image.jpg'|resize:?x20}
* {= 'media/image.jpg'|resize:40:'alt'}
* {= 'media/image.jpg'|resize:40:'alt':'title'}
*
* gallery link
* {= 'media/image.jpg'|gallery}
*/
class ImageHelper extends Object {
/**
* @var \Nette\Caching\Cache
*/
protected $cache;
/**
* @var string
*/
protected $baseUrl;
/**
* @var string
*/
protected $tempDir;
/**
* @var timestamp Default value is 1 day
*/
protected $cacheExpireTimestamp;
/**
* @param \Nette\Caching\Cache $cache
* @param string $baseUrl Absolute web URL
* @param string $tempDir Temporary directory path
*/
public function __construct(Cache $cache, $baseUrl, $tempDir) {
$this->cache = $cache->derive('images');
$this->baseUrl = $baseUrl;
$this->tempDir = $tempDir;
$this->cacheExpireTimestamp = 24 * 60 * 60; // default value is 1 day
}
/**
* Setup timestamp for cache expiration for each image.
*
* @param timestamp|int $timestamp
*/
public function setCacheExpireTimestamp($timestamp) {
$this->cacheExpireTimestamp = $timestamp;
}
/**
* @param string $filename
* @param string $dimensions
* @param string|null $alt
* @param string|null $title
* @return \Nette\Utils\Html
*/
public function resize($filename, $dimensions = '120x90', $alt = null, $title = null) {
$info = pathinfo($filename);
$title = !is_null($alt) && is_null($title) ? $alt : $title;
$alt = is_null($alt) ? basename($filename, '.' . $info['extension']) : $alt;
list($src, $width, $height) = $this->resizeImageWithCache($filename, $dimensions);
return Html::el('img')->src($this->baseUrl . $src)->width($width)->height($height)->alt($alt)->title($title);
}
/**
* Render gallery image with anchor to bigger sized image.
*
* @param string $filename
* @param string|null $alt
* @param string|null $title
* @param string|null $rel
* @param string $dimensions
* @param string $full_dimensions
* @return \Nette\Utils\Html
*/
public function gallery($filename, $alt = null, $title = null, $rel = null, $dimensions = '120x90', $full_dimensions = '640x480') {
$img = $this->resize($filename, $dimensions, $alt, $title);
list($src, $width, $height) = $this->resizeImageWithCache($filename, $full_dimensions);
return Html::el('a')->href($this->baseUrl . $src)->title($title)->rel($rel)->class('fancybox')->add($img);
}
/**
* Resize image and save thumbs to cache.
*
* @param string $original
* @param string $dimensions
* @param string $public_root
* @return array
* @throws \Nette\InvalidStateException
*/
public function resizeImageWithCache($original, $dimensions, $public_root = WWW_DIR) {
$original_absolute_path = $public_root . '/' . $original;
if (!is_file($original_absolute_path)) {
return array($original, null, null); // check if file exist
}
// check internal cache for image
$subfolder = $this->tempDir;
$subfolder_absolute_path = $public_root . '/' . $subfolder;
$cache = $this->cache;
$key = md5($original . $dimensions . $subfolder . $public_root);
if (isset($cache[$key])) {
return $cache[$key];
}
$info = pathinfo($original_absolute_path);
// read dimensions
$dim = explode('x', $dimensions, null);
$newWidth = isset($dim[0]) ? $dim[0] : null;
$newHeight = isset($dim[1]) ? $dim[1] : null;
// check public cache directory
if (!is_dir($subfolder_absolute_path) || !is_writable($subfolder_absolute_path)) {
throw new InvalidStateException('Thumbnail path ' . $subfolder_absolute_path . ' does not exists or is not writable.');
}
try {
$cache_path = $subfolder_absolute_path . '/' . md5(dirname($original));
if (!is_dir($cache_path)) {
mkdir($cache_path);
}
$image = Image::fromFile($original_absolute_path);
$image->resize((int) $newWidth, (int) $newHeight);
$cache_file = Strings::webalize(basename($original, '.' . $info['extension']) . '-' . $image->getWidth() . 'x' . $image->getHeight()) . '.' . $info['extension'];
$image->save($cache_path . '/' . $cache_file);
// resize image name
$resize = $subfolder . '/' . md5(dirname($original)) . '/' . $cache_file;
$result = array($resize, $image->getWidth(), $image->getHeight());
// save result to internal cache
$cache->save($key, $result, array(
Cache::FILES => $original,
Cache::EXPIRATION => time() + $this->cacheExpireTimestamp,
));
return $result;
} catch (Exception $e) {
return array($original, null, null);
}
}
}