-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.php
387 lines (347 loc) · 10.7 KB
/
helper.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* @package Gallery
* @subpackage plg_gallery
* @copyright Copyright (C) 2014 Rene Bentes Pinto, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see http://www.gnu.org/licenses/gpl-2.0.html
*/
// No direct access.
defined('_JEXEC') or die('Restricted access!');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
/**
* Gallery plugin helper class
*
* @package Gallery
* @subpackage plg_gallery
* @since 2.5
*/
abstract class GalleryHelper
{
/**
* Get items to Gallery
*
* @param string $folder Path to image files
* @param string $size Thumbnail size (width x height)
* @param boolean $smartResize Type of resize
* @param integer $quality JPEG quality
* @param integer $expireTime Cache time
* @param integer $id Gallery identifier
*
* @return ob Gallery items
*/
public static function getGallery($folder, $size = '200x160', $smartResize = true, $quality = 90, $expireTime = 86400, $id)
{
$folder = JPATH_SITE . '/images/' . $folder;
return self::_createThumbnail($folder, $size, $smartResize, $quality, $expireTime, $id);
}
/**
* Gets captions for items
*
* @param string $folder The gallery path
* @param string $reference The item gallery filename
*
* @return object Captions
*/
private static function _getCaption($folder, $reference)
{
$file = $folder . '/gallery.txt';
$caption = new JObject;
$caption->title = '';
$caption->description = '';
if (JFile::exists($file))
{
$content = file($file);
foreach ($content as $line)
{
$temp = explode(';', $line);
if (strtolower($temp[0]) == strtolower($reference))
{
$caption->title = nl2br($temp[1]);
$caption->description = nl2br($temp[2]);
break;
}
}
}
return $caption;
}
/**
* Get the path to a layout from Gallery
*
* @param string $type Plugin type
* @param string $name Plugin name
* @param string $layout Layout name
*
* @return string Layout path
*
* @since 2.5
*/
public static function getLayoutPath($type, $name, $layout = 'default')
{
$template = JFactory::getApplication()->getTemplate();
$defaultLayout = $layout;
if (strpos($layout, ':') !== false)
{
// Get the template and file name from the string
$temp = explode(':', $layout);
$template = ($temp[0] == '_') ? $template : $temp[0];
$layout = $temp[1];
$defaultLayout = ($temp[1]) ? $temp[1] : 'default';
}
// Build the template and base path for the layout
$tPath = JPATH_THEMES . '/' . $template . '/html/plg_' . $type . '_' . $name . '/' . $layout . '.php';
$bPath = JPATH_BASE . '/plugins/' . $type . '/' . $name . '/tmpl/' . $defaultLayout . '.php';
$dPath = JPATH_BASE . '/plugins/' . $type . '/' . $name . '/tmpl/default.php';
// If the template has a layout override use it
if (file_exists($tPath))
{
return $tPath;
}
elseif (file_exists($bPath))
{
return $bPath;
}
else
{
return $dPath;
}
}
/**
* Creates thumbnails for images
*
* @param string $folder Path to image files
* @param string $size Thumbnail size (width x height)
* @param boolean $smartResize Type of resize
* @param integer $quality JPEG quality
* @param integer $expireTime Cache time
* @param integer $id Gallery identifier
*
* @return array Gallery thumbnails
*/
private static function _createThumbnail($folder, $size, $smartResize, $quality, $expireTime, $id)
{
if (!JFolder::exists($folder))
{
JFactory::getApplication()->enqueueMessage(JText::_('PLG_CONTENT_GALLERY_ERROR_PATH_NOT_FOUND'), 'error');
return;
}
$files = JFolder::files($folder);
if (!$files)
{
JFactory::getApplication()->enqueueMessage(JText::_('PLG_CONTENT_GALLERY_ERROR_FILES_NOT_FOUND'), 'error');
return;
}
$size = explode('x', strtolower($size));
if (count($size) != 2)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('PLG_CONTENT_GALLERY_ERROR_ARGUMENT_SIZE_INVALID', $size), 'error');
return;
}
if (!is_numeric($size[0]) || $size[0] < 0)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('PLG_CONTENT_GALLERY_ERROR_WIDTH_INVALID', $size[0]), 'error');
return;
}
if (!is_numeric($size[1]) || $size[1] < 0)
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('PLG_CONTENT_GALLERY_ERROR_HEIGHT_INVALID', $size[1]), 'error');
return;
}
$cache = JPATH_SITE . '/cache/plg_content_gallery/' . $id;
if (JFolder::exists($cache) || JFolder::create($cache))
{
$fileTypes = array('jpg', 'jpeg', 'gif', 'png');
$found = array();
foreach ($files as $file)
{
$fileInfo = pathinfo($file);
if (array_key_exists('extension', $fileInfo) && in_array(strtolower($fileInfo['extension']), $fileTypes))
{
$found[] = $file;
}
}
// Bail out if there are no images found
if (count($found) == 0)
{
JFactory::getApplication()->enqueueMessage(JText::_('PLG_CONTENT_GALLERY_ERROR_IMAGES_NOT_FOUND'), 'error');
return;
}
sort($found);
$gallery = array();
foreach ($found as $key => $filename)
{
$gallery[$key] = new JObject;
if (self::_getCaption($folder, $filename))
{
$gallery[$key]->title = self::_getCaption($folder, $filename)->title;
$gallery[$key]->description = self::_getCaption($folder, $filename)->description;
}
$original = $folder . '/' . $filename;
$thumbname = $cache . '/' . strtolower($filename);
// Check if thumbnail exists already
if (!(JFile::exists($thumbname) && is_readable($thumbname) && (filemtime($thumbname) + $expireTime) > time()))
{
list($width, $height, $type) = getimagesize($original);
switch($type)
{
case 1 :
$source = @ imagecreatefromgif($original);
break;
case 2 :
$source = imagecreatefromjpeg($original);
break;
case 3 :
$source = imagecreatefrompng($original);
break;
default :
$source = NULL;
}
if (!$source)
{
JFactory::getApplication()->enqueueMessage(JText::_('PLG_CONTENT_GALLERY_ERROR_PROCESS_SOURCE'), 'error');
return;
}
$thumbnail = self::_getDimension($width, $height, $size[0], $size[1], $smartResize);
$thumb = imagecreatetruecolor($size[0], $size[1]);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $size[0], $size[1], $width, $height);
switch($type)
{
case 1 :
$success = imagegif($thumb, $thumbname);
break;
case 2 :
$success = imagejpeg($thumb, $thumbname, $quality);
break;
case 3 :
$success = imagepng($thumb, $thumbname);
break;
default :
$success = NULL;
}
// Bail out if there is a problem in the GD conversion
if (!$success)
{
return;
}
// remove the image resources from memory
imagedestroy($source);
imagedestroy($thumb);
}
$gallery[$key]->filename = $filename;
$gallery[$key]->image = str_replace(JPATH_SITE, JUri::base(true), $original);
$gallery[$key]->thumbnail = str_replace(JPATH_SITE, JUri::base(true), $thumbname);
}
}
return $gallery;
}
/**
* Calculate the dimensions of the thumbnails
*
* @param integer $width Source width
* @param integer $height Source height
* @param integer $thb_width Thumbnail width
* @param integer $thb_height Thumbnail height
* @param boolean $smartResize Resize type
*
* @return array The thumbnail dimensions
*/
private static function _getDimension($width, $height, $thb_width, $thb_height, $smartResize)
{
if ($smartResize)
{
// thumb ratio bigger that container ratio
if ($width / $height > $thb_width / $thb_height)
{
// wide containers
if ($thb_width >= $thb_height)
{
// wide thumbs
if ($width > $height)
{
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
}
// high thumbs
else
{
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
}
// high containers
}
else
{
// wide thumbs
if ($width > $height)
{
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
}
// high thumbs
else
{
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
}
}
}
else
{
// wide containers
if ($thb_width >= $thb_height)
{
// wide thumbs
if ($width > $height)
{
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
}
// high thumbs
else
{
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
}
// high containers
}
else
{
// wide thumbs
if ($width > $height)
{
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
}
// high thumbs
else
{
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
}
}
}
}
else
{
if ($width > $height)
{
$thumb_width = $thb_width;
$thumb_height = $thb_width * $height / $width;
}
elseif ($width < $height)
{
$thumb_width = $thb_height * $width / $height;
$thumb_height = $thb_height;
}
else
{
$thumb_width = $thb_width;
$thumb_height = $thb_height;
}
}
$thumbnail = array();
$thumbnail['width'] = round($thumb_width);
$thumbnail['height'] = round($thumb_height);
return $thumbnail;
}
}