1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DavideCasiraghi\ResponsiveGallery; |
4
|
|
|
|
5
|
|
|
class ResponsiveGalleryFactory |
6
|
|
|
{ |
7
|
|
|
/************************************************************************/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Returns the plugin parameters. |
11
|
|
|
* @param array $matches result from the regular expression on the string from the article |
12
|
|
|
* @return array $ret the array containing the parameters |
13
|
|
|
**/ |
14
|
1 |
|
public static function getGalleryParameters($matches, $publicPath) |
15
|
|
|
{ |
16
|
1 |
|
$ret = []; |
17
|
|
|
|
18
|
|
|
// Get Paths |
19
|
1 |
|
$sitePath = '/'; |
|
|
|
|
20
|
1 |
|
$siteUrl = $publicPath; |
|
|
|
|
21
|
|
|
|
22
|
|
|
// Get activation string parameters (from article) |
23
|
1 |
|
$ret['token'] = $matches[0]; |
24
|
1 |
|
$subDir = $matches[2]; |
25
|
1 |
|
$ret['column_width'] = $matches[4]; |
26
|
1 |
|
$ret['gutter'] = $matches[6]; |
27
|
|
|
|
28
|
|
|
// Directories |
29
|
1 |
|
$ret['images_dir'] = $publicPath.'/'.$subDir.'/'; |
30
|
1 |
|
$ret['thumbs_dir'] = $publicPath.'/'.$subDir.'/thumb/'; |
31
|
|
|
|
32
|
|
|
// Thumbnails size |
33
|
1 |
|
$ret['thumbs_size']['width'] = 300; |
34
|
1 |
|
$ret['thumbs_size']['height'] = 300; |
35
|
|
|
|
36
|
|
|
// URL variables |
37
|
1 |
|
$ret['gallery_url'] = $subDir.'/'; |
38
|
1 |
|
$ret['thumb_url'] = $subDir.'/thumbs/'; |
39
|
|
|
|
40
|
1 |
|
return $ret; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/************************************************************************/ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Generate a single thumbnail file from an image. |
47
|
|
|
* @param array $src path of the original image |
48
|
|
|
* @param array $dest path of the generated thumbnail |
49
|
|
|
* @param array $desired_width width of the thumbnail |
50
|
|
|
* @return create a file |
|
|
|
|
51
|
|
|
**/ |
52
|
2 |
|
public function generate_single_thumb_file($src, $dest, $desired_width, $desired_height) |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
// Read the source image |
55
|
2 |
|
$source_image = imagecreatefromjpeg($src); |
|
|
|
|
56
|
|
|
|
57
|
|
|
// Get width and height of the original image |
58
|
2 |
|
$width = imagesx($source_image); |
59
|
2 |
|
$height = imagesy($source_image); |
60
|
|
|
|
61
|
|
|
// Horizontal image |
62
|
2 |
|
if ($width > $height) { |
63
|
2 |
|
$desired_width = 400; |
64
|
2 |
|
$desired_height = floor($height * ($desired_width / $width)); |
65
|
|
|
} |
66
|
|
|
// Vertical image |
67
|
|
|
else { |
68
|
1 |
|
$desired_height = 500; |
69
|
1 |
|
$desired_width = floor($width * ($desired_height / $height)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Create a new, "virtual" image |
73
|
2 |
|
$virtual_image = imagecreatetruecolor($desired_width, $desired_height); |
|
|
|
|
74
|
|
|
|
75
|
|
|
// Copy source image at a resized size |
76
|
2 |
|
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); |
|
|
|
|
77
|
|
|
|
78
|
|
|
//Create the physical thumbnail image to its destination |
79
|
2 |
|
imagejpeg($virtual_image, $dest); |
|
|
|
|
80
|
2 |
|
} |
81
|
|
|
|
82
|
|
|
/************************************************************************/ |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generate all the thumbnails of the gallery. |
86
|
|
|
* @param string $images_dir images dir on the server |
87
|
|
|
* @param string $thumbs_dir thumb dir on the server |
88
|
|
|
* @param string $$thumbs_size |
89
|
|
|
* @param array $image_files |
90
|
|
|
* @return generate thumbnail files |
|
|
|
|
91
|
|
|
**/ |
92
|
1 |
|
public function generateThumbs($images_dir, $thumbs_dir, $thumbs_size, $image_files) |
93
|
|
|
{ |
94
|
|
|
// Thumbnails size |
95
|
1 |
|
$thumbs_width = $thumbs_size['width']; |
96
|
1 |
|
$thumbs_height = $thumbs_size['height']; |
97
|
|
|
|
98
|
|
|
// Create thumbs dir |
99
|
1 |
|
if (! is_dir($thumbs_dir)) { |
100
|
1 |
|
mkdir($thumbs_dir); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Generate missing thumbs |
104
|
1 |
|
if (count($image_files)) { |
105
|
1 |
|
$index = 0; |
|
|
|
|
106
|
1 |
|
foreach ($image_files as $index=>$file) { |
107
|
1 |
|
$index++; |
108
|
1 |
|
$thumbnail_image = $thumbs_dir.$file; |
109
|
1 |
|
if (! file_exists($thumbnail_image)) { |
110
|
1 |
|
$extension = self::get_file_extension($thumbnail_image); |
111
|
1 |
|
if ($extension) { |
112
|
|
|
//echo $images_dir." ".$file." ".$thumbnail_image." ".$thumbs_width; |
113
|
1 |
|
$this->generate_single_thumb_file($images_dir.$file, $thumbnail_image, $thumbs_width, $thumbs_height); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
1 |
|
} |
119
|
|
|
|
120
|
|
|
/************************************************************************/ |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Create images array. |
124
|
|
|
* @param array $image_files array with all the image names |
125
|
|
|
* @param $gallery_url |
126
|
|
|
* @return $ret array with the images datas |
|
|
|
|
127
|
|
|
**/ |
128
|
1 |
|
public function createImagesArray($image_file_names, $gallery_url, $dbImageDatas) |
129
|
|
|
{ |
130
|
1 |
|
sort($image_file_names); // Order by image name |
131
|
|
|
|
132
|
1 |
|
$ret = []; |
133
|
|
|
|
134
|
1 |
|
foreach ($image_file_names as $k => $image_file_name) { |
135
|
1 |
|
$ret[$k]['file_name'] = $image_file_name; |
136
|
1 |
|
$ret[$k]['file_path'] = $gallery_url.$image_file_name; |
137
|
1 |
|
$ret[$k]['thumb_path'] = $gallery_url.'thumb/'.$image_file_name; |
138
|
1 |
|
$ret[$k]['description'] = ''; |
139
|
1 |
|
$ret[$k]['alt'] = ''; |
140
|
1 |
|
$ret[$k]['video_link'] = null; |
141
|
|
|
|
142
|
1 |
|
if (! empty($dbImageDatas[$image_file_name])) { |
143
|
|
|
$ret[$k]['description'] = $dbImageDatas[$image_file_name]->description; |
144
|
|
|
$ret[$k]['alt'] = $dbImageDatas[$image_file_name]->alt; |
145
|
1 |
|
$ret[$k]['video_link'] = $dbImageDatas[$image_file_name]->video_link; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return $ret; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/************************************************************************/ |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get images file names array. |
156
|
|
|
* @param $images_dir the images dir on the server |
|
|
|
|
157
|
|
|
* @return array $ret array containing all the images file names |
158
|
|
|
**/ |
159
|
3 |
|
public function getImageFiles($images_dir) |
160
|
|
|
{ |
161
|
3 |
|
$ret = $this->get_files($images_dir); |
162
|
|
|
|
163
|
3 |
|
return $ret; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/************************************************************************/ |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Prepare the gallery HTML. |
170
|
|
|
* @param array $images Images array [file_path, short_desc, long_desc] |
171
|
|
|
* @param array $bootstrapDeviceImageWidth array that contain the sizes of the images |
172
|
|
|
* for the four kind of bootrap devices classes () |
173
|
|
|
* xs (phones), sm (tablets), md (desktops), and lg (larger desktops) |
174
|
|
|
* @param ****array $desired_width width of the thumbnail |
|
|
|
|
175
|
|
|
* @return string $ret the HTML to print on screen |
176
|
|
|
**/ |
177
|
1 |
|
public function prepareGallery($images, $parameters) |
178
|
|
|
{ |
179
|
|
|
// Animate box on hover |
180
|
1 |
|
$itemClass = 'animated'; |
181
|
|
|
|
182
|
|
|
// The gallery HTML |
183
|
1 |
|
$ret = "<div class='responsiveGallery bricklayer' id='my-bricklayer' data-column-width='".$parameters['column_width']."' data-gutter='".$parameters['gutter']."'>"; |
184
|
1 |
|
foreach ($images as $k => $image) { |
185
|
|
|
// Get item link |
186
|
1 |
|
$imageLink = ($image['video_link'] == null) ? $image['file_path'] : $image['video_link']; |
187
|
1 |
|
$videoPlayIcon = ($image['video_link'] == null) ? '' : "<i class='far fa-play-circle'></i>"; |
188
|
|
|
|
189
|
1 |
|
$ret .= "<div class='box ".$itemClass."'>"; |
190
|
1 |
|
$ret .= "<a href='".$imageLink."' data-fancybox='images' data-caption='".$image['description']."'>"; |
191
|
1 |
|
$ret .= "<img src='".$image['thumb_path']."' alt='".$image['alt']."'/>"; |
192
|
1 |
|
$ret .= $videoPlayIcon; |
193
|
1 |
|
$ret .= '</a>'; |
194
|
1 |
|
$ret .= '</div>'; |
195
|
|
|
} |
196
|
1 |
|
$ret .= '</div>'; |
197
|
|
|
|
198
|
1 |
|
return $ret; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/************************************************************************/ |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns files from dir. |
205
|
|
|
* @param string $images_dir The images directory |
206
|
|
|
* @param array $exts the file types (actually doesn't work the thumb with png, it's to study why) |
207
|
|
|
* @return array $files the files array |
208
|
|
|
**/ |
209
|
3 |
|
public function get_files($images_dir, $exts = ['jpg']) |
210
|
|
|
{ |
211
|
3 |
|
$files = []; |
212
|
|
|
|
213
|
3 |
|
if ($handle = opendir($images_dir)) { |
214
|
3 |
|
while (false !== ($file = readdir($handle))) { |
215
|
3 |
|
$extension = strtolower($this->get_file_extension($file)); |
216
|
3 |
|
if ($extension && in_array($extension, $exts)) { |
217
|
3 |
|
$files[] = $file; |
218
|
|
|
} |
219
|
|
|
} |
220
|
3 |
|
closedir($handle); |
221
|
|
|
} |
222
|
|
|
|
223
|
3 |
|
return $files; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/************************************************************************/ |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns a file's extension. |
230
|
|
|
* @param string $file_name the file name |
231
|
|
|
* @return string the extension |
232
|
|
|
**/ |
233
|
4 |
|
public static function get_file_extension($file_name) |
234
|
|
|
{ |
235
|
4 |
|
return substr(strrchr($file_name, '.'), 1); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/************************************************************************/ |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Find the gallery snippet occurances in the text. |
242
|
|
|
* @param array $text the text where to find the gallery snippets |
243
|
|
|
* @return array $ret the matches |
244
|
|
|
**/ |
245
|
1 |
|
public function getGallerySnippetOccurrences($text) |
246
|
|
|
{ |
247
|
1 |
|
$re = '/{\# |
248
|
|
|
\h+gallery |
249
|
|
|
\h+(src|column_width|gutter)=\[([^]]*)] |
250
|
|
|
\h+((?1))=\[([^]]*)] |
251
|
|
|
\h+((?1))=\[([^]]*)] |
252
|
|
|
\h*\#}/x'; |
253
|
|
|
|
254
|
1 |
|
if (preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0)) { |
|
|
|
|
255
|
1 |
|
return $matches; |
256
|
|
|
} else { |
257
|
1 |
|
return; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/************************************************************************/ |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Retrieve the datas from the package config file (published and edited by the user). |
265
|
|
|
* @param none |
266
|
|
|
* @return array $ret - the config parapeters |
267
|
|
|
**/ |
268
|
|
|
public function getPhotoDatasFromDb() |
269
|
|
|
{ |
270
|
|
|
$ret = GalleryImage::get()->keyBy('file_name'); |
271
|
|
|
//dd($ret); |
272
|
|
|
return $ret; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/************************************************************************/ |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Return the post body with the gallery HTML instead of the found snippet. |
279
|
|
|
* @param array $file_name the file name |
280
|
|
|
* @return array $ret the extension |
281
|
|
|
**/ |
282
|
|
|
public function getGallery($postBody, $publicPath) |
283
|
|
|
{ |
284
|
|
|
$matches = $this->getGallerySnippetOccurrences($postBody); |
285
|
|
|
|
286
|
|
|
foreach ($matches as $key => $single_gallery_matches) { |
287
|
|
|
$parameters = self::getGalleryParameters($single_gallery_matches, $publicPath); |
288
|
|
|
|
289
|
|
|
if (is_dir($parameters['images_dir'])) { |
290
|
|
|
// Get images file name array |
291
|
|
|
$image_files = $this->getImageFiles($parameters['images_dir']); |
292
|
|
|
|
293
|
|
|
if (! empty($image_files)) { |
294
|
|
|
|
295
|
|
|
$this->generateThumbs($parameters['images_dir'], $parameters['thumbs_dir'], $parameters['thumbs_size'], $image_files); |
296
|
|
|
$dbImageDatas = $this->getPhotoDatasFromDb(); |
297
|
|
|
|
298
|
|
|
// Create Images array [file_path, short_desc, long_desc] |
299
|
|
|
$images = $this->createImagesArray($image_files, $parameters['gallery_url'], $dbImageDatas); |
300
|
|
|
|
301
|
|
|
// Prepare Gallery HTML |
302
|
|
|
$galleryHtml = $this->prepareGallery($images, $parameters); |
303
|
|
|
} else { |
304
|
|
|
$galleryHtml = "<div class='alert alert-warning' role='alert'>The directory specified exist but it doesn't contain images</div>"; |
305
|
|
|
} |
306
|
|
|
} else { |
307
|
|
|
$galleryHtml = "<div class='alert alert-warning' role='alert'>Image directory not found<br />".$parameters['images_dir'].'</div>'; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
// Replace the TOKEN found in the article with the generatd gallery HTML |
311
|
|
|
$postBody = str_replace($parameters['token'], $galleryHtml, $postBody); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $postBody; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|