Passed
Push — master ( 3d289f...9d27b0 )
by PRATIK
02:59 queued 10s
created

Thumbnail::thumbnailCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 3
rs 10
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
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SL on line 45 at column 0
Loading history...
46
                    $this->makeCustomThumbnails($image_file, $imageStoreNameOnly, $extension, $storage, $thumbnails);
47
                    /* -------------------------------------------------------------------------------------------------- */
48
=======
49
                    $this->makeCustomThumbnails($image_file, $imageStoreNameOnly, $storage, $thumbnails);
50
                /* -------------------------------------------------------------------------------------------------- */
51
>>>>>>> 3d289ff4cdb1443b9593e3b319b7521874189227
52
                } else {
53
                    /* ---------------------------------------Default Thumbnails--------------------------------------- */
54
                    $this->makeDefaultThumbnails($image_file, $extension, $imageStoreNameOnly);
55
                    /* ------------------------------------------------------------------------------------------------ */
56
                }
57
            }
58
        }
59
    }
60
61
    // Make Image
62
    private function makeImg($image_file, $name, $location, $width, $height, $quality)
63
    {
64
        $image = $image_file->storeAs($location, $name, 'public'); // Thumbnail Storage Information
65
        $img = Image::cache(function ($cached_img) use ($image_file, $width, $height) {
66
            return $cached_img->make($image_file->getRealPath())->fit($width, $height);
67
        }, config('thumbnail.image_cached_time', 10), true); //Storing Thumbnail
68
        $img->save(public_path('storage/'.$image), $quality); //Storing Thumbnail
69
    }
70
71
    // Make Custom Thumbnail
72
    private function makeCustomThumbnails($image_file, $imageStoreNameOnly, $extension, $storage, $thumbnails)
73
    {
74
        foreach ($thumbnails as $thumbnail) {
75
            $customthumbnail = $imageStoreNameOnly.'-'.str_replace('-', '', $thumbnail['thumbnail-name']).'.'.$extension; // Making Thumbnail Name
76
            $this->makeImg(
77
                $image_file,
78
                $customthumbnail,
79
                $storage,
80
                (int) $thumbnail['thumbnail-width'],
81
                (int) $thumbnail['thumbnail-height'],
82
                (int) $thumbnail['thumbnail-quality']
83
            );
84
        }
85
    }
86
87
    // Make Default Thumbnail
88
    private function makeDefaultThumbnails($image_file, $extension, $imageStoreNameOnly)
89
    {
90
        /* --------------------- Thumbnail Info---------------------------------------- */
91
        //small thumbnail name
92
        $smallthumbnail = $imageStoreNameOnly.'-small'.'.'.$extension; // Making Thumbnail Name
93
94
        //medium thumbnail name
95
        $mediumthumbnail = $imageStoreNameOnly.'-medium'.'.'.$extension; // Making Thumbnail Name
96
97
        // Medium Thumbnail
98
        $this->makeImg(
99
            $image_file,
100
            $mediumthumbnail,
101
            config('thumbnail.storage_path', 'uploads'),
102
            config('thumbnail.medium_thumbnail_width', 800),
103
            config('thumbnail.medium_thumbnail_height', 600),
104
            config('thumbnail.medium_thumbnail_quality', 60)
105
        );
106
107
        // Small Thumbnail
108
        $this->makeImg(
109
            $image_file,
110
            $smallthumbnail,
111
            config('thumbnail.storage_path', 'uploads'),
112
            config('thumbnail.small_thumbnail_width', 800),
113
            config('thumbnail.small_thumbnail_height', 600),
114
            config('thumbnail.small_thumbnail_quality', 60)
115
        );
116
117
        /* ------------------------------------------------------------------------------------- */
118
    }
119
120
    // Thumbnail Path
121
    public function thumbnail($fieldname = 'image', $size = null)
122
    {
123
        return $this->imageDetail($fieldname, $size)->path;
124
    }
125
126
    /* Checking Image Existance */
127
    private function imageExists($image)
128
    {
129
        return file_exists($image->getRealPath());
130
    }
131
132
    // Checking Image's Thumbnail Existance
133
    public function hasThumbnail($fieldname = 'image', $size = null)
134
    {
135
        return $this->imageDetail($fieldname, $size)->property->has_thumbnail;
136
    }
137
138
    // Thumbnail Count
139
    public function thumbnailCount($fieldname = 'image', $size = null)
140
    {
141
        return $this->hasThumbnail($fieldname, $size) ? $this->imageDetail($fieldname, $size)->property->thumbnail_count : 0;
142
    }
143
144
    /* Image Details */
145
    public function imageDetail($fieldname = 'image', $size = null)
146
    {
147
        $image = $this->$fieldname;
148
        $path = explode('/', $image);
149
        $extension = \File::extension($image);
150
        $name = basename($image, '.'.$extension);
151
        $image_fullname = isset($size) ? $name.'-'.(string) $size.'.'.$extension : $name.'.'.$extension;
152
        array_pop($path);
153
        $location = implode('/', $path);
154
        $path = 'storage/'.$location.'/'.$image_fullname;
155
        $image_files = File::files(public_path('storage/'.$location));
156
        $images_property = $this->imageProperty($image_files);
157
        $image_detail = [
158
            'image'     => $image,
159
            'name'      => $name,
160
            'fullname'  => $image_fullname,
161
            'extension' => $extension,
162
            'path'      => $path,
163
            'directory' => public_path('storage/'.$location),
164
            'location'  => public_path('storage/'.$image),
165
            'property'  => $images_property,
166
        ];
167
168
        return json_decode(json_encode($image_detail));
169
    }
170
171
    // Image Property
172
    private function imageProperty($image_files)
173
    {
174
        $thumbnail_count = 0;
175
        $images_property = [];
176
        $thumbnails_property = [];
177
        foreach ($image_files as $image) {
178
            $image_partition = explode('-', basename($image));
179
            if (isset($image_partition[2])) {
180
                $thumbnails_property['image'] = $image->getFilename() ?? null;
181
                $thumbnails_property['real_name'] = $image_partition[0];
182
                $thumbnails_property['size'] = $image->getSize();
183
                $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;
184
                $thumbnails_property['directory'] = $image->getPath();
185
                $thumbnails_property['location'] = $image->getRealPath();
186
                $thumbnail_exists = $this->imageExists($image);
187
                $images_property['has_thumbnail'] = $thumbnail_exists || $this->imageExists($image);
188
                $images_property['thumbnail_count'] = $thumbnail_count + 1;
189
            } else {
190
                $images_property['real_name'] = $image_partition[0];
191
                $images_property['size'] = $image->getSize();
192
                $images_property['directory'] = $image->getPath();
193
                $images_property['location'] = $image->getRealPath();
194
            }
195
            if (isset($image_partition[2])) {
196
                $images_property['thumbnails'] = $thumbnails_property;
197
            }
198
        }
199
200
        return $images_property;
201
    }
202
}
203