1 | <?php |
||
2 | |||
3 | namespace DavideCasiraghi\LaravelEventsCalendar; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use DateTime; |
||
7 | use DavideCasiraghi\LaravelEventsCalendar\Models\Event; |
||
8 | use DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition; |
||
9 | use Illuminate\Support\Facades\Storage; |
||
10 | use Intervention\Image\ImageManagerStatic as Image; |
||
11 | |||
12 | class LaravelEventsCalendar |
||
13 | { |
||
14 | /***************************************************************************/ |
||
15 | |||
16 | /** |
||
17 | * Format a date from datepicker (d/m/Y) to a format ready to be stored on DB (Y-m-d). |
||
18 | * If the date picker date is null return today's date. |
||
19 | * the PARAM is a date in the d/m/Y format - the RETURN is a date in the Y-m-d format. |
||
20 | * If $todaysDateIfNull = 1, when the date is null return the date of today. |
||
21 | * |
||
22 | * @param string $DatePickerDate |
||
23 | * @param bool $todaysDateIfNull |
||
24 | * @return string $ret |
||
25 | */ |
||
26 | 1 | public static function formatDatePickerDateForMysql($DatePickerDate, bool $todaysDateIfNull = null) |
|
27 | { |
||
28 | 1 | if ($DatePickerDate) { |
|
29 | 1 | [$tid, $tim, $tiy] = explode('/', $DatePickerDate); |
|
30 | 1 | $ret = "$tiy-$tim-$tid"; |
|
31 | 1 | } elseif ($todaysDateIfNull) { |
|
32 | 1 | date_default_timezone_set('Europe/Rome'); |
|
33 | 1 | $ret = date('Y-m-d', time()); |
|
34 | } else { |
||
35 | 1 | $ret = ''; |
|
36 | } |
||
37 | |||
38 | 1 | return $ret; |
|
39 | } |
||
40 | |||
41 | /***************************************************************************/ |
||
42 | |||
43 | /** |
||
44 | * It returns a string that is composed by the array values separated by a comma. |
||
45 | * |
||
46 | * @param iterable $items |
||
47 | * @return string $ret |
||
48 | */ |
||
49 | 4 | public static function getStringFromArraySeparatedByComma(iterable $items) |
|
50 | { |
||
51 | 4 | $ret = ''; |
|
52 | 4 | $i = 0; |
|
53 | 4 | $len = count($items); // to put "," to all items except the last |
|
54 | |||
55 | 4 | foreach ($items as $key => $item) { |
|
56 | 4 | $ret .= $item; |
|
57 | 4 | if ($i != $len - 1) { // not last |
|
58 | 2 | $ret .= ', '; |
|
59 | } |
||
60 | 4 | $i++; |
|
61 | } |
||
62 | |||
63 | 4 | return $ret; |
|
64 | } |
||
65 | |||
66 | /***************************************************************************/ |
||
67 | |||
68 | /** |
||
69 | * Check the date and return true if the weekday is the one specified in $dayOfTheWeek. eg. if $dayOfTheWeek = 3, is true if the date is a Wednesday |
||
70 | * $dayOfTheWeek: 1|2|3|4|5|6|7 (MONDAY-SUNDAY) |
||
71 | * https://stackoverflow.com/questions/2045736/getting-all-dates-for-mondays-and-tuesdays-for-the-next-year. |
||
72 | * |
||
73 | * @param string $date |
||
74 | * @param int $dayOfTheWeek |
||
75 | * @return bool |
||
76 | */ |
||
77 | 4 | public static function isWeekDay(string $date, int $dayOfTheWeek) |
|
78 | { |
||
79 | // Fix the bug that was avoiding to save Sunday. Date 'w' identify sunday as 0 and not 7. |
||
80 | 4 | if ($dayOfTheWeek == 7) { |
|
81 | 1 | $dayOfTheWeek = 0; |
|
82 | } |
||
83 | |||
84 | 4 | return date('w', strtotime($date)) == $dayOfTheWeek; |
|
85 | } |
||
86 | |||
87 | /***************************************************************************/ |
||
88 | |||
89 | /** |
||
90 | * GET number of the specified weekday in this month (1 for the first). |
||
91 | * $dateTimestamp - unix timestramp of the date specified |
||
92 | * $dayOfWeekValue - 1 (for Monday) through 7 (for Sunday) |
||
93 | * Return the number of the week in the month of the weekday specified. |
||
94 | * @param string $dateTimestamp |
||
95 | * @param string $dayOfWeekValue |
||
96 | * @return int |
||
97 | */ |
||
98 | 2 | public static function weekdayNumberOfMonth(string $dateTimestamp, string $dayOfWeekValue) |
|
99 | { |
||
100 | 2 | $cut = substr($dateTimestamp, 0, 8); |
|
101 | 2 | $daylen = 86400; |
|
102 | 2 | $timestamp = strtotime($dateTimestamp); |
|
103 | 2 | $first = strtotime($cut.'01'); |
|
104 | 2 | $elapsed = (($timestamp - $first) / $daylen) + 1; |
|
105 | 2 | $i = 1; |
|
106 | 2 | $weeks = 0; |
|
107 | 2 | for ($i == 1; $i <= $elapsed; $i++) { |
|
108 | 2 | $dayfind = $cut.(strlen((string) $i) < 2 ? '0'.$i : $i); |
|
109 | 2 | $daytimestamp = strtotime($dayfind); |
|
110 | 2 | $day = strtolower(date('N', $daytimestamp)); |
|
111 | 2 | if ($day == strtolower($dayOfWeekValue)) { |
|
112 | 1 | $weeks++; |
|
113 | } |
||
114 | } |
||
115 | 2 | if ($weeks == 0) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
116 | 1 | $weeks++; |
|
117 | } |
||
118 | |||
119 | 2 | return $weeks; |
|
120 | } |
||
121 | |||
122 | /***************************************************************************/ |
||
123 | |||
124 | /** |
||
125 | * GET number of week from the end of the month - https://stackoverflow.com/questions/5853380/php-get-number-of-week-for-month |
||
126 | * Week of the month = Week of the year - Week of the year of first day of month + 1. |
||
127 | * Return the number of the week in the month of the day specified |
||
128 | * $when - unix timestramp of the date specified. |
||
129 | * |
||
130 | * @param int $when |
||
131 | * @return int |
||
132 | */ |
||
133 | 2 | public static function weekOfMonthFromTheEnd(int $when) |
|
134 | { |
||
135 | 2 | $numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31 |
|
136 | 2 | $lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date |
|
137 | 2 | $dayDifference = (int) $lastDayOfMonth - (int) $numberOfDayOfTheMonth; |
|
138 | |||
139 | switch (true) { |
||
140 | 2 | case $dayDifference < 7: |
|
141 | 1 | $weekFromTheEnd = 1; |
|
142 | 1 | break; |
|
143 | |||
144 | 2 | case $dayDifference < 14: |
|
145 | 1 | $weekFromTheEnd = 2; |
|
146 | 1 | break; |
|
147 | |||
148 | 2 | case $dayDifference < 21: |
|
149 | 2 | $weekFromTheEnd = 3; |
|
150 | 2 | break; |
|
151 | |||
152 | 1 | case $dayDifference < 28: |
|
153 | 1 | $weekFromTheEnd = 4; |
|
154 | 1 | break; |
|
155 | |||
156 | default: |
||
157 | 1 | $weekFromTheEnd = 5; |
|
158 | 1 | break; |
|
159 | } |
||
160 | |||
161 | 2 | return $weekFromTheEnd; |
|
162 | } |
||
163 | |||
164 | /***************************************************************************/ |
||
165 | |||
166 | /** |
||
167 | * GET number of day from the end of the month. |
||
168 | * $when - unix timestramp of the date specified |
||
169 | * Return the number of day of the month from end. |
||
170 | * |
||
171 | * @param int $when |
||
172 | * @return int |
||
173 | */ |
||
174 | 2 | public static function dayOfMonthFromTheEnd(int $when) |
|
175 | { |
||
176 | 2 | $numberOfDayOfTheMonth = strftime('%e', $when); // Day of the month 1-31 |
|
177 | 2 | $lastDayOfMonth = strftime('%e', strtotime(date('Y-m-t', $when))); // the last day of the month of the specified date |
|
178 | 2 | $dayDifference = (int) $lastDayOfMonth - (int) $numberOfDayOfTheMonth; |
|
179 | |||
180 | 2 | return $dayDifference; |
|
181 | } |
||
182 | |||
183 | /***************************************************************************/ |
||
184 | |||
185 | /** |
||
186 | * Decode the event repeat_weekly_on field - used in event.show. |
||
187 | * Return a string like "Monday". |
||
188 | * |
||
189 | * @param string $repeatWeeklyOn |
||
190 | * @return string |
||
191 | */ |
||
192 | 2 | public static function decodeRepeatWeeklyOn(string $repeatWeeklyOn) |
|
193 | { |
||
194 | $weekdayArray = [ |
||
195 | 2 | '', |
|
196 | 2 | __('laravel-events-calendar::general.monday'), |
|
197 | 2 | __('laravel-events-calendar::general.tuesday'), |
|
198 | 2 | __('laravel-events-calendar::general.wednesday'), |
|
199 | 2 | __('laravel-events-calendar::general.thursday'), |
|
200 | 2 | __('laravel-events-calendar::general.friday'), |
|
201 | 2 | __('laravel-events-calendar::general.saturday'), |
|
202 | 2 | __('laravel-events-calendar::general.sunday'), |
|
203 | ]; |
||
204 | 2 | $ret = $weekdayArray[$repeatWeeklyOn]; |
|
205 | |||
206 | 2 | return $ret; |
|
207 | } |
||
208 | |||
209 | /***************************************************************************/ |
||
210 | |||
211 | /** |
||
212 | * Decode the event on_monthly_kind field - used in event.show. |
||
213 | * Return a string like "the 4th to last Thursday of the month". |
||
214 | * |
||
215 | * @param string $onMonthlyKindCode |
||
216 | * @return string |
||
217 | */ |
||
218 | 2 | public static function decodeOnMonthlyKind(string $onMonthlyKindCode) |
|
219 | { |
||
220 | 2 | $ret = ''; |
|
221 | 2 | $onMonthlyKindCodeArray = explode('|', $onMonthlyKindCode); |
|
222 | $weekDays = [ |
||
223 | 2 | '', |
|
224 | 2 | __('laravel-events-calendar::general.monday'), |
|
225 | 2 | __('laravel-events-calendar::general.tuesday'), |
|
226 | 2 | __('laravel-events-calendar::general.wednesday'), |
|
227 | 2 | __('laravel-events-calendar::general.thursday'), |
|
228 | 2 | __('laravel-events-calendar::general.friday'), |
|
229 | 2 | __('laravel-events-calendar::general.saturday'), |
|
230 | 2 | __('laravel-events-calendar::general.sunday'), |
|
231 | ]; |
||
232 | |||
233 | //dd($onMonthlyKindCodeArray); |
||
234 | 2 | switch ($onMonthlyKindCodeArray[0]) { |
|
235 | 2 | case '0': // 0|7 eg. the 7th day of the month |
|
236 | 2 | $dayNumber = $onMonthlyKindCodeArray[1]; |
|
237 | 2 | $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_x_of_the_month'); |
|
238 | 2 | $ret = sprintf($format, __('laravel-events-calendar::general.day')); |
|
239 | 2 | break; |
|
240 | 1 | case '1': // 1|2|4 eg. the 2nd Thursday of the month |
|
241 | 1 | $dayNumber = $onMonthlyKindCodeArray[1]; |
|
242 | 1 | $weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday |
|
243 | 1 | $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_x_of_the_month'); |
|
244 | 1 | $ret = sprintf($format, $weekDay); |
|
245 | 1 | break; |
|
246 | 1 | case '2': // 2|20 eg. the 21st to last day of the month |
|
247 | 1 | $dayNumber = (int) $onMonthlyKindCodeArray[1] + 1; |
|
248 | 1 | $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_to_last_x_of_the_month'); |
|
249 | 1 | $ret = sprintf($format, __('laravel-events-calendar::general.day')); |
|
250 | 1 | break; |
|
251 | 1 | case '3': // 3|3|4 eg. the 4th to last Thursday of the month |
|
252 | 1 | $dayNumber = (int) $onMonthlyKindCodeArray[1] + 1; |
|
253 | 1 | $weekDay = $weekDays[$onMonthlyKindCodeArray[2]]; // Monday, Tuesday, Wednesday |
|
254 | 1 | $format = __('laravel-events-calendar::ordinalDays.the_'.($dayNumber).'_to_last_x_of_the_month'); |
|
255 | 1 | $ret = sprintf($format, $weekDay); |
|
256 | 1 | break; |
|
257 | } |
||
258 | |||
259 | 2 | return $ret; |
|
260 | } |
||
261 | |||
262 | /***************************************************************************/ |
||
263 | |||
264 | /** |
||
265 | * Return the GPS coordinates of the venue |
||
266 | * https://developer.mapquest.com/. |
||
267 | * |
||
268 | * @param string $address |
||
269 | * @return array $ret |
||
270 | */ |
||
271 | 6 | public static function getVenueGpsCoordinates(string $address) |
|
272 | { |
||
273 | 6 | $address = self::cleanString($address); |
|
274 | 6 | $key = 'Ad5KVnAISxX6aHyj6fAnHcKeh30n4W60'; |
|
275 | 6 | $url = 'https://www.mapquestapi.com/geocoding/v1/address?key='.$key.'&location='.$address; |
|
276 | 6 | $response = @file_get_contents($url); |
|
277 | |||
278 | 6 | if (! $response) { |
|
279 | $url = 'http://open.mapquestapi.com/geocoding/v1/address?key='.$key.'&location='.$address; |
||
280 | $response = @file_get_contents($url); |
||
281 | } |
||
282 | |||
283 | 6 | $response = json_decode($response, true); |
|
284 | |||
285 | 6 | $ret = []; |
|
286 | 6 | $ret['lat'] = $response['results'][0]['locations'][0]['latLng']['lat']; |
|
287 | 6 | $ret['lng'] = $response['results'][0]['locations'][0]['latLng']['lng']; |
|
288 | |||
289 | 6 | return $ret; |
|
290 | } |
||
291 | |||
292 | /***************************************************************************/ |
||
293 | |||
294 | /** |
||
295 | * Return a string with the list of the collection id separated by comma. |
||
296 | * without any space. eg. "354,320,310". |
||
297 | * |
||
298 | * @param iterable $items |
||
299 | * @return string $ret |
||
300 | */ |
||
301 | 2 | public static function getCollectionIdsSeparatedByComma(iterable $items) |
|
302 | { |
||
303 | 2 | $itemsIds = []; |
|
304 | 2 | foreach ($items as $item) { |
|
305 | 1 | array_push($itemsIds, $item->id); |
|
306 | } |
||
307 | 2 | $ret = implode(',', $itemsIds); |
|
308 | |||
309 | 2 | return $ret; |
|
310 | } |
||
311 | |||
312 | /***************************************************************************/ |
||
313 | |||
314 | /** |
||
315 | * Return a string that describe the report misuse reason. |
||
316 | * |
||
317 | * @param int $reason |
||
318 | * @return string $ret |
||
319 | */ |
||
320 | 1 | public static function getReportMisuseReasonDescription(int $reason) |
|
321 | { |
||
322 | 1 | $ret = ''; |
|
323 | switch ($reason) { |
||
324 | 1 | case '1': |
|
325 | 1 | $ret = 'Not about Contact Improvisation'; |
|
326 | 1 | break; |
|
327 | 1 | case '2': |
|
328 | 1 | $ret = 'Contains wrong informations'; |
|
329 | 1 | break; |
|
330 | 1 | case '3': |
|
331 | 1 | $ret = 'It is not translated in english'; |
|
332 | 1 | break; |
|
333 | 1 | case '4': |
|
334 | 1 | $ret = 'Other (specify in the message)'; |
|
335 | 1 | break; |
|
336 | } |
||
337 | |||
338 | 1 | return $ret; |
|
339 | } |
||
340 | |||
341 | /***************************************************************************/ |
||
342 | |||
343 | /** |
||
344 | * Return a string that describe repetition kind in the event show view. |
||
345 | * |
||
346 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\Event $event |
||
347 | * @param \DavideCasiraghi\LaravelEventsCalendar\Models\EventRepetition $firstRpDates |
||
348 | * @return string $ret |
||
349 | */ |
||
350 | 5 | public static function getRepetitionTextString(Event $event, EventRepetition $firstRpDates) |
|
351 | { |
||
352 | 5 | $ret = ''; |
|
353 | |||
354 | 5 | switch ($event->repeat_type) { |
|
355 | 5 | case '1': // noRepeat |
|
356 | 2 | break; |
|
357 | 3 | case '2': // repeatWeekly |
|
358 | 1 | $repeatUntil = new DateTime($event->repeat_until); |
|
359 | |||
360 | // Get the name of the weekly day when the event repeat, if two days, return like "Thursday and Sunday" |
||
361 | 1 | $repetitonWeekdayNumbersArray = explode(',', $event->repeat_weekly_on); |
|
362 | 1 | $repetitonWeekdayNamesArray = []; |
|
363 | 1 | foreach ($repetitonWeekdayNumbersArray as $key => $repetitonWeekdayNumber) { |
|
364 | 1 | $repetitonWeekdayNamesArray[] = self::decodeRepeatWeeklyOn($repetitonWeekdayNumber); |
|
365 | } |
||
366 | // create from an array a string with all the values divided by " and " |
||
367 | 1 | $nameOfTheRepetitionWeekDays = implode(' and ', $repetitonWeekdayNamesArray); |
|
368 | |||
369 | //$ret = 'The event happens every '.$nameOfTheRepetitionWeekDays.' until '.$repeatUntil->format('d/m/Y'); |
||
370 | 1 | $format = __('laravel-events-calendar::event.the_event_happens_every_x_until_x'); |
|
371 | 1 | $ret .= sprintf($format, $nameOfTheRepetitionWeekDays, $repeatUntil->format('d/m/Y')); |
|
372 | 1 | break; |
|
373 | 2 | case '3': //repeatMonthly |
|
374 | 1 | $repeatUntil = new DateTime($event->repeat_until); |
|
375 | 1 | $repetitionFrequency = self::decodeOnMonthlyKind($event->on_monthly_kind); |
|
376 | |||
377 | //$ret = 'The event happens '.$repetitionFrequency.' until '.$repeatUntil->format('d/m/Y'); |
||
378 | 1 | $format = __('laravel-events-calendar::event.the_event_happens_x_until_x'); |
|
379 | 1 | $ret .= sprintf($format, $repetitionFrequency, $repeatUntil->format('d/m/Y')); |
|
380 | 1 | break; |
|
381 | 1 | case '4': //repeatMultipleDays |
|
382 | 1 | $dateStart = date('d/m/Y', strtotime($firstRpDates->start_repeat)); |
|
383 | 1 | $singleDaysRepeatDatas = explode(',', $event->multiple_dates); |
|
384 | |||
385 | // Sort the datas |
||
386 | usort($singleDaysRepeatDatas, function ($a, $b) { |
||
387 | 1 | $a = Carbon::createFromFormat('d/m/Y', $a); |
|
388 | 1 | $b = Carbon::createFromFormat('d/m/Y', $b); |
|
389 | |||
390 | 1 | return strtotime($a) - strtotime($b); |
|
391 | 1 | }); |
|
392 | |||
393 | 1 | $ret .= __('laravel-events-calendar::event.the_event_happens_on_this_dates'); |
|
394 | 1 | $ret .= $dateStart.', '; |
|
395 | 1 | $ret .= self::getStringFromArraySeparatedByComma($singleDaysRepeatDatas); |
|
396 | 1 | break; |
|
397 | } |
||
398 | |||
399 | 5 | return $ret; |
|
400 | } |
||
401 | |||
402 | /***************************************************************************/ |
||
403 | |||
404 | /** |
||
405 | * Return the map marker icon color. |
||
406 | * |
||
407 | * @param int $eventCategoryId |
||
408 | * @return string $ret |
||
409 | */ |
||
410 | 3 | public static function getMapMarkerIconColor(int $eventCategoryId) |
|
411 | { |
||
412 | 3 | $ret = ''; |
|
413 | 3 | switch ($eventCategoryId) { |
|
414 | 3 | case 1: //Regular Jam |
|
415 | $ret = 'greenIcon'; |
||
416 | break; |
||
417 | 3 | case 10: // Special Jam |
|
418 | $ret = 'yellowIcon'; |
||
419 | break; |
||
420 | 3 | case 2: // Class |
|
421 | $ret = 'goldIcon'; |
||
422 | break; |
||
423 | 3 | case 3: // Workshop |
|
424 | 1 | $ret = 'orangeIcon'; |
|
425 | 1 | break; |
|
426 | 2 | case 6: // Festival |
|
427 | case 16: // Camp / Journey |
||
428 | 2 | $ret = 'redIcon'; |
|
429 | 2 | break; |
|
430 | case 11: // Underscore |
||
431 | $ret = 'blueIcon'; |
||
432 | break; |
||
433 | case 13: // Performance |
||
434 | case 14: // Lecture / Conference / Film |
||
435 | case 17: // Other event |
||
436 | $ret = 'violetIcon'; |
||
437 | break; |
||
438 | case 15: // Lab |
||
439 | $ret = 'greyIcon'; |
||
440 | break; |
||
441 | case 12: // Teachers Meeting |
||
442 | $ret = 'blackIcon'; |
||
443 | break; |
||
444 | } |
||
445 | |||
446 | 3 | return $ret; |
|
447 | } |
||
448 | |||
449 | /***************************************************************************/ |
||
450 | |||
451 | /** |
||
452 | * Remove all special characters from a string - Remove all special characters from a string. |
||
453 | * |
||
454 | * @param string $text |
||
455 | * @return string $ret |
||
456 | */ |
||
457 | 7 | public static function cleanString(string $text) |
|
458 | { |
||
459 | // Transform whitespaces to %20 for the URL |
||
460 | 7 | $text = str_replace(' ', '%20', $text); |
|
461 | 7 | $text = str_replace('ß', '%DF', $text); |
|
462 | |||
463 | $utf8 = [ |
||
464 | 7 | '/[áàâãªä]/u' => 'a', |
|
465 | '/[ÁÀÂÃÄ]/u' => 'A', |
||
466 | '/[ÍÌÎÏİ]/u' => 'I', |
||
467 | '/[íìîïı]/u' => 'i', |
||
468 | '/[éèêëěė]/u' => 'e', |
||
469 | '/[ÉÈÊË]/u' => 'E', |
||
470 | '/[óòôõºöø]/u' => 'o', |
||
471 | '/[ÓÒÔÕÖ]/u' => 'O', |
||
472 | '/[úùûüüū]/u' => 'u', |
||
473 | '/[ÚÙÛÜ]/u' => 'U', |
||
474 | '/[çć]/u' => 'c', |
||
475 | '/Ç/' => 'C', |
||
476 | '/ğ/' => 'g', |
||
477 | '/ñ/' => 'n', |
||
478 | '/Ñ/' => 'N', |
||
479 | '/ř/' => 'r', |
||
480 | '/[šş]/u' => 's', |
||
481 | '/–/' => '-', // UTF-8 hyphen to "normal" hyphen |
||
482 | '/[’‘‹›‚]/u' => ' ', // Literally a single quote |
||
483 | '/[“”«»„]/u' => ' ', // Double quote |
||
484 | '/ /' => ' ', // nonbreaking space (equiv. to 0x160) |
||
485 | ]; |
||
486 | 7 | $ret = preg_replace(array_keys($utf8), array_values($utf8), $text); |
|
487 | |||
488 | 7 | return $ret; |
|
489 | } |
||
490 | |||
491 | // ********************************************************************** |
||
492 | |||
493 | /** |
||
494 | * Upload image on server. |
||
495 | * $imageFile - the file to upload |
||
496 | * $imageSubdir is the subdir in /storage/app/public/images/.. |
||
497 | * |
||
498 | * @param mixed $imageFile |
||
499 | * @param string $imageName |
||
500 | * @param string $imageSubdir |
||
501 | * @param int $imageWidth |
||
502 | * @param int $thumbWidth |
||
503 | * @return void |
||
504 | */ |
||
505 | 1 | public static function uploadImageOnServer($imageFile, string $imageName, string $imageSubdir, int $imageWidth, int $thumbWidth): void |
|
506 | { |
||
507 | |||
508 | // Create dir if not exist (in /storage/app/public/images/..) |
||
509 | 1 | if (! Storage::disk('public')->has('images/'.$imageSubdir.'/')) { |
|
510 | 1 | Storage::disk('public')->makeDirectory('images/'.$imageSubdir.'/'); |
|
511 | } |
||
512 | |||
513 | 1 | $destinationPath = 'app/public/images/'.$imageSubdir.'/'; |
|
514 | |||
515 | // Resize the image with Intervention - http://image.intervention.io/api/resize |
||
516 | // - resize and store the image to a width of 300 and constrain aspect ratio (auto height) |
||
517 | // - save file as jpg with medium quality |
||
518 | 1 | $image = Image::make($imageFile->getRealPath()) |
|
519 | 1 | ->resize($imageWidth, null, |
|
520 | function ($constraint) { |
||
521 | 1 | $constraint->aspectRatio(); |
|
522 | 1 | }) |
|
523 | 1 | ->save(storage_path($destinationPath.$imageName), 75); |
|
524 | |||
525 | // Create the thumb |
||
526 | 1 | $image->resize($thumbWidth, null, |
|
527 | function ($constraint) { |
||
528 | 1 | $constraint->aspectRatio(); |
|
529 | 1 | }) |
|
530 | 1 | ->save(storage_path($destinationPath.'thumb_'.$imageName), 75); |
|
531 | 1 | } |
|
532 | } |
||
533 |