www.webdeveloper.com
Results 1 to 5 of 5

Thread: SVG to transparent PNG with imagick

Hybrid View

  1. #1
    Join Date
    May 2010
    Location
    Connecticut
    Posts
    19

    SVG to transparent PNG with imagick

    I am trying to dynamically generate images from svg files using imagemagick and the imagick wrapper, but the converted png's always have white backgrounds rather than transparent. Does anyone know how to make them transparent without having to erase all the white areas?

    Heres my code

    PHP Code:
    $im = new Imagick();
    $svg file_get_contents("design.svg");
    $svg str_replace(array("color1","color2"),array("red","lightblue"),$svg);
    $im->readImageBlob($svg);
    $im->setImageBackgroundColor(new ImagickPixel('transparent'));
    $im->setImageFormat("png24");

    header("Content-Type: image/png");
    echo 
    $im

  2. #2
    Join Date
    May 2010
    Location
    Connecticut
    Posts
    19
    bump?

  3. #3
    Join Date
    Aug 2010
    Posts
    1
    If there's any other poor sod out there looking for the answer to this one (I was...)

    Here's the proper way to do it:

    Code:
    <?
    $im = new Imagick();
    $im->setBackgroundColor(new ImagickPixel('transparent'));
    $svg = file_get_contents("path/to/test.svg");
    $im->readImageBlob($svg);
    
    $im->setImageFormat("png32");
    
    header('Content-type: image/png');
    
    echo $im;
    ?>
    Note the "setBackgroundColor" first, the order matters.
    Also note the image format is "png32".

    Hope this helps someone...

  4. #4
    Join Date
    May 2010
    Location
    Connecticut
    Posts
    19
    Wow! That works perfectly! Thank you for your help, I had given up on this problem!

  5. #5
    Join Date
    Nov 2005
    Location
    India
    Posts
    336

    Opacity

    When I use your code opacity of svg elements is not working.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles