From c2b208f103ee20af0d94d3c5e419df04ae063c8b Mon Sep 17 00:00:00 2001 From: Matt Edholm Date: Wed, 6 May 2026 13:16:58 -0400 Subject: [PATCH] fix: decode .bin to PNG via PPM blob, not importImagePixels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Imagick extension on the server (3.7+) requires importImagePixels to receive an array of ints, not a string blob, so the previous code 500'd. Wrap the raw RGB bytes in a PPM (P6) header and let Imagick decode it via readImageBlob — same result, no 70 MB-array detour. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Controller/DeviceApiController.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Controller/DeviceApiController.php b/src/Controller/DeviceApiController.php index 5a45cf0..d15cb51 100644 --- a/src/Controller/DeviceApiController.php +++ b/src/Controller/DeviceApiController.php @@ -231,10 +231,10 @@ class DeviceApiController extends AbstractController private function renderBinToPng(string $binPath, string $pngPath, int $width, int $height): void { - $bin = (string) file_get_contents($binPath); - $len = strlen($bin); - $rgb = ''; - $white = self::PALETTE[0x1]; + $bin = (string) file_get_contents($binPath); + $len = strlen($bin); + $rgb = ''; + $white = self::PALETTE[0x1]; for ($i = 0; $i < $len; $i++) { $byte = ord($bin[$i]); foreach ([($byte >> 4) & 0xF, $byte & 0xF] as $idx) { @@ -243,9 +243,13 @@ class DeviceApiController extends AbstractController } } + // Wrap the raw RGB bytes in a PPM (P6) header so Imagick can decode the + // blob directly. importImagePixels would also work but its current + // signature requires an array of ints, which is ~70 MB for an 800×480. + $ppm = "P6\n{$width} {$height}\n255\n" . $rgb; + $im = new \Imagick(); - $im->newImage($width, $height, new \ImagickPixel('white')); - $im->importImagePixels(0, 0, $width, $height, 'RGB', \Imagick::PIXEL_CHAR, $rgb); + $im->readImageBlob($ppm); $im->setImageFormat('png'); $im->writeImage($pngPath); $im->destroy();