Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert SVG to image (JPEG, PNG, etc.) in the browser

Tags:

javascript

svg

I want to convert SVG into bitmap images (like JPEG, PNG, etc.) through JavaScript.

like image 608
Zain Avatar asked Oct 20 '10 07:10

Zain


People also ask

How do I convert SVG to PNG locally?

Navigate to an . svg file, right click on it and click on the context menu item 'Save SVG as PNG. Lets you click on the extension icon or right click on an . svg file and choose Save SVG as PNG.

How do I turn an SVG into a JPEG?

Go to File. Select Export. Click Export As. Select JPG format from the drop-down menu.

Can you turn an SVG into a PNG?

Use Adobe Photoshop to convert SVG images to PNG images. Get lossless compression, preserve image resolution, and lower your file size by converting from SVG to PNG. Use Photoshop for a fast way to turn large file formats into smaller, easier-to-use graphic types. Open your SVG file in Photoshop.

What is the best free SVG converter?

RealWorld Paint can be considered the best free SVG converter whose main function is to create vector graphics and illustrations, add touchups to the pictures, etc.


2 Answers

Here is how you can do it through JavaScript:

  1. Use the canvg JavaScript library to render the SVG image using Canvas: https://github.com/gabelerner/canvg
  2. Capture a data URI encoded as a JPG (or PNG) from the Canvas, according to these instructions: Capture HTML Canvas as gif/jpg/png/pdf?
like image 60
jbeard4 Avatar answered Oct 08 '22 09:10

jbeard4


jbeard4 solution worked beautifully.

I'm using Raphael SketchPad to create an SVG. Link to the files in step 1.

For a Save button (id of svg is "editor", id of canvas is "canvas"):

$("#editor_save").click(function() {  // the canvg call that takes the svg xml and converts it to a canvas canvg('canvas', $("#editor").html());  // the canvas calls to output a png var canvas = document.getElementById("canvas"); var img = canvas.toDataURL("image/png"); // do what you want with the base64, write to screen, post to server, etc... }); 
like image 36
coop Avatar answered Oct 08 '22 09:10

coop