Click here to Skip to main content
15,891,721 members
Articles / Programming Languages / Javascript
Article

PNG alpha support for Internet Explorer

Rate me:
Please Sign up or sign in to vote.
4.47/5 (11 votes)
15 Sep 2004 77.6K   16   15
An article on how to implement PNG alpha support for Internet Explorer

Introduction

Internet Explorer doesn’t support the alpha channel in PNG images. However the fix is to use the "AlphaImageLoader" filter. This script replaces all PNG images on the page with a blank image (blank.gif) and adds the "AlphaImageLoader" filter with the original image source. The blank image is to avoid the "broken image" picture to display above the filter. This script also supports printing and changing the source image at runtime.

Source

JavaScript
//Array containing all PNG images on the page
var PNGimageArray = new Array();
var isPrinting = false;

//Path to the blank image (1x1 transparent)
var blankSrc = "blank.gif";

//Captures print events
window.attachEvent("onbeforeprint", function () { beforePrint(); } );
window.attachEvent("onafterprint", function () { afterPrint(); } );
//Tests if element is a PNG image, and if so fixes it
function addPngImage(element){
  if (/\.png$/i.test(element.src)) {
    fixImage(element);
    element.attachEvent("onpropertychange", function () 
      { propertyChanged(); } );
    PNGimageArray[PNGimageArray.length] = element;
  }
}

//Applies filter and changes source to blank
function fixImage(element) {
  element.runtimeStyle.filter = 
   "progid:DXImageTransform.Microsoft.AlphaImageLoader(
      src='" + element.src + "')";
  element.src = blankSrc;
}

//If property "src" is changed fixs image (not 
//if it is changed to blank though)
function propertyChanged() {
  if (isPrinting) return;
  var element = event.srcElement;
  var pName = event.propertyName;
  if (pName != "src") return;
  if (!new RegExp(blankSrc).test(element.src))
    fixImage(element);

}

//Turns image back to original before print (Explorer can't print filters)
function beforePrint() {
  isPrinting = true;
  var element;
  for(var i = 0; i < PNGimageArray.length; i++){
    element = PNGimageArray[i];
    element.src = element.filters[0].src;
    element.runtimeStyle.filter = "";
  }

}

//Fixes image after print
function afterPrint() {
  isPrinting = false;
  var element;
  for(var i = 0; i < PNGimageArray.length; i++){
    element = PNGimageArray[i];
    fixImage(element);

  }
}
Style that makes all images run the addPngImage function. I use "expression" instead of "behavior" to avoid some security issues in Internet Explorer.
HTML
<style type="text/css"> img { filter:expression(addPngImage(this)); }
</style>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
Software developer

Comments and Discussions

 
GeneralFIX for resizing images Pin
Max Hamner8-Dec-07 18:29
Max Hamner8-Dec-07 18:29 
Nice library. Better than the other one I was using (IE5.5+ PNG Alpha www.twinhelix.com) because the images are not non-transparent durring load.

But I wanted to be able to stretch PNGs, such as image based backgrounds. So I added a definition for sizemethod. I set it to stretch, but then images that did not have a defined size in their tag (size from image attribute) do not size correctly.

I noticed that the image height &amp; width are valid if you view them even though it doesn't work, apparently with stretch they get resized to the transparent gif size.

But adding the two lines that set the height and width to themselves allows this to work properly for all images, those that have declared values like when you want to stretch them, but also for images that are relying on the image's size.

function fixImage(element) {
// required so images with no size tag still work with "scale"
element.width = element.width;
element.height = element.height;
// added sizing method.
element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + element.src + "',sizingMethod='scale')";
element.src = blankSrc;
}

Hope this helps others!
max
maxhamner@hotmail.com
QuestionWhat license? Pin
chappi120-Sep-06 3:55
chappi120-Sep-06 3:55 
GeneralGreat Work But Resizing images doesnt work!!! Pin
Pier...29-Jun-06 1:28
Pier...29-Jun-06 1:28 
GeneralResource Protocol ! Pin
Nicolas MEURET13-Feb-06 22:49
Nicolas MEURET13-Feb-06 22:49 
GeneralMaps Pin
EDB_ENVC17-Jun-05 4:09
EDB_ENVC17-Jun-05 4:09 
GeneralRe: Maps Pin
noname987631-Jul-05 0:56
noname987631-Jul-05 0:56 
GeneralFix for filenames with "(" or ")" Pin
PolyVector17-Feb-05 12:03
PolyVector17-Feb-05 12:03 
QuestionPNG Background images??? Pin
dclark11-Jan-05 14:26
dclark11-Jan-05 14:26 
GeneralCross-Browser Compatibilty Pin
Davy Boy29-Nov-04 11:53
Davy Boy29-Nov-04 11:53 
GeneralIt is a problem Pin
sanjit_rath17-Oct-04 8:33
sanjit_rath17-Oct-04 8:33 
GeneralBrilliant Pin
CSnerd14-Oct-04 15:19
CSnerd14-Oct-04 15:19 
GeneralRe: Brilliant Pin
Magnus_15-Oct-04 6:06
Magnus_15-Oct-04 6:06 
GeneralLooks Familiar Pin
DFU2323-Sep-04 6:59
DFU2323-Sep-04 6:59 
GeneralRe: Looks Familiar Pin
Magnus_23-Sep-04 11:43
Magnus_23-Sep-04 11:43 
GeneralWell known problem Pin
Philippe Lhoste22-Sep-04 0:15
Philippe Lhoste22-Sep-04 0:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.