This is a tutorial that I wrote quite a while ago and then forgot about. A number of things have changed since I wrote it (such as the release of two additional versions of flash), but other than that, the general concept introduced here still stands.
This technique addresses a number of issues that have plagued web developers for a long time.
Issue #1: JPEG’s complete lack of transparency options.
Issue #2: GIF’s inflexible transparency options (no alpha channels or anti-aliasing)
Issue #3: Spotty support for PNG files (which *do* have transparency)
Issue #4 Lack of compression options for PNG files. (PNG is a “loss-less” format so a simple 300×300 PNG image could be like 250K, where a JPEG of the same thing is only 30K.)
So without further ado, here’s one of my fun little PNG workarounds I have discovered and used in the past. Heck, you can even do it yourself fairly easily!
- Photoshop, the GIMP, or equivalent image editing tool
- Macromedia Flash MX (the editor, not the plugin)
- HTML editor of some sort
Follow these steps to create a transparent PNG file:
- Open up photoshop and create a new 500×500 pixel (72 dpi) image with a transparent background.
- Create a new layer and scribble, draw, or write random things all over it. Make sure that there are plenty of blurred edges and semi-transparent things showing through to the next layer.
- Click File > Save for Web.
- Under the Settings drop-down of presets, select “PNG-24” and make sure “Transparency” is checked. For the image, you should see a white and gray checkerboard pattern with your scribbles and what not overlaying it.
- Save file off as “testimage.png”
Ok. Now go right click on that file in windows explorer and view the properties. Mine was over 200K. Geez! Definitely in need of compression or something!!!
Load the PNG into Flash and save it for the web with compression:
- Open up the Macromedia Flash MX editor.
- Create a new document and set the size to 500×500 pixels.
- Go up to File > Import and open the “testimage.png” file we created earlier.
- Go to File > Save As and save the document as “testimage.fla”.
- Go to File > Publish Settings.
- On the Formats tab, we only need the Flash (.swf) and HTML (.html) items checked.
- On the Flash tab, the default settings should be ok, but you may want to adjust the JPEG quality up to like 75 or something.
- On the HTML tab, select “Flash Only” from the template drop-down, and set your window mode to “Transparent Windowless”. (**THIS IS VERY IMPORTANT!!! DO NOT LEAVE OUT THIS STEP!**)
- Click the Publish button.
Ok. Done! Now close out of there, save the testimage.fla file, close out flash, whatever…
Go back to windows explorer and look at the folder where you saved testimage.fla.
You should see two new files: testimage.swf, and testimage.html.
Open up testimage.html in your web browser (either IE or Firefox, preferably).
Bam! There you have it! Your PNG file still has all its transparency, yet it displays perfectly in 98% of all the web browsers out there (basically anyone who has the flash plugin). And to top it off, it has JPEG compression as well. Check it out, right-click on testimage.swf and look at your properties. Wow! Only around 100K (as opposed to over 200K as a plain PNG)
Try putting a tiling background image of some sort in the testimage.html file to see how the alpha transparency works. You can also put the flash in a DIV or SPAN on it’s own layer above everything else and it’ll work great, too.
Another thing to try:
Try slapping a flash-based loader bar on each one of your PNG flash files. Your HTML page will now load completely by itself, and then all the PNG files will load separately in place with their own little loading status bars. Snazzy!
5 responses to “How to use transparent PNG files on your web site (with JPEG compression)”
Very snazzy indeed. Not a totally “PNG” solution (in that you end up with a Flash file) but snazzy nonetheless.
Yes, all the images would essentially be flash files. However, it’s not as lame as you might think once you get used to the idea. These embedded flash images load basically just as fast as normal images. 98% of all web viewers have the flash plugin installed (according to recent statistics), so there’s really nothing to lose.
This technique’s biggest selling point is the fact that it allows you to compress PNG files, getting you JPEG download performance.
But still one rather big problem, you can’t use the png/flash as a background image.
You’re right. That is the one downside to PNG still. However, the big advantage of PNG is transparency, which doesn’t make sense for a page background anyways.
May not make sense for a page background, but backgound-images can be used on nearly all html elements.
By placing layout images in backgrounds you further seperate (x)html, css and programming. And (x)html will be stictly content and stucture, css will include your transpernt png images and all the other layout things. You would also avoid png’s(used for layout) beening printed.