Code in PHP to convert SVG into Other Image Formats and opposite??
Well, you'll probably want to use the ImageMagick or GD functions. But be aware that you can't convert from PNG/JPEG/BMP to SVG, that's a one-way street.
Edit: Elaboration
Converting an SVG (vector) image to a PNG (bitmap) image is trivial. ImageMagick can do this for sure, and I'm fairly certain GD should be able to as well. If not, there are also PHP bindings for rsvg.
Converting PNG (bitmap) to SVG (vector), on the other hand, is a whole 'nuther problem. The core of the issue is that a bitmap image quite simply contains less information than a vector image: Information about shapes, lines and structure in particular is flattened to pixel goop and irretrievably lost. In some cases, this information can be reconstructed, but never actually retrieved or rediscovered.
Vectorization is merely a method for creating a new vector image based on a bitmap image, having the computer attempt to discover lines and shapes. This problem is far from trivial, and the process is certainly not perfect, nor even reliable in an automated setting.
Only Jon Skeet can convert a PNG into an SVG.
Reedit: Autotracing and personal experience
Some time back I actually did do some work on tracing a raster image for some prototype-building of a small web-app (find it here, MNSFW, requires FFx/Safari/Opera). Creating the vector was done using potrace to repeatedly tracing a thresholded black/white image to isolate elements and subsequently reconstructing the image by hand. It was a grueling process, but I was out of work at the time and had nothing better to do.
Point is: Automatic tracing of raster images to produce vector images is hit-and-miss at best, and reliably getting good results requires
- A good source image
- A lot of effort
Several options
embed the image as a dataURI. That's technically not a conversion though, but a bunch on online convertors do work that way.
convert the raster image to SVG pixel by pixel. This will not be scalable and the filesize will be huge, so it's not that optimal either.
You interface with AutoTrace (via
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 are Postscript, svg, xfig, swf, pstoedit, emf, dxf, cgm, mif, p2e and sk. AutoTrace is FOSS.
However, if you go with 3, dont expect the output to look too much like the input afterwards. Like others pointed out already, you will lose details during the conversion process.