Posts

Showing posts with the label Imagemagick

Convert SVG To Transparent PNG With Antialiasing, Using ImageMagick

Answer : As a side note, I found that getting transparency was a bit tricky. Instead of using transparent , I had to use none . convert -background none in.svg out.png Inkscape will do this: inkscape \ --export-png=out.png --export-dpi=200 \ --export-background-opacity=0 --without-gui in.svg Update The terminology has changed: all the export params suppress gui, and the output parameter is now simply based on the file type. For example, a type of png will cause a file in /path/to/picture.svg to be exported as /path/to/picture.png (caution: this overwrites output). inkscape \ --export-type=png --export-dpi=200 \ --export-background-opacity=0 picture.svg Note cited wiki has quotes on --export-type=png , which is incorrect. Also if don't have Inkscape command line, MacOS can access via bash directly: /Applications/Inkscape.app/Contents/MacOS/inkscape Actually, reading imagemagick documentation: -antialias Enable/Disable of the rendering of anti-aliasing pix...

Convert SVG Image To PNG With PHP

Image
Answer : That's funny you asked this, I just did this recently for my work's site and I was thinking I should write a tutorial... Here is how to do it with PHP/Imagick, which uses ImageMagick: $usmap = '/path/to/blank/us-map.svg'; $im = new Imagick(); $svg = file_get_contents($usmap); /*loop to color each state as needed, something like*/ $idColorArray = array( "AL" => "339966" ,"AK" => "0099FF" ... ,"WI" => "FF4B00" ,"WY" => "A3609B" ); foreach($idColorArray as $state => $color){ //Where $color is a RRGGBB hex value $svg = preg_replace( '/id="'.$state.'" style="fill:#([0-9a-f]{6})/' , 'id="'.$state.'" style="fill:#'.$color , $svg ); } $im->readImageBlob($svg); /*png settings*/ $im->setImageFormat("png24"); $im->resizeImage(720, 445, imagick...

Converting A PDF To PNG

Answer : You can use one commandline with two commands ( gs , convert ) connected through a pipe, if the first command can write its output to stdout, and if the second one can read its input from stdin. Luckily, gs can write to stdout ( ... -o %stdout ... ). Luckily, convert can read from stdin ( convert -background transparent - output.png ). Problem solved: GS used for alpha channel handling a special image, convert used for creating transparent background, pipe used to avoid writing out a temp file on disk. Complete solution: gs -sDEVICE=pngalpha \ -o %stdout \ -r144 cover.pdf \ | \ convert \ -background transparent \ - \ cover.png Update If you want to have a separate PNG per PDF page, you can use the %d syntax: gs -sDEVICE=pngalpha -o file-%03d.png -r144 cover.pdf This will create PNG files named page-000.png , page-001.png , ... (Note that the %d -counting is zero...