tags:

views:

63

answers:

2

Code in PHP to convert SVG into Other Image Formats and opposite??

+2  A: 

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

  1. A good source image
  2. A lot of effort
Williham Totland
Hmmm There is no way at all ? to convert from any image to SVG maps??
PhpSeeker
@PhpSeeker: Updated my answer with some elaboration.
Williham Totland
@PhpSeeker There are some fairly complicated ways of doing it, but the results are rarely that brilliant. http://vectormagic.com/ has a good conversion engine, as do some of the Photoshop products, from what I remember, but they still require human intervention for the best results. Producing a vector image from a raster image is *hard*.
Matt Gibson
SVGs can contain bitmaps, so it is possible to convert PNG, etc., to SVG perfectly. However, if you would do a SVG-to-bitmap-to-SVG roundtrip, you would end up with a completely different kind of SVG than the original, because tonnes of information are lost during the first conversion, of SVG to bitmap. For that conversion (SVG to bitmap), look into librsvg; I don't know if it's available from PHP.
gpvos
@gpvos Yes, good point, you can store a raster image inside an SVG. But of course, the question that springs to mind at that point would be "why?"...
Matt Gibson
@gpvos: MediaWiki has an option to use rsvg, so I assume it has PHP bindings. And an SVG file containing nothing but an included PNG doesn't really count as a conversion, as such. The image is still the PNG file.
Williham Totland
But you can then do additional things to the PNG like rotation, scaling and skewing.
graham.reeds
@graham.reeds: True; but that can also be done with the same libraries that you would use to convert an SVG into a PNG in the first place, or you could do it to the original SVG before converting it, for better results.
Williham Totland
Lots of Thanks for Support!!
PhpSeeker
@Williham Totland: I never thought anyone would seriously consider an SVG-to-PNG-to-SVG roundtrip, I just wanted to make something clear about the nature of bitmaps and vector images that I thought was not entirely clear to the original poster.
gpvos
There's a pretty good tracer built into inkscape.
echo-flow
@echo-flow: Pretty good is a stretch, but yeah, there definitely *is* a tracer built into inkscape. Kind of hard to access from PHP, but it's there.
Williham Totland
The engine inkscape uses for tracing raster images is called potrace, and that should be rather simple to access in PHP since it's a commandline app: http://potrace.sourceforge.net/
Erik Dahlström
@Erik Dahlström: potrace has two problems, one minor, one not so much: Minor: It only supports a few image formats, but conversion is easy. Major: It only supports black/white images. That's a pretty big hurdle right there. AFAIK, InkScape overcomes the second hurdle by using a technique similar to what I described, as well as by tracing on single channels to form a composite.
Williham Totland
@Williham-Totland I'm not suggesting using either of autotrace or potrace to automatically produce an aesthetically pleasing result is easy, just saying that you can quite easily access these commandline tools from PHP.
Erik Dahlström
+1  A: 

Several options

  1. embed the image as a dataURI. That's technically not a conversion though, but a bunch on online convertors do work that way.

  2. 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.

  3. 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.

Gordon
1: Then why use SVG? 2: No, it won't. It won't be any more scalable than a raster file. In fact, if anything, it'll be *less* scalable, because it prevents interpolation that would usually occur, making the approach completely and utterly pointless in *every single way imaginable*.
Williham Totland
@Will 1. that wasnt the question, 2. you got a point. I corrected that part. I've also added another possible approach, so please consider removing your DV now.
Gordon
My point vis-a-vis 2 stands, but in light of 3, I rescind the DV.
Williham Totland
Lots of Thanks for the Support...!!
PhpSeeker
@Will thanks and I agree 1 and 2 is not feasible, but, well, it's possible. @phpseeker you're welcome.
Gordon