Convert SVG to PNG / JPEG / BMP and vice versa?

PHP code for converting SVG to other image formats and vice versa

+2
php image svg
source share
2 answers

Well, you probably want to use ImageMagick or GD . But keep in mind that you cannot convert from PNG / JPEG / BMP to SVG, which is a one-way street.

Edit: development

Converting an SVG (vector) image to a PNG (raster) image is trivial. ImageMagick can do this for sure, and I'm pretty sure GD should be able to. If not, there are also PHP bindings for rsvg.

Converting PNG (bitmap) to SVG (vector), on the other hand, is a whole problem. The core of the problem is that the bitmap quite simply contains less information than the vector image: information about the shapes, lines and structure, in particular, is smoothed to the pixel vertex and irretrievably lost. In some cases, this information may be recovered, but will never be recovered or rediscovered.

Vectorization is just a method of creating a new vector image based on a raster image when the computer tries to detect lines and shapes. This problem is far from trivial, and this process, of course, is not ideal and is not even reliable in automated configuration.

Only John Skeet can convert PNG to SVG.

Reedit: Auto Tracing and Personal Experience

Some time ago, I really worked a bit on tracing a bitmap to prototype a small web application ( find it here, MNSFW, it requires FFx / Safari / Opera ). The vector was created using potrace to repeatedly track the threshold black and white image to select elements and then reconstruct the image manually. It was a grueling process, but at that time I did not work and had nothing better.

Point: automatic tracing of raster images to create vector images at best is hits and misses, and to get good results reliably requires

  • Good source image
  • A lot of effort
+5
source share

Several options

  • embed image as dataURI. This is technically not a conversion, though, but a bunch on online converters works just that.

  • convert bitmap to SVG pixel by pixel. It will not be scalable and the file size will be huge, so it will also not be optimal.

  • You interact with AutoTrace (through the system ) to analyze the images you want to convert to get an approximate. Inputformats are BMP, TGA, PNM, PPM, PGM, PBM and those supported by ImageMagick. Exportformats - Postscript, svg, xfig, swf, pstoedit, emf, dxf, cgm, mif, p2e and sk. AutoTrace is FOSS.

However, if you go with 3, do not expect the result to look too strong as the input after that. As others have already pointed out, you will lose detail during the conversion process.

+2
source share

All Articles