Passed
Push — master ( dc496a...7e7027 )
by Darko
06:53
created

ReleaseImage   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 53
c 1
b 0
f 0
dl 0
loc 129
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchImage() 0 21 5
A __construct() 0 8 1
A delete() 0 5 1
B saveImage() 0 41 10
1
<?php
2
3
namespace Blacklight;
4
5
use Illuminate\Support\Facades\File;
6
use Intervention\Image\Drivers\Imagick\Driver;
7
use Intervention\Image\Exceptions\NotWritableException;
8
use Intervention\Image\ImageManager;
9
use Spatie\LaravelImageOptimizer\Facades\ImageOptimizer;
10
11
/**
12
 * Resize/save/delete images to disk.
13
 *
14
 *
15
 * Class ReleaseImage
16
 */
17
class ReleaseImage
18
{
19
    /**
20
     * Path to save ogg audio samples.
21
     */
22
    public string $audSavePath;
23
24
    /**
25
     * Path to save video preview jpg pictures.
26
     */
27
    public string $imgSavePath;
28
29
    /**
30
     * Path to save large jpg pictures(xxx).
31
     */
32
    public string $jpgSavePath;
33
34
    /**
35
     * Path to save movie jpg covers.
36
     */
37
    public string $movieImgSavePath;
38
39
    /**
40
     * Path to save video ogv files.
41
     */
42
    public string $vidSavePath;
43
44
    protected ColorCLI $colorCli;
45
46
    /**
47
     * ReleaseImage constructor.
48
     */
49
    public function __construct()
50
    {
51
        $this->colorCli = new ColorCLI();
52
        $this->audSavePath = storage_path('covers/audiosample/');
53
        $this->imgSavePath = storage_path('covers/preview/');
54
        $this->jpgSavePath = storage_path('covers/sample/');
55
        $this->movieImgSavePath = storage_path('covers/movies/');
56
        $this->vidSavePath = storage_path('covers/video/');
57
    }
58
59
    protected function fetchImage(string $imgLoc): bool|\Intervention\Image\Interfaces\ImageInterface
60
    {
61
        try {
62
            $manager = new ImageManager(Driver::class);
63
            $img = $manager->read(file_get_contents($imgLoc));
64
        } catch (\Exception $e) {
65
            if ($e->getCode() === 404) {
66
                $this->colorCli->notice('Data not available on server');
67
            } elseif ($e->getCode() === 503) {
68
                $this->colorCli->notice('Service unavailable');
69
            } else {
70
                $this->colorCli->notice('Unable to fetch image: '.$e->getMessage());
71
            }
72
73
            $img = false;
74
        } catch (\Error $e) {
75
            $this->colorCli->climate()->info($e->getMessage());
76
            $img = false;
77
        }
78
79
        return $img;
80
    }
81
82
    /**
83
     * Save an image to disk, optionally resizing it.
84
     *
85
     * @param  string  $imgName  What to name the new image.
86
     * @param  string  $imgLoc  URL or location on the disk the original image is in.
87
     * @param  string  $imgSavePath  Folder to save the new image in.
88
     * @param  string  $imgMaxWidth  Max width to resize image to.   (OPTIONAL)
89
     * @param  string  $imgMaxHeight  Max height to resize image to.  (OPTIONAL)
90
     * @param  bool  $saveThumb  Save a thumbnail of this image? (OPTIONAL)
91
     * @return int 1 on success, 0 on failure Used on site to check if there is an image.
92
     */
93
    public function saveImage(string $imgName, string $imgLoc, string $imgSavePath, string $imgMaxWidth = '', string $imgMaxHeight = '', bool $saveThumb = false): int
94
    {
95
        $cover = $this->fetchImage($imgLoc);
96
97
        if ($cover === false) {
98
            return 0;
99
        }
100
101
        // Check if we need to resize it.
102
        if ($imgMaxWidth !== '' && $imgMaxHeight !== '') {
103
            $width = $cover->width();
104
            $height = $cover->height();
105
            $ratio = min($imgMaxHeight / $height, $imgMaxWidth / $width);
106
            // New dimensions
107
            $new_width = $ratio * $width;
108
            $new_height = $ratio * $height;
109
            if ($new_width < $width && $new_width > 10 && $new_height > 10) {
110
                $cover->resize($new_width, $new_height);
111
112
                if ($saveThumb) {
113
                    $cover->toJpeg(100)->save($imgSavePath.$imgName.'_thumb.jpg');
114
                    //Optimize the thumbnail.
115
                    ImageOptimizer::optimize($imgSavePath.$imgName.'_thumb.jpg');
116
                }
117
            }
118
        }
119
        // Store it on the hard drive.
120
        $coverPath = $imgSavePath.$imgName.'.jpg';
121
        try {
122
            $cover->toJpeg(100)->save($coverPath);
123
            //Optimize the image.
124
            ImageOptimizer::optimize($coverPath);
125
        } catch (NotWritableException $e) {
126
            return 0;
127
        }
128
        // Check if it's on the drive.
129
        if (! File::isReadable($coverPath)) {
130
            return 0;
131
        }
132
133
        return 1;
134
    }
135
136
    /**
137
     * Delete images for the release.
138
     *
139
     * @param  string  $guid  The GUID of the release.
140
     */
141
    public function delete(string $guid): void
142
    {
143
        $thumb = $guid.'_thumb.jpg';
144
145
        File::delete([$this->audSavePath.$guid.'.ogg', $this->imgSavePath.$thumb, $this->jpgSavePath.$thumb, $this->vidSavePath.$guid.'.ogv']);
146
    }
147
}
148