I am trying to convert svg images to png through Imagick. I did it "successfully" and images were converted but they seems broken with different colors. Here is my code sample and image examples before and after conversion.
$image = new \Imagick();
$svgImage = file_get_contents($tmpFilePath);
$image->setImageBackgroundColor(new \ImagickPixel('transparent'));
$image->readImageBlob($svgImage);
$image->setImageFormat("png32");
$pngtmpFileName = pathinfo($tmpFilePath, PATHINFO_FILENAME).'.png';
$pngtmpFilePath = $this->_directory->getRelativePath($this->downloadDir . '/' .
$image->writeImage($pngtmpFilePath);
header('Content-type: image/png');
$image->clear();
$image->destroy();
The same code sample is all over the internet and it looks like this is the only way to convert svg to png with php but it is not working.
- Example images before and after conversion:
- Example before and after conversion:
Any ideas? Thanks!