Skip to content

Commit 78322f0

Browse files
authored
Get imagick image from file instead of blob (#166)
1 parent 30e25c2 commit 78322f0

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/card.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,14 @@ function echoAsPng(string $svg): void
324324
$svg = preg_replace('/(animation: fadein.*?;)/m', 'opacity: 1;', $svg);
325325
$svg = preg_replace('/(animation: currentstreak.*?;)/m', 'font-size: 28px;', $svg);
326326

327-
// create canvas
328-
$imagick = new Imagick();
329-
$imagick->setBackgroundColor(new ImagickPixel('transparent'));
327+
// save SVG in a unique filename
328+
$filename = md5($svg) . ".svg";
329+
file_put_contents($filename, '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svg);
330330

331-
// add svg image
332-
$imagick->setFormat('svg');
333-
$imagick->readImageBlob('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svg);
334-
$imagick->setFormat('png');
331+
// create Imagick object from SVG
332+
$imagick = new Imagick($filename);
333+
$imagick->setImageFormat("png");
334+
$imagick->setBackgroundColor(new ImagickPixel('transparent'));
335335

336336
// echo PNG data
337337
header('Content-Type: image/png');
@@ -340,4 +340,7 @@ function echoAsPng(string $svg): void
340340
// clean up memory
341341
$imagick->clear();
342342
$imagick->destroy();
343+
344+
// delete temporary file
345+
unlink($filename);
343346
}

0 commit comments

Comments
 (0)