View Single Post
06-03-14, 01:46 PM   #1
Digital_Utopia
A Flamescale Wyrmkin
 
Digital_Utopia's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2012
Posts: 110
Making PHP (and gd) do tricks

Because converting just takes too much time.

Code:
<?
$fh=imagecreatefromblp("CHARACTER/BloodElf/HAIR00_00.BLP");
imagesavealpha($fh,true);
imagealphablending($fh, 1);
header('Content-Type: image/png');
imagepng($fh);
?>
This is the code for the first successful test of an upcoming PHP extension, that uses the same code used in my other BLP related projects, to convert the format into a gd compatible image resource, allowing you to work with it, and output it to a web-compatible format on the fly.

For instance, let's say that php file is called "BLP.php". With that script, you could essentially do this on a web page:

Code:
<img src="BLP.php"/>
and your image will appear like any other. Or, if you wanted something a little bit more flexible...

Code:
<?
$im=$_GET['f'];
$fh=imagecreatefromblp($im);
imagesavealpha($fh,true);
imagealphablending($fh, 1);
header('Content-Type: image/png');
imagepng($fh);
?>
and then you could do

Code:
<img src="BLP.php?f=CHARACTER/BloodElf/HAIR00_00.BLP"/>
fun stuff, right? So, now you might be asking...

Do you have some kind of weird ADHD, that just makes you jump around random projects?

And the answer is...kinda.

Believe it or not, this is actually related to that model viewer I mentioned below, and if you're prepared for a wall of text, keep on reading.

First and foremost - JavaScript is awesome, and as far as scripting languages go, it's pretty damn fast. Unfortunately, when it comes to parsing file formats, it's far, far too slow. And PHP itself is even worse.

So, I could go ahead and convert the models to the end format, but that means every time there's an update, I have to reconvert the model. And although a project should be maintained, there's a difference between maintenance, and babysitting.

So, then I thought - what's superfast? Compiled code. How to get compiled code running in a web environment, when using PHP? PHP Extensions.

So, cool. I'll make a PHP extension that does the parsing! But that still requires extracting from the game archives, and uploading a metric ton of files. So, not so cool.

But! What if I made an extension that would load up all the MPQ files on init (well, the handles to them - i.e. using StormLib), and any calling script could grab the resource(s) it needs, by simply passing it the requested file path? Now, that would be pretty sexy. But where to start? I've never written an extension before - and have no idea if it's even possible to pass resources between extensions!

But...gd uses a resource, and I have a BLP library... So why not see if I can't make a BLP extension that provides an image resource, that gd will accept? If I can pull that off, everything else is just applying the same methods.

And that, above - is step 1.
__________________

Last edited by Digital_Utopia : 06-03-14 at 01:48 PM.
  Reply With Quote