.png if else statement.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bjeffries111
Forum Newbie
Posts: 11
Joined: Thu Oct 08, 2009 12:56 pm

.png if else statement.

Post by Bjeffries111 »

I want to create a .png if else statement because I have noticed some browsers do not support .png files and I have almost finishes a site using .png. I dont like the quality of the .gif for what I am using this for.

I know how to write an if else statement but i do not know how to tel php to figure out if the browser supports .png transparency.

I am also not an expert with PHP, I have almost finished my PHP & MySQL for Dummies book so i have a lot to learn.

Thanks for the help,
Brian
pa28
Forum Newbie
Posts: 7
Joined: Mon Oct 05, 2009 9:26 pm

Re: .png if else statement.

Post by pa28 »

There is no way in PHP to 'ask' a browser whether it supports PHP - McInfo's suggestion of looking at the user agent string and comparing it to your own database of supported browsers is the best you'll be able to do.

Having said that, the only modern browser I'm aware of that doesn't support transparent PNGs is MS Internet Explorer, versions prior to 7 IIRC. So:

Code: Select all

 
$pngSupport = true;
 
if(preg_match("/MSIE\s*(\d+)\./", $_SERVER['HTTP_USER_AGENT'], $matches)===1)
   if((int)$matches[1]<7)
      $pngSupport = false;
 
//If, by now, $pngSupport is still true, it's probably supported.
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: .png if else statement.

Post by onion2k »

Every browser supports PNG. http://www.twinhelix.com/css/iepngfix/
snail
Forum Newbie
Posts: 3
Joined: Fri Sep 25, 2009 10:49 pm

Re: .png if else statement.

Post by snail »

i think it cant
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: .png if else statement.

Post by onion2k »

snail wrote:i think it cant
Really? Why not?
User avatar
desperado
Forum Commoner
Posts: 46
Joined: Wed Dec 10, 2008 8:49 am

Re: .png if else statement.

Post by desperado »

This will enable transparency support:

Code: Select all

 
<script type="text/javascript">
if (typeof document.body.style.maxHeight != "undefined") {
  // IE 7, mozilla, safari, opera 9
} else {
  // IE6, older browsers
  
// alert( "In order to best experience this website, you should upgrade your copy of Internet Explorer to the latest version, or use Firefox or Safari." ); 
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      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 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}
window.attachEvent("onload", correctPNG);
}
</script>
 
Bjeffries111
Forum Newbie
Posts: 11
Joined: Thu Oct 08, 2009 12:56 pm

Re: .png if else statement.

Post by Bjeffries111 »

I will try the code snippets that were offered and see if it works. Thanks for all the responses! hopefully something works!
Thanks,
Brian
Bjeffries111
Forum Newbie
Posts: 11
Joined: Thu Oct 08, 2009 12:56 pm

Re: .png if else statement.

Post by Bjeffries111 »

I so far have tried all those code snippets and even tried that "iepngfix" css script but I still don't think it works. Although I am not directly seeing the results I am relying on another person to check for me from an older computer.

So i think I may just say screw it and replace the non important images with text and just leave the main images png for the people that can view png.

Brian
Post Reply