-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFancyImageGalleryController.php
More file actions
294 lines (231 loc) · 6.91 KB
/
FancyImageGalleryController.php
File metadata and controls
294 lines (231 loc) · 6.91 KB
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
<?php
//security measure
if (!defined('IN_CMS')) { exit(); }
/**
* Fancy Image Gallery for Wolf CMS- Create gallery and display images with few clicks
* Gallery is free for non-profit and commercial usage.
* @package wolf
* @subpackage plugin.fancy_image_gallery
*
* @author Sanja Andjelkovic <sanja@medio.com.hr>
* @author Dejan Andjelkovic <dejan@medio.com.hr>
* @version 0.8.6
* @for Wolf version 0.7.0 and above
* @license http://www.gnu.org/licenses/gpl.html GPL License
* @copyright medio.com.hr & project79.net, 2009-2011
*/
class FancyImageGalleryController extends PluginController {
public static function _checkLog()
{
AuthUser::load();
if ( ! AuthUser::isLoggedIn())
{
redirect(get_url('login'));
}
}
public function __construct()
{
self::_checkLog();
$this->setLayout('backend');
$this->assignToLayout('sidebar', new View('../../plugins/fancy_image_gallery/views/sidebar'));
}
function index($page = 0)
{
$this->display('fancy_image_gallery/views/settings');
}
public function documentation()
{
$this->display('fancy_image_gallery/views/documentation');
}
function settings()
{
$this->display('fancy_image_gallery/views/settings', Plugin::getAllSettings('fancy_image_gallery'));
}
public function create_thumb()
{
$path = $_POST['gallery_path'];
if (substr($path, -1) != '/')
{
// Put trailing slash on path instead of warning user about it
$path = $path . '/';
}
$image_dir = CMS_ROOT . '/public/images/' . $path;
if ( ! is_dir($image_dir))
{
Flash::set('error', __('The folder does not exist!'));
redirect(get_url('plugin/fancy_image_gallery/'));
}
// Check for valid width/height
$thumb_width = $_POST['thumb_width'];
$thumb_height = $_POST['thumb_height'];
if ($thumb_width == NULL)
{
Flash::set('error', __('Specify thumbnail width!'));
redirect(get_url('plugin/fancy_image_gallery/'));
}
if ($thumb_height == NULL)
{
Flash::set('error', __('Specify thumbnail height!'));
redirect(get_url('plugin/fancy_image_gallery/'));
}
// Open the images folder and find out what's in
$handle = opendir($image_dir);
$count_images = 0;
if ($handle)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
// Grab extension of file and convert to lowercase
$parts = explode('.', $file);
$ext = end($parts);
$ext = strtolower($ext);
if (in_array($ext, array('jpg', 'gif', 'png')))
{
if (preg_match('/-thumb\./', strtolower($file)))
{
// Delete if thumbnail
unlink($image_dir . '/' . $file);
}
else
{
$files[] = $file;
}
$count_images++;
}
}
}
}
closedir($handle);
$counter = 0;
if($count_images == 0)
{
Flash::set('error', __('There are no images in this folder!'));
redirect(get_url('plugin/fancy_image_gallery/'));
}
if($files)
{
$success_count = 0;
$failure_count = 0;
// Loop through all the files
foreach($files as $file)
{
$parts = explode('.', $file);
$ext = end($parts);
$ext = $ext;
// Process the file and get result
$results[$file] = $this->_process_image($image_dir, $file, $thumb_width, $thumb_height);
// Check status and increment counters
if ($results[$file] == TRUE)
{
$success_count++;
}
else
{
$failure_count++;
}
}
// Set flash messages based on processed status of files
if ($success_count == count($files))
{
Flash::set('success', __('Thumbnails have been successfully created!'));
}
if ($failure_count > 0 && $success_count > 0)
{
Flash::set('error', __('Some thumbnails could not be created.'));
}
if ($failure_count == count($files))
{
Flash::set('error', __('No thumbnail images could be created.'));
}
}
else
{
Flash::set('error', __('There are no images in this folder!'));
}
redirect(get_url('plugin/fancy_image_gallery/'));
}
private function _process_image($image_dir, $filename = '', $dest_width, $dest_height)
{
// Get/set info about the name
$fileparts = explode('.', $filename);
$basename = $fileparts[0];
$ext = end($fileparts);
// Full path to file
$file_path = $image_dir . $filename;
// Initial result to return
$res = false;
// Make correct image handle from source file
switch (strtolower($ext))
{
case 'jpg':
$source = imagecreatefromjpeg($file_path);
break;
case 'gif':
$source = imagecreatefromgif($file_path);
break;
case 'png':
$source = imagecreatefrompng($file_path);
break;
}
// Get dimensions of original file
$width = imagesx($source);
$height = imagesy($source);
// Check dimeisions are OK to resize for
if ($width >= $dest_width AND $height >= $dest_height)
{
$proportion_X = $width / $dest_width;
$proportion_Y = $height / $dest_height;
if ($proportion_X > $proportion_Y )
{
$proportion = $proportion_Y;
}
else
{
$proportion = $proportion_X;
}
$target['width'] = $dest_width * $proportion;
$target['height'] = $dest_height * $proportion;
$original['diagonal_center'] = round(sqrt(($width*$width)+($height*$height))/2);
$target['diagonal_center'] = round(sqrt(($target['width'] * $target['width']) + ($target['height'] * $target['height'])) / 2 );
$crop = round($original['diagonal_center'] - $target['diagonal_center']);
if ($proportion_X < $proportion_Y )
{
$target['x'] = 0;
$target['y'] = round( ( ($height / 2) * $crop) / $target['diagonal_center']);
}
else
{
$target['x'] = round( ( ($width / 2) * $crop) / $target['diagonal_center']);
$target['y'] = 0;
}
// Create handle to new thumbnail image
$dest_img = imagecreatetruecolor($dest_width, $dest_height);
// Copy the source iamge to the new image with crop/resize parameters
imagecopyresampled($dest_img, $source,
0, 0,
$target['x'], $target['y'],
$dest_width, $dest_height,
$target['width'], $target['height']
);
// Save the destination image
switch (strtolower($ext))
{
case 'jpg':
$res = imagejpeg($dest_img, $image_dir . $basename . '-thumb.jpg');
break;
case 'gif':
$res = imagegif($dest_img, $image_dir . $basename . '-thumb.gif');
break;
case 'png':
$res = imagepng($dest_img, $image_dir . $basename . '-thumb.png');
break;
}
// Close files
imagedestroy($dest_img);
imagedestroy($source);
}
return $res;
}
}