Completed
Push — master ( 272243...2e6f42 )
by Sherif
13:48
created

Media::uploadImageBas64()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php namespace App\Modules\V1\Core\Utl;
2
3
class Media
4
{
5
    /**
6
     * Upload the given image.
7
     * 
8
     * @param  object  $image
9
     * @param  string  $dir
10
     * @return string
11
     */
12
    public function uploadImage($image, $dir) 
13
    {
14
        $image = \Image::make($image);
15
        return $this->saveImage($image, $dir);
16
    }
17
18
    /**
19
     * Upload the given image.
20
     * 
21
     * @param  object  $image
22
     * @param  string  $dir
23
     * @return string
24
     */
25
    public function uploadImageBas64($image, $dir) 
26
    {
27
        if ( ! strlen($image)) 
28
        {
29
            return null;
30
        }
31
32
        $base  = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image));
33
        $image = \Image::make($base);
34
35
        return $this->saveImage($image, $dir);
36
    }
37
38
    /**
39
     * Delete the given image.
40
     * 
41
     * @param  object $path
42
     * @return void
43
     */
44
    public function deleteImage($path) 
45
    {   
46
        \Storage::delete($path);
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
47
    }
48
49
    /**
50
     * Save the given image.
51
     * 
52
     * @param  object  $image
53
     * @param  string  $dir
54
     * @return string
55
     */
56
    protected function saveImage($image, $dir) 
57
    {
58
        $imageName = 'image'.uniqid().time().'.jpg';
59
        $path      = 'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$imageName;
60
        \Storage::put($path, $image->stream());
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

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...
61
62
        return $path;
63
    }
64
}
65