SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast numeropi's Avatar
    Join Date
    Apr 2001
    Location
    somewhere between 3.14 and 3.15
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Transparent image processing

    I'm trying to dynamically create an image composed of a background and a round button. I have both images as PNG, and the button is a transparent PNG (checked with Netscape, the background of the page shows through, IE doesn't support transparent PNGs properly)

    However, I can't get it to work, I must be missing some function call to treat the button as transparent. Here is what I'm doing at the moment:

    Code:
    <?php
    
    $fondo = ImageCreateFromPNG('fondo.png');
    $boton = ImageCreateFromPNG('boton.png');
    
    $nuevaimagen = ImageCreate(350,350);
    
    $transparente = ImageColorTransparent($boton);
    
    ImageCopy($nuevaimagen, $fondo, 0,0,313,108,350,350);
    ImageCopy($nuevaimagen,$boton,50,50,0,0,32,32);
    
    
    Header( "Content-type: image/png"); 
    ImagePNG($nuevaimagen);
    
    
    ?>
    fondo.png is the background and boton.png is the button. Right now, the button is copied over the background, but inside a horrible white square (not transparently).

    Has anybody dealt with something like this before? After reading the manual, I think I need the ImageColorTransparent function, but the manual is not very clear about how to use it.

    Any ideas? Thanks

  2. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, remove your current line with ImageColorTransparent on it and place this code before the header:

    $white = ImageColorAllocate($nuevaimagen, 255, 255, 255);
    $trans = ImageColorTransparent($nuevaimagen, $white);

    It should hopefully work.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  3. #3
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    another way is if you use .gifs instead of .pngs and make the original gifs have transparent backgrounds in photoshop you can copy them like you were doing with their transparence intact.

    note however: as soon as you need to draw or write on the image you are creating, the first colour you set to use will overwrite the transparence.

    so using ImageColorTransparent is more reliable yet not sure if it can do good blending transparent effects like in photoshop, have'nt tested that, maybe somebody can respond to that?

    a+
    Garrett

  4. #4
    SitePoint Enthusiast numeropi's Avatar
    Join Date
    Apr 2001
    Location
    somewhere between 3.14 and 3.15
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no luck, white background still shows. I appreciate your time, nevertheless

    A blending effect would be really good, the PNG format supports it, but I still can't get a single color to be transparent, let alone alpha blending GIF is not an option because of Unisys' patent. GIF support was dropped from GD because of this.

  5. #5
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i assure you it does work with a gif!!! i have done a work with it. true the gif format has been droped but it still seems to be the most widely supported with servers and cause no problems display side mac/pc.

    have a look here...
    http://www.zendco.com/site/experimen...ers/city3.html

    the city is made from 3 buildings with transparent backgrounds randomly placed each time the page is loaded, when the buildings overlap it looks as if its a crowded city.

    the code is ...

    <?

    //header makes sure it refreshes
    header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); //Date in the past
    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header ("Cache-Control: no-cache, must-revalidate"); //HTTP/1.1
    header ("Pragma: no-cache");

    $image = imagecreate(500, 300);

    //colors
    $white = ImageColorAllocate($image,255,255,255);
    $grey = ImageColorAllocate($image,200,200,200);



    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //place a scraper
    $scraper = ImageCreateFromgif("scraper1.gif"); //the image pulled in from a gif
    $number_one_x = (rand() % 500) - 70; $number_one_y = (rand() % 139) + 161;
    ImageCopy ($image, $scraper, $number_one_x, $number_one_y, 0, 0, 70, 139);

    //visualise image
    header("content-type: image/gif");
    imagegif($image);
    imagedestroy($image);

    ?>

    if you copy this all you need to do is place a gif called "scraper1.gif" which has a transparent background and size of 161*70 in the same folder as the php file and it will work.

    good luck!

    Garrett

  6. #6
    SitePoint Enthusiast numeropi's Avatar
    Join Date
    Apr 2001
    Location
    somewhere between 3.14 and 3.15
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The result looks cool!

    It's a pity it doesn't work just as well with PNGs... I could find an older version of PHP with the GD that supported GIF, but someday the host will upgrade and my script will stop working, I see this as too much dependency, and what is worse, the dependency is on using outdated software.

    I'll have to keep working on it until I get it right with PNGs

  7. #7
    SitePoint Addict Garrett's Avatar
    Join Date
    Feb 2001
    Location
    England
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure why it does'nt work with pngs (pngs have transparency possibilities right?) it should be exactly the same just at the end imagecreatepng.

    anyway i cant help anymore cos i'm new to all this anyway and thats as far as my knowledge goes! sorry

    good luck let me know if you sort it out!! as thats interesting, few people seem to be working with images in php!

    a+
    Garrett


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
  •