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