|
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 int $imgMaxWidth Max width to resize image to. (OPTIONAL) |
|
89
|
|
|
* @param int $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, int $imgMaxWidth = 0, int $imgMaxHeight = 0, 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 !== 0 && $imgMaxHeight !== 0) { |
|
103
|
|
|
$width = $cover->width(); |
|
104
|
|
|
$height = $cover->height(); |
|
105
|
|
|
if ($width !== 0 || $height !== 0) { |
|
106
|
|
|
$ratio = min($imgMaxHeight / $height, $imgMaxWidth / $width); |
|
107
|
|
|
// New dimensions |
|
108
|
|
|
$new_width = $ratio * $width; |
|
109
|
|
|
$new_height = $ratio * $height; |
|
110
|
|
|
if ($new_width < $width && $new_width > 10 && $new_height > 10) { |
|
111
|
|
|
$cover->resize($new_width, $new_height); |
|
112
|
|
|
|
|
113
|
|
|
if ($saveThumb) { |
|
114
|
|
|
$cover->toJpeg(100)->save($imgSavePath.$imgName.'_thumb.jpg'); |
|
115
|
|
|
//Optimize the thumbnail. |
|
116
|
|
|
ImageOptimizer::optimize($imgSavePath.$imgName.'_thumb.jpg'); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
// Store it on the hard drive. |
|
122
|
|
|
$coverPath = $imgSavePath.$imgName.'.jpg'; |
|
123
|
|
|
try { |
|
124
|
|
|
$cover->toJpeg(100)->save($coverPath); |
|
125
|
|
|
//Optimize the image. |
|
126
|
|
|
ImageOptimizer::optimize($coverPath); |
|
127
|
|
|
} catch (NotWritableException $e) { |
|
128
|
|
|
return 0; |
|
129
|
|
|
} |
|
130
|
|
|
// Check if it's on the drive. |
|
131
|
|
|
if (! File::isReadable($coverPath)) { |
|
132
|
|
|
return 0; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return 1; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Delete images for the release. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $guid The GUID of the release. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function delete(string $guid): void |
|
144
|
|
|
{ |
|
145
|
|
|
$thumb = $guid.'_thumb.jpg'; |
|
146
|
|
|
|
|
147
|
|
|
File::delete([$this->audSavePath.$guid.'.ogg', $this->imgSavePath.$thumb, $this->jpgSavePath.$thumb, $this->vidSavePath.$guid.'.ogv']); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|