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.
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