Skip to content

Commit d75d099

Browse files
committed
feat: support for additional devices
1 parent 27ea7d1 commit d75d099

File tree

4 files changed

+96
-16
lines changed

4 files changed

+96
-16
lines changed

app/Enums/ImageFormat.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
enum ImageFormat: string
6+
{
7+
case AUTO = 'auto';
8+
case PNG_8BIT_GRAYSCALE = 'png_8bit_grayscale';
9+
case BMP3_1BIT_SRGB = 'bmp3_1bit_srgb';
10+
case PNG_8BIT_256C = 'png_8bit_256c';
11+
12+
public function label(): string
13+
{
14+
return match ($this) {
15+
self::AUTO => 'Auto',
16+
self::PNG_8BIT_GRAYSCALE => 'PNG 8-bit Grayscale Gray 2c',
17+
self::BMP3_1BIT_SRGB => 'BMP3 1-bit sRGB 2c',
18+
self::PNG_8BIT_256C => 'PNG 8-bit Grayscale Gray 256c',
19+
};
20+
}
21+
}

app/Services/ImageGenerationService.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Services;
44

5+
use App\Enums\ImageFormat;
56
use App\Models\Device;
67
use App\Models\Plugin;
78
use Illuminate\Support\Facades\Storage;
@@ -38,21 +39,40 @@ public static function generateImage(string $markup, $deviceId): string
3839
throw new \RuntimeException('Failed to generate PNG: '.$e->getMessage(), 0, $e);
3940
}
4041
}
41-
42-
if (isset($device->last_firmware_version)
43-
&& version_compare($device->last_firmware_version, '1.5.2', '<')) {
44-
try {
45-
ImageGenerationService::convertToBmpImageMagick($pngPath, $bmpPath);
46-
} catch (\ImagickException $e) {
47-
throw new \RuntimeException('Failed to convert image to BMP: '.$e->getMessage(), 0, $e);
48-
}
49-
} else {
50-
try {
51-
ImageGenerationService::convertToPngImageMagick($pngPath, $device->width, $device->height, $device->rotate);
52-
} catch (\ImagickException $e) {
53-
throw new \RuntimeException('Failed to convert image to PNG: '.$e->getMessage(), 0, $e);
54-
}
42+
switch ($device->image_format) {
43+
case ImageFormat::BMP3_1BIT_SRGB->value:
44+
try {
45+
ImageGenerationService::convertToBmpImageMagick($pngPath, $bmpPath);
46+
} catch (\ImagickException $e) {
47+
throw new \RuntimeException('Failed to convert image to BMP: '.$e->getMessage(), 0, $e);
48+
}
49+
break;
50+
case ImageFormat::PNG_8BIT_GRAYSCALE->value:
51+
case ImageFormat::PNG_8BIT_256C->value:
52+
try {
53+
ImageGenerationService::convertToPngImageMagick($pngPath, $device->width, $device->height, $device->rotate, quantize: $device->image_format === ImageFormat::PNG_8BIT_GRAYSCALE);
54+
} catch (\ImagickException $e) {
55+
throw new \RuntimeException('Failed to convert image to PNG: '.$e->getMessage(), 0, $e);
56+
}
57+
break;
58+
case ImageFormat::AUTO->value:
59+
default:
60+
if (isset($device->last_firmware_version)
61+
&& version_compare($device->last_firmware_version, '1.5.2', '<')) {
62+
try {
63+
ImageGenerationService::convertToBmpImageMagick($pngPath, $bmpPath);
64+
} catch (\ImagickException $e) {
65+
throw new \RuntimeException('Failed to convert image to BMP: '.$e->getMessage(), 0, $e);
66+
}
67+
} else {
68+
try {
69+
ImageGenerationService::convertToPngImageMagick($pngPath, $device->width, $device->height, $device->rotate);
70+
} catch (\ImagickException $e) {
71+
throw new \RuntimeException('Failed to convert image to PNG: '.$e->getMessage(), 0, $e);
72+
}
73+
}
5574
}
75+
5676
$device->update(['current_screen_image' => $uuid]);
5777
\Log::info("Device $device->id: updated with new image: $uuid");
5878

@@ -77,7 +97,7 @@ private static function convertToBmpImageMagick(string $pngPath, string $bmpPath
7797
/**
7898
* @throws \ImagickException
7999
*/
80-
private static function convertToPngImageMagick(string $pngPath, ?int $width, ?int $height, ?int $rotate): void
100+
private static function convertToPngImageMagick(string $pngPath, ?int $width, ?int $height, ?int $rotate, $quantize = true): void
81101
{
82102
$imagick = new \Imagick($pngPath);
83103
if ($width !== 800 || $height !== 480) {
@@ -87,7 +107,9 @@ private static function convertToPngImageMagick(string $pngPath, ?int $width, ?i
87107
$imagick->rotateImage(new ImagickPixel('black'), $rotate);
88108
}
89109
$imagick->setImageType(\Imagick::IMGTYPE_GRAYSCALE);
90-
$imagick->quantizeImage(2, \Imagick::COLORSPACE_GRAY, 0, true, false);
110+
if ($quantize) {
111+
$imagick->quantizeImage(2, \Imagick::COLORSPACE_GRAY, 0, true, false);
112+
}
91113
$imagick->setImageDepth(8);
92114
$imagick->stripImage();
93115

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('devices', function (Blueprint $table) {
15+
$table->string('image_format')->default('auto')->after('rotate');
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('devices', function (Blueprint $table) {
25+
$table->dropColumn('image_format');
26+
});
27+
}
28+
};

resources/views/livewire/devices/configure.blade.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
public $width;
1717
public $height;
1818
public $rotate;
19+
public $image_format;
1920
2021
// Playlist properties
2122
public $playlists;
@@ -41,6 +42,7 @@ public function mount(\App\Models\Device $device)
4142
$this->width = $device->width;
4243
$this->height = $device->height;
4344
$this->rotate = $device->rotate;
45+
$this->image_format = $device->image_format;
4446
$this->playlists = $device->playlists()->with('items.plugin')->orderBy('created_at')->get();
4547
4648
return view('livewire.devices.configure', [
@@ -68,6 +70,7 @@ public function updateDevice()
6870
'width' => 'required|integer|min:1',
6971
'height' => 'required|integer|min:1',
7072
'rotate' => 'required|integer|min:0|max:359',
73+
'image_format' => 'required|string',
7174
]);
7275
7376
$this->device->update([
@@ -78,6 +81,7 @@ public function updateDevice()
7881
'width' => $this->width,
7982
'height' => $this->height,
8083
'rotate' => $this->rotate,
84+
'image_format' => $this->image_format,
8185
]);
8286
8387
Flux::modal('edit-device')->close();
@@ -288,6 +292,11 @@ class="dark:text-zinc-200"/>
288292
<flux:input label="Height (px)" wire:model="height" type="number"/>
289293
<flux:input label="Rotate °" wire:model="rotate" type="number"/>
290294
</div>
295+
<flux:select label="Image Format" wire:model="image_format">
296+
@foreach(\App\Enums\ImageFormat::cases() as $format)
297+
<flux:select.option value="{{ $format->value }}">{{$format->label()}}</flux:select.option>
298+
@endforeach
299+
</flux:select>
291300
<flux:input label="Default Refresh Interval (seconds)" wire:model="default_refresh_interval"
292301
type="number"/>
293302

0 commit comments

Comments
 (0)