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) <noreply@anthropic.com>
This commit is contained in:
@@ -231,10 +231,10 @@ class DeviceApiController extends AbstractController
|
|||||||
|
|
||||||
private function renderBinToPng(string $binPath, string $pngPath, int $width, int $height): void
|
private function renderBinToPng(string $binPath, string $pngPath, int $width, int $height): void
|
||||||
{
|
{
|
||||||
$bin = (string) file_get_contents($binPath);
|
$bin = (string) file_get_contents($binPath);
|
||||||
$len = strlen($bin);
|
$len = strlen($bin);
|
||||||
$rgb = '';
|
$rgb = '';
|
||||||
$white = self::PALETTE[0x1];
|
$white = self::PALETTE[0x1];
|
||||||
for ($i = 0; $i < $len; $i++) {
|
for ($i = 0; $i < $len; $i++) {
|
||||||
$byte = ord($bin[$i]);
|
$byte = ord($bin[$i]);
|
||||||
foreach ([($byte >> 4) & 0xF, $byte & 0xF] as $idx) {
|
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 = new \Imagick();
|
||||||
$im->newImage($width, $height, new \ImagickPixel('white'));
|
$im->readImageBlob($ppm);
|
||||||
$im->importImagePixels(0, 0, $width, $height, 'RGB', \Imagick::PIXEL_CHAR, $rgb);
|
|
||||||
$im->setImageFormat('png');
|
$im->setImageFormat('png');
|
||||||
$im->writeImage($pngPath);
|
$im->writeImage($pngPath);
|
||||||
$im->destroy();
|
$im->destroy();
|
||||||
|
|||||||
Reference in New Issue
Block a user