PHP IMAGICK - Conversion from SVG to PNG results in broken images

1.6k views Asked by At

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.

  1. Example images before and after conversion:

screenshot svg image

converted png image

  1. Example before and after conversion:

screenshot svg image 2

after conversion to png 2

Any ideas? Thanks!

0

There are 0 answers