…but not kissing.  That would be like kissing a red-headed cousin or something.

As you do with anything programmig related you jump straight in at the deep end. In this case I wanted to take SVG input, and output a JPG, from PHP.

So taking a look around there wasn’t much to choose from:

  • GD for PHP can take and output practically anything, but not SVG. And unfortunately its vector object model doesn’t seem to be as expressive as SVG is. There is GD::SVG, which is a perl module. Perl is enabled on the server (we have a horrible perl script for backing up the DB every hour but that is another post) but I’d rather not mess with a language I know nothing about.
  • Call ImageMagick and have that do the conversion for us. While actually not that difficult, the cost of starting ImageMagick for each image is prohibitive – while you won’t get 300 people all trying to convert images at the same time, it is possible to get 10 or so clumped together.
  • Call Batik to handle the conversion. I’ve used Batik and, if you ignore the first time cost of starting a JVM, it is wonderfully responsive. Unfortunately it is written in Java and Space is written PHP.

I know Java better than Perl, I decided to go with the Batik option.  This means enabling the PHP-Java extension, which is simply editing the ini file to uncomment out the line that reads extension=php_java.dll.

In addition to that you need to edit the [Java] section of the ini file. And that is where things get problematic. If you read the scant information on installation you will find that the last known installation was with Java 1.4. I have Java 1.6.14. I found a pretty detailed instructions on the O’Reilly site for Java 1.2. Shouldn’t be too different should it? So I configured it as best I could and ran a simple test program:


ini_set(“display_errors”, “1”);

$systemInfo = new Java(“java.lang.System”);

print “Total seconds since January 1, 1970: “.$systemInfo->currentTimeMillis();

print ” Java Version: “.$systemInfo->getProperty(“java.version”);


The output was Fatal error: Unable to load Java Library C:\Program Files\Java\jdk1.6.0_14\jre\bin\client\jvm.dll, error: The specified module could not be found. in C:\Develop\projects\zerosumgames\php\map\htdocs\java.php on line 6 which is never pleasant. So I pushed and poked with the ini file settings – there are only 4 variables so there isn’t much to play with, but nothing would make it work.

So it sat on a back burner for a few days, nagging at the back of my mind: What if I used the JVM that they used themselves? That should work shouldn’t it? So I rooted around on Sun’s site and found what I was looking for: An archive of the all the old JDKs!

So I grabbed the last 1.2, 1.3 and 1.4 JDK’s (after reading very, very carefully Suns EOL agreement) and began by installing Java 1.2 and configuring the ini file:
java.home = "C:\Program Files\Java\jdk1.2.2\bin"
java.library = "C:\Program Files\Java\jdk1.2.2\jre\bin\classic\jvm.dll"

Bingo the output was Total seconds since January 1, 1970: 1248652639311 Java Version: 1.2.2.

Java1.3 went on – and worked too. This is exciting as Batik needs a Java 1.3 runtime (or compatible) to work: Total seconds since January 1, 1970: 1248652802706 Java Version: 1.3.1_20

What about Java 1.4? This gives Batik a speed increase and allows Batik to use additional features (though I don’t really care about mouse-wheels): Total seconds since January 1, 1970: 1248652920024 Java Version: 1.4.2_19

You beauty! So why doesn’t 1.6 work? Surely it is possible? I know the JVM has moved from the classic folder into a client and server folder, but the internals must surely be the same. I pointed at the correct location, but are there any other 1.6 JVMs beside the client and server ones? Well there is the one that exists in jre6 folder put that produces the same error as before.

And finally 1.5? Well I never tried that one – I have a working PHP-Java bridge – and now I need to move on to bigger & better things.

While researching this post I came across Image_Canvas which might do what I want it to and entirelly in PHP – no Java required. However the problem with the documentation is that it isn’t very thorough and your are pretty much on your own. If I get stuck with Java & Batick then I will probably take a closer look but until then…tada!