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
|
|
|
|
14
|
|
|
if (!empty(request()->$fieldname) && request()->has($fieldname) || $custom['image']) { |
|
|
|
|
15
|
|
|
|
16
|
|
|
/* ------------------------------------------------------------------- */ |
17
|
|
|
|
18
|
|
|
$image_file = $custom['image'] ?? request()->file($fieldname); // Retriving Image File |
19
|
|
|
$filenamewithextension = $image_file->getClientOriginalName(); //Retriving Full Image Name |
|
|
|
|
20
|
|
|
$raw_filename = pathinfo($filenamewithextension, PATHINFO_FILENAME); //Retriving Image Raw Filename only |
21
|
|
|
$filename = str_replace('-', '', $raw_filename); // Retrive Filename |
22
|
|
|
$extension = $image_file->getClientOriginalExtension(); //Retriving Image extension |
23
|
|
|
$imageStoreNameOnly = $filename . "-" . time(); //Making Image Store name |
24
|
|
|
$imageStoreName = $filename . "-" . time() . "." . $extension; //Making Image Store name |
25
|
|
|
|
26
|
|
|
/* ------------------------------------------------------------------- */ |
27
|
|
|
|
28
|
|
|
/* ----------------------------------------Image Upload----------------------------------------- */ |
29
|
|
|
$img = $custom['image'] ?? request()->$fieldname; |
30
|
|
|
$this->update([ |
|
|
|
|
31
|
|
|
$fieldname => $img->storeAs($custom['storage'] ?? config("thumbnail.storage_path", "uploads"), $imageStoreName, 'public') |
32
|
|
|
]); |
33
|
|
|
/* --------------------------------------------------------------------------------------------- */ |
34
|
|
|
|
35
|
|
|
$image = Image::cache(function ($cached_img) use ($image_file, $custom) { |
36
|
|
|
return $cached_img->make($image_file->getRealPath())->fit($custom['width'] ?? config('thumbnail.img_width', 1000), $custom['height'] ?? config('thumbnail.img_height', 800)); |
37
|
|
|
}, config('thumbnail.image_cached_time', 10), true); |
38
|
|
|
$image->save(public_path('storage/' . $this->$fieldname), $custom['quality'] ?? config('thumbnail.image_quality', 80)); |
39
|
|
|
|
40
|
|
|
if (config('thumbnail.thumbnail', true)) { |
41
|
|
|
$thumbnails = false; |
|
|
|
|
42
|
|
|
$thumbnails = $custom['thumbnails'] ?? config('thumbnail.thumbnails', false) ?? false; |
43
|
|
|
$storage = $custom['storage'] ?? config('thumbnail.thumbnails_storage', false) ?? false; |
44
|
|
|
if ($thumbnails) { |
45
|
|
|
/* --------------------------------Custom Thumbnails------------------------------------------------- */ |
46
|
|
|
foreach ($thumbnails as $thumbnail) { |
47
|
|
|
$customthumbnail = $imageStoreNameOnly . '-' . $thumbnail['thumbnail-name'] . '.' . $extension; // Making Thumbnail Name |
48
|
|
|
$custom_thumbnail = $image_file->storeAs($storage ?? config("thumbnail.storage_path", "uploads"), $customthumbnail, 'public'); // Thumbnail Storage Information |
49
|
|
|
$make_custom_thumbnail = Image::cache(function ($cached_img) use ($image_file, $thumbnail) { |
50
|
|
|
return $cached_img->make($image_file->getRealPath())->fit($thumbnail['thumbnail-width'], $thumbnail['thumbnail-height']); |
51
|
|
|
}, config('thumbnail.image_cached_time', 10), true); //Storing Thumbnail |
52
|
|
|
$make_custom_thumbnail->save(public_path('storage/' . $custom_thumbnail), $thumbnail['thumbnail-quality']); //Storing Thumbnail |
|
|
|
|
53
|
|
|
} |
54
|
|
|
/* -------------------------------------------------------------------------------------------------- */ |
55
|
|
|
} else { |
56
|
|
|
/* --------------------- Thumbnail Info--------------------------------- */ |
57
|
|
|
//small thumbnail name |
58
|
|
|
$smallthumbnail = $imageStoreNameOnly . '-small' . '.' . $extension; // Making Thumbnail Name |
59
|
|
|
|
60
|
|
|
//medium thumbnail name |
61
|
|
|
$mediumthumbnail = $imageStoreNameOnly . '-medium' . '.' . $extension; // Making Thumbnail Name |
62
|
|
|
|
63
|
|
|
$small_thumbnail = $image_file->storeAs(config("thumbnail.storage_path", "uploads"), $smallthumbnail, 'public'); // Thumbnail Storage Information |
64
|
|
|
$medium_thumbnail = $image_file->storeAs(config("thumbnail.storage_path", "uploads"), $mediumthumbnail, 'public'); // Thumbnail Storage Information |
65
|
|
|
|
66
|
|
|
/* --------------------------------- Saving Thumbnail------------------------------------ */ |
67
|
|
|
|
68
|
|
|
$medium_img = Image::cache(function ($cached_img) use ($image_file) { |
69
|
|
|
return $cached_img->make($image_file->getRealPath())->fit(config('thumbnail.medium_thumbnail_width', 800), config('thumbnail.medium_thumbnail_height', 600)); //Storing Thumbnail |
70
|
|
|
}, config('thumbnail.image_cached_time', 10), true); |
71
|
|
|
|
72
|
|
|
$medium_img->save(public_path('storage/' . $medium_thumbnail), config('thumbnail.medium_thumbnail_quality', 60)); //Storing Thumbnail |
|
|
|
|
73
|
|
|
|
74
|
|
|
$small_img = Image::cache(function ($cached_img) use ($image_file) { |
75
|
|
|
return $cached_img->make($image_file->getRealPath())->fit(config('thumbnail.small_thumbnail_width', 400), config('thumbnail.small_thumbnail_height', 300)); //Storing Thumbnail |
76
|
|
|
}, config('thumbnail.image_cached_time', 10), true); |
77
|
|
|
|
78
|
|
|
$small_img->save(public_path('storage/' . $small_thumbnail), config('thumbnail.small_thumbnail_quality', 30)); //Storing Thumbnail |
|
|
|
|
79
|
|
|
|
80
|
|
|
/* ------------------------------------------------------------------------------------- */ |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Thumbnail Return |
87
|
|
|
public function thumbnail($fieldname = "image", $size) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return $this->imageDetail($fieldname = "image", $size)->path; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* Image Details */ |
93
|
|
|
public function imageDetail($fieldname = "image", $size = null) |
94
|
|
|
{ |
95
|
|
|
$image = $this->$fieldname; |
96
|
|
|
$path = explode("/", $image); |
97
|
|
|
$extension = \File::extension($image); |
98
|
|
|
$name = basename($image, "." . $extension); |
99
|
|
|
$image_fullname = isset($size) ? $name . "-" . (string) $size . "." . $extension : $name . "." . $extension; |
100
|
|
|
array_pop($path); |
101
|
|
|
$location = implode("/", $path); |
102
|
|
|
$path = "storage/" . $location . "/" . $image_fullname; |
103
|
|
|
$image_files = File::files(public_path('storage/' . $location)); |
104
|
|
|
$images_property = $this->imageProperty($image_files); |
105
|
|
|
$image_detail = array( |
106
|
|
|
'image' => $image, |
107
|
|
|
'name' => $name, |
108
|
|
|
'fullname' => $image_fullname, |
109
|
|
|
'extension' => $extension, |
110
|
|
|
'path' => $path, |
111
|
|
|
'directory' => public_path('storage/' . $location), |
112
|
|
|
'location' => public_path('storage/' . $image), |
113
|
|
|
'property' => $images_property |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
return json_decode(json_encode($image_detail)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/* Checking Image Existance */ |
121
|
|
|
public function imageExists($image) |
122
|
|
|
{ |
123
|
|
|
return file_exists(public_path('storage/' . $image)) ? true : false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// Checking Image's Thumbnail Existance |
127
|
|
|
public function hasThumbnail($image) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
// |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Image Property |
133
|
|
|
private function imageProperty($image_files) |
134
|
|
|
{ |
135
|
|
|
$images_property = array(); |
136
|
|
|
$thumbnails_property = array(); |
137
|
|
|
foreach ($image_files as $image) { |
138
|
|
|
$image_partition = explode('-', basename($image)); |
139
|
|
|
if (isset($image_partition[2])) { |
140
|
|
|
$thumbnails_property['image'] = $image->getFilename() ?? null; |
141
|
|
|
$thumbnails_property['real_name'] = isset($image_partition[0]) ? $image_partition[0] : null; |
142
|
|
|
$thumbnails_property['size'] = isset($image_partition[0]) ? $image->getSize() : null; |
143
|
|
|
$thumbnails_property['created_date'] = isset($image_partition[1]) ? Carbon::createFromFormat('Y/m/d H:i:s', date('Y/m/d H:i:s', $image_partition[1])) : null; |
|
|
|
|
144
|
|
|
$thumbnails_property['directory'] = isset($image_partition[0]) ? $image->getPath() : null; |
145
|
|
|
$thumbnails_property['location'] = isset($image_partition[0]) ? $image->getRealPath() : null; |
146
|
|
|
} else { |
147
|
|
|
$images_property['real_name'] = isset($image_partition[0]) ? $image_partition[0] : null; |
148
|
|
|
$images_property['size'] = isset($image_partition[0]) ? $image->getSize() : null; |
149
|
|
|
$images_property['directory'] = isset($image_partition[0]) ? $image->getPath() : null; |
150
|
|
|
$images_property['location'] = isset($image_partition[0]) ? $image->getRealPath() : null; |
151
|
|
|
} |
152
|
|
|
if (isset($image_partition[2])) { |
153
|
|
|
$images_property['thumbnails'] = $thumbnails_property; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
return $images_property; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|