Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

PHP code to resolve PNG transparency issues

Go to End

5 Posts   2195 Views


Community Member, 17 Posts

26 May 2010 at 4:57pm

I'm working on a website which will primarily be displayed in IE6. The design requires the use of transparent PNG images, and a solution I've used previously is a PHP script. The requirements of the script are as follows:

1. Before EVERYTHING in the entire site, the following function must be called:


2. After EVERYTHING in the entire site, the following must be inserted:

echo png_fix(ob_get_clean());

As you can see, I've placed the PNG fix script at /mysite/code/png_fix.php. The two changes above were inserted into /mysite/code/Page.php (at the beginning and end, obviously).

This doesn't seem to have any effect. I've used this script on a number of other websites with no problems. Additionally I've tried some JavaScript solutions in the past, with mixed success. The best JavaScript solution I've used (unitpngfix.js) in this case destroys the site layout (elements appear all over the place).

So, my question is: Have I inserted the PHP-based PNG transparency fix script in the right place?


Community Member, 904 Posts

26 May 2010 at 6:35pm

It seems like your PHP Code is capturing the whole page output and parsing it for PNGs.
That won't work inside Page.php, since that file holds the model and the controller and is not responsible for output. Your best bet would be to modify the template engine...

Using a PHP solution for a PNG Fix seems to be rather awkward. It adds a lot of overhead to your page processing and does basically the same as a JavaScript (works on the HTML-Output). If I were implementing a PHP solution (for SilverStripe) I would probably subclass the "Image" class and handle the issue there.

From my experience there are some very good JS implementations out there.. did you try this one:


Community Member, 17 Posts

26 May 2010 at 7:58pm

That's correct - it needs to capture the entire page's output. I'm not particularly concerned about the overheads/processing as this is an extremely small website that I would estimate will only pull in about 1000 hits per year, tops. This particular script is the only perfect solution I've ever come across - no bugs, no workarounds, and nothing to do except add those three lines of code.

I haven't tried the JS solution you provided but it seems like a bit of stuffing around to get it working (explicitly specifying elements etc) and I really want to get this site published this week. The client is every designer's worst nightmare lol

As a SS novice, and definitely not a programmer by any means, how would I go about modifying the template engine as per your suggestion?


Community Member, 904 Posts

27 May 2010 at 2:05am

Well, this is a really ugly hack and the community here will probably kill me for even posting something like this, but here we go:
Add your code to the sapphire/main.php file, like you did with Page.php (ob_start(); at the top, rest at the bottom of the file).

That's it.

Please note: This code will affect all pages generated by SilverStripe, even the Admin section etc..
Also: Since you're messing with core files here, you can no longer update your SilverStripe installation easily. You would always have to hack main.php after an update.
I sincerely hope you find some time to further evaluate the JS solutions ;)


Community Member, 17 Posts

27 May 2010 at 1:27pm

Thanks! Unless there are major security issues, I doubt I'll ever upgrade this site (at least in its current form...). Hopefully IE6 is well and truly dead by that point :)