Passed
Push — master ( 066072...22cc5e )
by PRATIK
02:32
created

Thumbnail::imageProperty()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 36
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 7
eloc 29
c 3
b 1
f 0
nc 8
nop 2
dl 0
loc 36
rs 8.5226
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) || $custom['image']) {
14
            /* ------------------------------------------------------------------- */
15
16
            $image_file = $custom['image'] ?? request()->file($fieldname); // Retriving Image File   
17
            $extension = $this->image_info($image_file)->extension; //Retriving Image extension
18
            $imageStoreNameOnly = $this->image_info($image_file)->imageStoreNameOnly; //Making Image Store name
19
20
21
            /* ------------------------------------------------------------------- */
22
23
            /* ----------------------------------------Parent Image Upload----------------------------------------- */
24
            $this->uploadImage($fieldname, $custom); // Upload Parent Image
25
            /* --------------------------------------------------------------------------------------------- */
26
            if (config('thumbnail.thumbnail', true)) {
27
                $thumbnails = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $thumbnails is dead and can be removed.
Loading history...
28
                $thumbnails = $custom['thumbnails'] ?? config('thumbnail.thumbnails') ?? false; // Grab Thumbnails
29
                $storage = $custom['storage'] ?? config('thumbnail.storage_path', 'uploads') ?? false; // Grab Storage Info
30
                if ($thumbnails) {
31
                    /* -----------------------------------------Custom Thumbnails------------------------------------------------- */
32
                    $this->makeCustomThumbnails($image_file, $imageStoreNameOnly, $extension, $storage, $thumbnails);
33
                    /* -------------------------------------------------------------------------------------------------- */
34
                } else {
35
                    /* ---------------------------------------Default Thumbnails--------------------------------------- */
36
                    $this->makeDefaultThumbnails($image_file, $extension, $imageStoreNameOnly);
37
                    /* ------------------------------------------------------------------------------------------------ */
38
                }
39
            }
40
        }
41
    }
42
43
    // Make Image
44
    private function makeImg($image_file, $name, $location, $width, $height, $quality)
45
    {
46
        $image = $image_file->storeAs($location, $name, 'public'); // Thumbnail Storage Information
47
        $img = Image::cache(function ($cached_img) use ($image_file, $width, $height) {
48
            return $cached_img->make($image_file->getRealPath())->fit($width, $height);
49
        }, config('thumbnail.image_cached_time', 10), true); //Storing Thumbnail
50
        $img->save(public_path('storage/' . $image), $quality); //Storing Thumbnail
51
    }
52
53
    // Make Custom Thumbnail
54
    private function makeCustomThumbnails($image_file, $imageStoreNameOnly, $extension, $storage, $thumbnails)
55
    {
56
        foreach ($thumbnails as $thumbnail) {
57
            $customthumbnail = $imageStoreNameOnly . '-' . str_replace('-', '', $thumbnail['thumbnail-name']) . '.' . $extension; // Making Thumbnail Name
58
            $this->makeImg(
59
                $image_file,
60
                $customthumbnail,
61
                $storage,
62
                (int) $thumbnail['thumbnail-width'],
63
                (int) $thumbnail['thumbnail-height'],
64
                (int) $thumbnail['thumbnail-quality']
65
            );
66
        }
67
    }
68
69
    // Make Default Thumbnail
70
    private function makeDefaultThumbnails($image_file, $extension, $imageStoreNameOnly)
71
    {
72
        /* --------------------- Thumbnail Info---------------------------------------- */
73
        //small thumbnail name
74
        $smallthumbnail = $imageStoreNameOnly . '-small' . '.' . $extension; // Making Thumbnail Name
75
76
        //medium thumbnail name
77
        $mediumthumbnail = $imageStoreNameOnly . '-medium' . '.' . $extension; // Making Thumbnail Name
78
79
        // Medium Thumbnail
80
        $this->makeImg(
81
            $image_file,
82
            $mediumthumbnail,
83
            config('thumbnail.storage_path', 'uploads'),
84
            config('thumbnail.medium_thumbnail_width', 800),
85
            config('thumbnail.medium_thumbnail_height', 600),
86
            config('thumbnail.medium_thumbnail_quality', 60)
87
        );
88
89
        // Small Thumbnail
90
        $this->makeImg(
91
            $image_file,
92
            $smallthumbnail,
93
            config('thumbnail.storage_path', 'uploads'),
94
            config('thumbnail.small_thumbnail_width', 800),
95
            config('thumbnail.small_thumbnail_height', 600),
96
            config('thumbnail.small_thumbnail_quality', 60)
97
        );
98
99
        /* ------------------------------------------------------------------------------------- */
100
    }
101
102
    /* Image Upload Process Info */
103
    private function image_info($image_file)
104
    {
105
        $filenamewithextension = $image_file->getClientOriginalName(); //Retriving Full Image Name
106
        $raw_filename = pathinfo($filenamewithextension, PATHINFO_FILENAME); //Retriving Image Raw Filename only
107
        $filename = str_replace(['-', ' '], '', $raw_filename); // Retrive Filename
108
        $extension = $image_file->getClientOriginalExtension(); //Retriving Image extension
109
        $imageStoreNameOnly = $filename . '-' . time(); //Making Image Store name
110
        $imageStoreName = $filename . '-' . time() . '.' . $extension; //Making Image Store name
111
112
        $image_info['filenamewithextension'] = $filenamewithextension;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$image_info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $image_info = array(); before regardless.
Loading history...
113
        $image_info['raw_filename'] = $raw_filename;
114
        $image_info['filename'] = $filename;
115
        $image_info['extension'] = $extension;
116
        $image_info['imageStoreNameOnly'] = $imageStoreNameOnly;
117
        $image_info['imageStoreName'] = $imageStoreName;
118
        return json_decode(json_encode($image_info));
119
    }
120
121
    // Upload Parent Image
122
    public function uploadImage($fieldname = 'image', $custom = [])
123
    {
124
        $image_file = $custom['image'] ?? request()->file($fieldname); // Retriving Image File
125
        $img = $custom['image'] ?? request()->$fieldname;
126
        $imageStoreName = $this->image_info($image_file)->imageStoreName;
127
        $this->update([
0 ignored issues
show
Bug introduced by
It seems like update() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
        $this->/** @scrutinizer ignore-call */ 
128
               update([
Loading history...
128
            $fieldname => $img->storeAs($custom['storage'] ?? config('thumbnail.storage_path', 'uploads'), $imageStoreName, 'public'), // Storing Parent Image
129
        ]);
130
131
        $image = Image::cache(function ($cached_img) use ($image_file, $custom) {
132
            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
0 ignored issues
show
Bug introduced by
The method getRealPath() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            return $cached_img->make($image_file->/** @scrutinizer ignore-call */ getRealPath())->fit($custom['width'] ?? config('thumbnail.img_width', 1000), $custom['height'] ?? config('thumbnail.img_height', 800)); //Parent Image Interventing

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
        }, config('thumbnail.image_cached_time', 10), true);
134
        $image->save(public_path('storage/' . $this->$fieldname), $custom['quality'] ?? config('thumbnail.image_quality', 80)); // Parent Image Locating Save
135
    }
136
137
    // Thumbnail Path
138
    public function thumbnail($fieldname = 'image', $size = null)
139
    {
140
        return $this->imageDetail($fieldname, $size)->path;
141
    }
142
143
    /* Checking Image Existance */
144
    private function imageExists($image)
145
    {
146
        return file_exists($image->getRealPath());
147
    }
148
149
    // Checking Image's Thumbnail Existance
150
    public function hasThumbnail($fieldname = 'image', $size = null)
151
    {
152
        return $this->imageDetail($fieldname, $size)->property->has_thumbnail;
153
    }
154
155
    // Thumbnail Count
156
    public function thumbnailCount($fieldname = 'image', $size = null)
157
    {
158
        return $this->hasThumbnail($fieldname, $size) ? $this->imageDetail($fieldname, $size)->property->thumbnail_count : 0;
159
    }
160
161
    /* Image Details */
162
    public function imageDetail($fieldname = 'image', $size = null)
163
    {
164
        $image = $this->$fieldname;
165
        $path = explode('/', $image);
166
        $extension = \File::extension($image);
167
        $name = basename($image, '.' . $extension);
168
        $image_fullname = isset($size) ? $name . '-' . (string) $size . '.' . $extension : $name . '.' . $extension;
169
        array_pop($path);
170
        $location = implode('/', $path);
171
        $path = 'storage/' . $location . '/' . $image_fullname;
172
        $image_files = File::files(public_path('storage/' . $location));
173
        $images_property = $this->imageProperty($image_files, $name);
174
        $image_detail = [
175
            'image'     => $image,
176
            'name'      => $name,
177
            'fullname'  => $image_fullname,
178
            'extension' => $extension,
179
            'path'      => $path,
180
            'directory' => public_path('storage/' . $location),
181
            'location'  => public_path('storage/' . $image),
182
            'property'  => $images_property,
183
        ];
184
185
        return json_decode(json_encode($image_detail));
186
    }
187
188
    // Image Property
189
    private function imageProperty($image_files, $parent_name)
190
    {
191
192
        $images_property = [];
193
        $thumbnails_property = [];
194
        $thumbnail_count = 0;
195
        foreach ($image_files as $image) {
196
            $image_partition = explode('-', basename($image));
197
            $parent_thumbnail_name = $image_partition[0] . '-' . $image_partition[1];
198
            if ($parent_name == $parent_thumbnail_name) {
199
                $thumbnail_count++;
200
                $thumbnail_exists = $this->imageExists($image);
201
                if (isset($image_partition[2])) {
202
                    $thumbnails_property['image'] = $image->getFilename() ?? null;
203
                    $thumbnails_property['real_name'] = $image_partition[0];
204
                    $thumbnails_property['size'] = $image->getSize();
205
                    $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;
206
                    $thumbnails_property['directory'] = $image->getPath();
207
                    $thumbnails_property['location'] = $image->getRealPath();
208
                    $images_property['has_thumbnail'] = $thumbnail_exists || $this->imageExists($image);
209
                    $images_property['thumbnail_count'] = $thumbnail_count;
210
                    $thumbnails[] = $thumbnails_property;
211
                    $images_property['thumbnails'] = $thumbnails;
212
                }
213
            } elseif ($image->getFileNameWithoutExtension() == $parent_name) {
214
                $images_property['has_thumbnail'] = ($thumbnail_exists ?? false);
215
                $images_property['real_name'] = $image_partition[0];
216
                $images_property['size'] = $image->getSize();
217
                $images_property['directory'] = $image->getPath();
218
                $images_property['location'] = $image->getRealPath();
219
            } else {
220
                false;
221
            }
222
        }
223
224
        return $images_property;
225
    }
226
227
    // Hard Delete
228
    public function hardDelete($fieldname = 'image'): void
229
    {
230
        if (File::exists($this->imageDetail($fieldname)->location)) {
231
            if ($this->imageDetail($fieldname)->property->has_thumbnail) {
232
                foreach ($this->imageDetail($fieldname)->property->thumbnails as $thumbnail) {
233
                    File::exists($thumbnail->location) ? File::delete($thumbnail->location) : '';
234
                }
235
            }
236
            File::exists($this->imageDetail($fieldname)->location) ? File::delete($this->imageDetail($fieldname)->location) : false;
237
        }
238
        $this->delete();
0 ignored issues
show
Bug introduced by
It seems like delete() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
        $this->/** @scrutinizer ignore-call */ 
239
               delete();
Loading history...
239
    }
240
}
241