|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace drh2so4\Thumbnail\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Support\Facades\File; |
|
7
|
|
|
use Intervention\Image\Facades\Image as Image; |
|
8
|
|
|
|
|
9
|
|
|
trait Thumbnail |
|
10
|
|
|
{ |
|
11
|
|
|
public function makeThumbnail($fieldname = 'image', $custom = []) |
|
12
|
|
|
{ |
|
13
|
|
|
if (!empty(request()->$fieldname) && request()->has($fieldname) || $custom['image']) { |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/* ------------------------------------------------------------------- */ |
|
16
|
|
|
|
|
17
|
|
|
$image_file = $custom['image'] ?? request()->file($fieldname); // Retriving Image File |
|
18
|
|
|
$filenamewithextension = $image_file->getClientOriginalName(); //Retriving Full Image Name |
|
|
|
|
|
|
19
|
|
|
$raw_filename = pathinfo($filenamewithextension, PATHINFO_FILENAME); //Retriving Image Raw Filename only |
|
20
|
|
|
$filename = str_replace('-', '', $raw_filename); // Retrive Filename |
|
21
|
|
|
$extension = $image_file->getClientOriginalExtension(); //Retriving Image extension |
|
22
|
|
|
$imageStoreNameOnly = $filename . '-' . time(); //Making Image Store name |
|
23
|
|
|
$imageStoreName = $filename . '-' . time() . '.' . $extension; //Making Image Store name |
|
24
|
|
|
|
|
25
|
|
|
/* ------------------------------------------------------------------- */ |
|
26
|
|
|
|
|
27
|
|
|
/* ----------------------------------------Image Upload----------------------------------------- */ |
|
28
|
|
|
$img = $custom['image'] ?? request()->$fieldname; |
|
29
|
|
|
$this->update([ |
|
|
|
|
|
|
30
|
|
|
$fieldname => $img->storeAs($custom['storage'] ?? config('thumbnail.storage_path', 'uploads'), $imageStoreName, 'public'), // Storing Parent Image |
|
31
|
|
|
]); |
|
32
|
|
|
/* --------------------------------------------------------------------------------------------- */ |
|
33
|
|
|
|
|
34
|
|
|
$image = Image::cache(function ($cached_img) use ($image_file, $custom) { |
|
35
|
|
|
return $cached_img->make($image_file->getRealPath())->fit($custom['width'] ?? config('thumbnail.img_width', 1000), $custom['height'] ?? config('thumbnail.img_height', 800)); //Parent Image Interventing |
|
36
|
|
|
}, config('thumbnail.image_cached_time', 10), true); |
|
37
|
|
|
$image->save(public_path('storage/' . $this->$fieldname), $custom['quality'] ?? config('thumbnail.image_quality', 80)); // Parent Image Locating Save |
|
38
|
|
|
|
|
39
|
|
|
if (config('thumbnail.thumbnail', true)) { |
|
40
|
|
|
$thumbnails = false; |
|
|
|
|
|
|
41
|
|
|
$thumbnails = $custom['thumbnails'] ?? config('thumbnail.thumbnails', false) ?? false; |
|
42
|
|
|
$storage = $custom['storage'] ?? config('thumbnail.storage_path', 'uploads') ?? false; |
|
43
|
|
|
if ($thumbnails) { |
|
44
|
|
|
/* -----------------------------------------Custom Thumbnails------------------------------------------------- */ |
|
45
|
|
|
$this->makeCustomThumbnails($image_file, $imageStoreNameOnly, $extension, $storage, $thumbnails); |
|
46
|
|
|
/* -------------------------------------------------------------------------------------------------- */ |
|
47
|
|
|
} else { |
|
48
|
|
|
/* ---------------------------------------Default Thumbnails--------------------------------------- */ |
|
49
|
|
|
$this->makeDefaultThumbnails($image_file, $extension, $imageStoreNameOnly); |
|
50
|
|
|
/* ------------------------------------------------------------------------------------------------ */ |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Make Image |
|
57
|
|
|
private function makeImg($image_file, $name, $location, $width, $height, $quality) |
|
58
|
|
|
{ |
|
59
|
|
|
$image = $image_file->storeAs($location, $name, 'public'); // Thumbnail Storage Information |
|
60
|
|
|
$img = Image::cache(function ($cached_img) use ($image_file, $width, $height) { |
|
61
|
|
|
return $cached_img->make($image_file->getRealPath())->fit($width, $height); |
|
62
|
|
|
}, config('thumbnail.image_cached_time', 10), true); //Storing Thumbnail |
|
63
|
|
|
$img->save(public_path('storage/' . $image), $quality); //Storing Thumbnail |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Make Custom Thumbnail |
|
67
|
|
|
private function makeCustomThumbnails($image_file, $imageStoreNameOnly, $extension, $storage, $thumbnails) |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($thumbnails as $thumbnail) { |
|
70
|
|
|
$customthumbnail = $imageStoreNameOnly . '-' . str_replace('-', '', $thumbnail['thumbnail-name']) . '.' . $extension; // Making Thumbnail Name |
|
71
|
|
|
$this->makeImg( |
|
72
|
|
|
$image_file, |
|
73
|
|
|
$customthumbnail, |
|
74
|
|
|
$storage, |
|
75
|
|
|
(int) $thumbnail['thumbnail-width'], |
|
76
|
|
|
(int) $thumbnail['thumbnail-height'], |
|
77
|
|
|
(int) $thumbnail['thumbnail-quality'] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Make Default Thumbnail |
|
83
|
|
|
private function makeDefaultThumbnails($image_file, $extension, $imageStoreNameOnly) |
|
84
|
|
|
{ |
|
85
|
|
|
/* --------------------- Thumbnail Info---------------------------------------- */ |
|
86
|
|
|
//small thumbnail name |
|
87
|
|
|
$smallthumbnail = $imageStoreNameOnly . '-small' . '.' . $extension; // Making Thumbnail Name |
|
88
|
|
|
|
|
89
|
|
|
//medium thumbnail name |
|
90
|
|
|
$mediumthumbnail = $imageStoreNameOnly . '-medium' . '.' . $extension; // Making Thumbnail Name |
|
91
|
|
|
|
|
92
|
|
|
// Medium Thumbnail |
|
93
|
|
|
$this->makeImg( |
|
94
|
|
|
$image_file, |
|
95
|
|
|
$mediumthumbnail, |
|
96
|
|
|
config('thumbnail.storage_path', 'uploads'), |
|
97
|
|
|
config('thumbnail.medium_thumbnail_width', 800), |
|
98
|
|
|
config('thumbnail.medium_thumbnail_height', 600), |
|
99
|
|
|
config('thumbnail.medium_thumbnail_quality', 60) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
// Small Thumbnail |
|
103
|
|
|
$this->makeImg( |
|
104
|
|
|
$image_file, |
|
105
|
|
|
$smallthumbnail, |
|
106
|
|
|
config('thumbnail.storage_path', 'uploads'), |
|
107
|
|
|
config('thumbnail.small_thumbnail_width', 800), |
|
108
|
|
|
config('thumbnail.small_thumbnail_height', 600), |
|
109
|
|
|
config('thumbnail.small_thumbnail_quality', 60) |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
/* ------------------------------------------------------------------------------------- */ |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Thumbnail Path |
|
116
|
|
|
public function thumbnail($fieldname = 'image', $size = null) |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->imageDetail($fieldname, $size)->path; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/* Checking Image Existance */ |
|
122
|
|
|
private function imageExists($image) |
|
123
|
|
|
{ |
|
124
|
|
|
return file_exists($image->getRealPath()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Checking Image's Thumbnail Existance |
|
128
|
|
|
public function hasThumbnail($fieldname = 'image', $size = null) |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->imageDetail($fieldname, $size)->property->has_thumbnail; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Thumbnail Count |
|
134
|
|
|
public function thumbnailCount($fieldname = 'image', $size = null) |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->hasThumbnail($fieldname, $size) ? $this->imageDetail($fieldname, $size)->property->thumbnail_count : 0; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/* Image Details */ |
|
140
|
|
|
public function imageDetail($fieldname = 'image', $size = null) |
|
141
|
|
|
{ |
|
142
|
|
|
$image = $this->$fieldname; |
|
143
|
|
|
$path = explode('/', $image); |
|
144
|
|
|
$extension = \File::extension($image); |
|
145
|
|
|
$name = basename($image, '.' . $extension); |
|
146
|
|
|
$image_fullname = isset($size) ? $name . '-' . (string) $size . '.' . $extension : $name . '.' . $extension; |
|
147
|
|
|
array_pop($path); |
|
148
|
|
|
$location = implode('/', $path); |
|
149
|
|
|
$path = 'storage/' . $location . '/' . $image_fullname; |
|
150
|
|
|
$image_files = File::files(public_path('storage/' . $location)); |
|
151
|
|
|
$images_property = $this->imageProperty($image_files); |
|
152
|
|
|
$image_detail = [ |
|
153
|
|
|
'image' => $image, |
|
154
|
|
|
'name' => $name, |
|
155
|
|
|
'fullname' => $image_fullname, |
|
156
|
|
|
'extension' => $extension, |
|
157
|
|
|
'path' => $path, |
|
158
|
|
|
'directory' => public_path('storage/' . $location), |
|
159
|
|
|
'location' => public_path('storage/' . $image), |
|
160
|
|
|
'property' => $images_property, |
|
161
|
|
|
]; |
|
162
|
|
|
|
|
163
|
|
|
return json_decode(json_encode($image_detail)); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// Image Property |
|
167
|
|
|
private function imageProperty($image_files) |
|
168
|
|
|
{ |
|
169
|
|
|
$thumbnail_count = 0; |
|
170
|
|
|
$images_property = []; |
|
171
|
|
|
$thumbnails_property = []; |
|
172
|
|
|
foreach ($image_files as $image) { |
|
173
|
|
|
$image_partition = explode('-', basename($image)); |
|
174
|
|
|
if (isset($image_partition[2])) { |
|
175
|
|
|
$thumbnails_property['image'] = $image->getFilename() ?? null; |
|
176
|
|
|
$thumbnails_property['real_name'] = $image_partition[0]; |
|
177
|
|
|
$thumbnails_property['size'] = $image->getSize(); |
|
178
|
|
|
$thumbnails_property['created_date'] = isset($image_partition[1]) ? Carbon::createFromFormat('Y/m/d H:i:s', date('Y/m/d H:i:s', (int) $image_partition[1])) : null; |
|
179
|
|
|
$thumbnails_property['directory'] = $image->getPath(); |
|
180
|
|
|
$thumbnails_property['location'] = $image->getRealPath(); |
|
181
|
|
|
$thumbnail_exists = $this->imageExists($image); |
|
182
|
|
|
$images_property['has_thumbnail'] = $thumbnail_exists || $this->imageExists($image); |
|
183
|
|
|
$images_property['thumbnail_count'] = $thumbnail_count + 1; |
|
184
|
|
|
} else { |
|
185
|
|
|
$images_property['real_name'] = $image_partition[0]; |
|
186
|
|
|
$images_property['size'] = $image->getSize(); |
|
187
|
|
|
$images_property['directory'] = $image->getPath(); |
|
188
|
|
|
$images_property['location'] = $image->getRealPath(); |
|
189
|
|
|
} |
|
190
|
|
|
if (isset($image_partition[2])) { |
|
191
|
|
|
$images_property['thumbnails'] = $thumbnails_property; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $images_property; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|