2
votes

I'm trying to allow users to upload an SVG image through a CMS and generate a fallback PNG so that when the CMS displays the content on the front end the PNG fallback is shown for older browsers.

The problem is that when I upload an SVG the PNG output is a bit off, gradients seem to be missing as shown in the screenshot below.

SVG on the left PNG on the right

In these tests I'm just using ImageMagick commandline as below but have tried it through the CMS upload using Imagick with the same result.

convert gallardo.svg gallardo.png

Imagick version sets background to transparent as suggested in other threads but PNG output is the same as commandline.

class ResampleSvgUpload extends DataExtension {

    function onAfterUpload() {
        if($this->isSvg()){
            $this->resample();
        }
    }

    function onAfterWrite() {
        if($this->isSvg()){
            $this->resample();
        }
    }

    function isSvg() {
        $extension = strtolower($this->owner->getExtension());
        return ($extension == 'svg') ? true : false;
    }

    function resample() {
        $original = $this->owner->getFullPath();
        $resampled = $original . '.png';    
        $imagick = new Imagick($original);
        $imagick->setBackgroundColor(new ImagickPixel('transparent'));
        $imagick->setImageFormat('png');
        $imagick->writeImage($resampled);
    }
}

I'm using:

  • ImageMagick 6.9.0-3
  • Imagick 3.1.0RC2
  • PHP 5.3.29
  • OSX Yosemite 10.10
1
If the answer below doesn't help, can you post the result of the output with the verbose command enabled, as well as post an example pdf?Danack

1 Answers

4
votes

ImageMagick does not do the conversion of SVG itself, instead it delegates that task to a 3rd party program. You can find which program is being used by adding the -verbose option to the convert command.

The issue will almost certainly be being caused by a bug in that underlying program - and will hopefully go away when you upgrade to a newer version of it.