|
10 | 10 | class Image |
11 | 11 | { |
12 | 12 | /** |
| 13 | + * Resize the image and save it. |
| 14 | + * |
13 | 15 | * @throws \Imahmood\FileStorage\Exceptions\NotWritableException |
14 | 16 | * @throws \Jcupitt\Vips\Exception |
15 | 17 | */ |
16 | | - public function resize(string $disk, string $sourceFile, string $targetFile, int $width, int $height): void |
| 18 | + public function resize(string $disk, string $sourceFile, string $targetFile, int $width, int $height, bool $flatten): void |
17 | 19 | { |
18 | | - $image = VipsImage::newFromBuffer( |
19 | | - Storage::disk($disk)->get($sourceFile) |
20 | | - ); |
21 | | - |
22 | | - if ($image->hasAlpha()) { |
23 | | - $image = $image->flatten(['background' => [255, 255, 255]]); |
24 | | - } |
| 20 | + $image = $this->loadImage($disk, $sourceFile); |
25 | 21 |
|
26 | 22 | $width = min($image->width, $width); |
27 | 23 | $height = min($image->height, $height); |
28 | | - $ext = pathinfo($targetFile, PATHINFO_EXTENSION); |
29 | | - |
30 | | - $saved = Storage::disk($disk)->put( |
31 | | - path: $targetFile, |
32 | | - contents: $image->thumbnail_image($width, ['height' => $height])->writeToBuffer('.'.$ext), |
33 | | - ); |
34 | | - |
35 | | - $image = null; |
36 | 24 |
|
37 | | - if ($saved === false) { |
38 | | - throw new NotWritableException("Can't write image data to path `$targetFile`"); |
| 25 | + if ($flatten && $image->hasAlpha()) { |
| 26 | + $image = $image->flatten(['background' => [255, 255, 255]]); |
39 | 27 | } |
| 28 | + |
| 29 | + $image = $image->thumbnail_image($width, ['height' => $height]); |
| 30 | + $this->saveImage($disk, $targetFile, $image); |
40 | 31 | } |
41 | 32 |
|
42 | 33 | /** |
| 34 | + * Convert the image format and save it. |
| 35 | + * |
43 | 36 | * @throws \Imahmood\FileStorage\Exceptions\NotWritableException |
44 | 37 | * @throws \Jcupitt\Vips\Exception |
45 | 38 | */ |
46 | | - public function convert(string $disk, string $sourceFile, string $targetFile): void |
| 39 | + public function convert(string $disk, string $sourceFile, string $targetFile, bool $flatten): void |
47 | 40 | { |
48 | | - $image = VipsImage::newFromBuffer( |
49 | | - Storage::disk($disk)->get($sourceFile) |
50 | | - ); |
| 41 | + $image = $this->loadImage($disk, $sourceFile); |
51 | 42 |
|
52 | | - if ($image->hasAlpha()) { |
| 43 | + if ($flatten && $image->hasAlpha()) { |
53 | 44 | $image = $image->flatten(['background' => [255, 255, 255]]); |
54 | 45 | } |
55 | 46 |
|
56 | | - $ext = pathinfo($targetFile, PATHINFO_EXTENSION); |
| 47 | + $this->saveImage($disk, $targetFile, $image); |
| 48 | + } |
57 | 49 |
|
58 | | - $saved = Storage::disk($disk)->put( |
59 | | - path: $targetFile, |
60 | | - contents: $image->writeToBuffer('.'.$ext), |
61 | | - ); |
| 50 | + /** |
| 51 | + * Load image from storage. |
| 52 | + */ |
| 53 | + private function loadImage(string $disk, string $path): VipsImage |
| 54 | + { |
| 55 | + $content = Storage::disk($disk)->get($path); |
| 56 | + |
| 57 | + return VipsImage::newFromBuffer($content); |
| 58 | + } |
62 | 59 |
|
63 | | - $image = null; |
| 60 | + /** |
| 61 | + * Save image to storage. |
| 62 | + * |
| 63 | + * @throws \Imahmood\FileStorage\Exceptions\NotWritableException |
| 64 | + */ |
| 65 | + private function saveImage(string $disk, string $path, VipsImage $image): void |
| 66 | + { |
| 67 | + $extension = '.'.pathinfo($path, PATHINFO_EXTENSION); |
| 68 | + $buffer = $image->writeToBuffer($extension); |
64 | 69 |
|
65 | | - if ($saved === false) { |
66 | | - throw new NotWritableException("Can't write image data to path `$targetFile`"); |
| 70 | + if (! Storage::disk($disk)->put($path, $buffer)) { |
| 71 | + throw new NotWritableException("Can't write image data to path `$path`"); |
67 | 72 | } |
68 | 73 | } |
69 | 74 | } |
0 commit comments