PDA

View Full Version : PNG transparency in IE/Win


mnkeybsness
Dec 8, 2003, 12:56 PM
After searching what seemed like forever, I found a little hack to make IE on Windows allow alpha transparency in PNG images.

I'm sure someone will ask: "Why not just use a GIF?"...
The answer is simple... take a look...
Sample images have been lost

you should download the images and try placing a different color background on them than white. you will then notice that the GIF does not allow true alpha transparency and creates a white border around the image.

all you need to do to make this work is to add this little javascript to your page:
<!--[if gte IE 5.5000]>
<script type="text/javascript" src="pngfix.js"></script>
<![endif]-->


add the following into a monospace text file and name it pngfix.js and place it in the root directory:
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
{
for(var i=0; i<document.images.length; i++)
{
var img = document.images[i]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText
var imgAttribs = img.attributes;
for (var j=0; j<imgAttribs.length; j++)
{
var imgAttrib = imgAttribs[j];
if (imgAttrib.nodeName == "align")
{
if (imgAttrib.nodeValue == "left") imgStyle = "float:left;" + imgStyle
if (imgAttrib.nodeValue == "right") imgStyle = "float:right;" + imgStyle
break
}
}
var strNewHTML = "<span " + imgID + imgClass + imgTitle
strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
strNewHTML += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
i = i-1
}
}
}
window.attachEvent("onload", correctPNG);

FattyMembrane
Dec 8, 2003, 01:34 PM
God bless you!:)

the more web-design i do, the more i hate ie. even dillo can handle png files correctly.

Rower_CPU
Dec 9, 2003, 10:09 PM
Interesting...

I'm going to have to try this at work tomorrow. :)

mrjamin
Dec 22, 2003, 05:32 AM
I posted a similar find relatively recently (http://forums.macrumors.com/showthread.php?s=&threadid=34092&highlight=png).

Here's the code i use:


// === SLEIGHT.JS CODE (Copyrightę YoungPup) WITH MODIFIED CSS-INLINE IMAGE SUPPORT ========================
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
document.writeln('<style type="text/css">img { visibility:hidden; } </style>');
window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
if (itsAllGood && img.src.match(/\.png$/i) != null) {
var src = img.src;
img.style.width = img.width + "px";
img.style.height = img.height + "px";
img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
img.src = "images/spacer.gif"; // change this to your own gif
}
img.style.visibility = "visible";
}
}

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
window.attachEvent("onload", alphaBackgrounds);
}

function alphaBackgrounds(){
var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
for (i=0; i<document.all.length; i++){
var bg = document.all[i].currentStyle.backgroundImage;
if (itsAllGood && bg){
if (bg.match(/\.png/i) != null){
var mypng = bg.substring(5,bg.length-2);
document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
document.all[i].style.backgroundImage = "url('images/spacer.gif')"; // change this to your own gif
}
}
}
}


That also works for PNGs applied as background images (even in external stylesheets). All you have to do is change all mentions of 'images/spacer.gif' in the code above to the location of a 1px*1px transparent GIF.

warning: if you use a DIV (for example) with a PNG24 background image, and that DIV has a hyperlink in it, the hyperlink will become unclickable since the JS works by overlaying the transparent GIF over the element with the PNG. Weird that; someone exploits a bug in IE which fixes another bug.

vembusridhar
Oct 24, 2005, 07:07 AM
Hello All
If any body has solution to the bottom-most "warning" section of this posting, please let me know. Unfortunately I'm having the exact issue as mentioned there :(

Thanks
Sri