SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inefficient use of a gd script?

    I have a site where users can change the background color of the page, however I have a lot of icons/buttons throughout the site with transparent backgrounds. In the interests of future-proofing the code I was thinking all the icons should be pngs rather than gifs. In a lot of cases I am using 8-bit pngs without antialiased backgrounds so they display correctly in IE6 without any adjustment, but in some places they look terrible so I'd rather use 32-bit pngs to get the antialiasing.

    So I wrote a script to detect whether or not the browser is IE6 or lower (don't tell me that's bad practice, I figure anyone who wants to make their browser pretend it's IE gets what's coming to them!) From there the script detects whether the png in question is indexed (8-bit) or true color (32-bit) and then decides how to display it. The plan is to filter every image on the site through the script so I can change the pngs at any time without having to worry about the consequences.

    My concern is that this is hugely inefficient in all cases where the browser is not IE or the png is only 8-bit. I don't know how much work it will create for the server or how it will affect the page loading time and just thought it'd be good to find out before I go ahead and set things in concrete. Can anyone confirm whether this is a good or bad idea?

    The images are all quite small (around 20 x 20px) and there might be anywhere from 20 to 500 of them on a page.

    Here's the code, anyway:

    PHP Code:
    $imagePath $_REQUEST['imagePath'];

    $src_img imageCreateFromPNG("$root/includes/images/$imagePath");

    if((
    $browser == 'MSIE') && ($version 7) && (imageIsTrueColor($src_img) == TRUE)) {

      
    $width imageSX($src_img);
      
    $height imageSY($src_img);
      
      
    // function to convert hexadecimal color values to rgb
      
    function fromhex($string) {
        global 
    $tmp_img;
        
    $string str_replace('#','',$string);
        
    sscanf($string"%2x%2x%2x"$red$green$blue);
        return 
    imageColorAllocate($tmp_img$red$green$blue);
      }
      
      
    // copy old image into new image
      
    $tmp_img imageCreateTrueColor($width$height);
      
    $background fromhex($backgroundColor);
      
    $transparent ImageColorTransparent($tmp_img$background);
      
    imageFill($tmp_img00$transparent);
      
      
    imageCopy($tmp_img$src_img0000$width$height);
      
    imageDestroy($src_img);
      
    $img $tmp_img;
      
      
    // convert to palette
      
    imageTrueColorToPalette($imgFALSE256);
      
      
    // output the image
      
    header("Content-Type: image/gif");
      
    imageGIF($img);
      
    imageDestroy($img);

    } else {
      
      
    imageAlphaBlending($src_imgfalse);
      
    imageSaveAlpha($src_imgtrue);
      
      
    // output the image
      
    header("Content-Type: image/png");
      
    imagePNG($src_img);
      
    imageDestroy($src_img);


  2. #2
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do these images need to be _created_ dynamically? if not, then create them once and save. just reference the correct file in the html. you are going to make your pages horribly slow if you plan to dynamically create 20-500 images per page load. not to mention, you have sent no http headers to even tell the browser its ok to cache these images. the browser is going to keep checking back with your script to see if the image has changed, and you have not implemented a way to repond to these requests. the result is going to probably be very slow for many users.

    as far as browser sniffing. are you sure you are doing it effectively? what about the many users who have no idea what an http user agent string is, yet thier browser by default pretends to be IE, or contains the MSIE substring in it? just, f' em eh?

  3. #3
    Kiwi Fr00t jylyn's Avatar
    Join Date
    Apr 2005
    Location
    New Zealand
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    do these images need to be _created_ dynamically? if not, then create them once and save. just reference the correct file in the html.
    Not necessarily, I suppose. It's just there will be lots of them if a copy has to be saved for each different colored background. That's kind of why I was asking, I wasn't sure of the best plan of attack, but if it's going to be as slow as you think then it's probably best to save them as they're created.

    Quote Originally Posted by clamcrusher
    you are going to make your pages horribly slow if you plan to dynamically create 20-500 images per page load. not to mention, you have sent no http headers to even tell the browser its ok to cache these images. the browser is going to keep checking back with your script to see if the image has changed, and you have not implemented a way to repond to these requests. the result is going to probably be very slow for many users.
    Oooh, thanks, I wasn't aware I could do that! Caching would be great since most of the images are duplicates. How do I go about writing the headers? If the image is cached, would I need to call a differet script for each image (eg <img src='script1.php' /><img src='script2.php' />) or would it be enough to pass different parameters (eg <img src='script.php?imageID=1' />)?

    Quote Originally Posted by clamcrusher
    as far as browser sniffing. are you sure you are doing it effectively? what about the many users who have no idea what an http user agent string is, yet thier browser by default pretends to be IE, or contains the MSIE substring in it? just, f' em eh?
    I was trying to avoid going down that path, but since you mention it... no I'm not sure how effective my methods are, but in this case it's not so much "f' em" as "serve 'em a gif instead of a png" which really isn't going to hamper their browsing experience in any way. If it was for something important I'd think about it a bit more...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •