2
2
3
3
namespace App \Services ;
4
4
5
+ use App \Enums \ImageFormat ;
5
6
use App \Models \Device ;
6
7
use App \Models \Plugin ;
7
8
use Illuminate \Support \Facades \Storage ;
@@ -38,21 +39,40 @@ public static function generateImage(string $markup, $deviceId): string
38
39
throw new \RuntimeException ('Failed to generate PNG: ' .$ e ->getMessage (), 0 , $ e );
39
40
}
40
41
}
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
+ }
55
74
}
75
+
56
76
$ device ->update (['current_screen_image ' => $ uuid ]);
57
77
\Log::info ("Device $ device ->id : updated with new image: $ uuid " );
58
78
@@ -77,7 +97,7 @@ private static function convertToBmpImageMagick(string $pngPath, string $bmpPath
77
97
/**
78
98
* @throws \ImagickException
79
99
*/
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
81
101
{
82
102
$ imagick = new \Imagick ($ pngPath );
83
103
if ($ width !== 800 || $ height !== 480 ) {
@@ -87,7 +107,9 @@ private static function convertToPngImageMagick(string $pngPath, ?int $width, ?i
87
107
$ imagick ->rotateImage (new ImagickPixel ('black ' ), $ rotate );
88
108
}
89
109
$ 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
+ }
91
113
$ imagick ->setImageDepth (8 );
92
114
$ imagick ->stripImage ();
93
115
0 commit comments