How to set background color with GD

Discussion in 'PHP' started by bobby9101, Oct 20, 2007.

  1. #1
    Hi, I am creating thumbnails with PHP, the problem is when I upload a transparent image, it makes a really bad thumbnail.
    I would like to set a solid color as the background, how do I do this?
    My thumbnail code is:
    function thumbnail($imagename) {
    list($width, $height, $type) = getimagesize("photos/$imagename");
    if ($type == 1) $img = imagecreatefromgif("photos/$imagename");
    else if ($type == 2) $img = imagecreatefromjpeg("photos/$imagename");
    else if ($type == 3) $img = imagecreatefrompng("photos/$imagename");
    if ($width > $height && $width >= 150) {
    $diff = $width/150;
    $width = 150;
    $height = $height/$diff;
    }
    else if ($width < $height && $height >= 150) {
    $diff = $height/150;
    $height = 150;
    $width = $width/$diff;
    }
    $dstimg = imagecreate($width,$height);
    imagecopyresized($dstimg,$img, 0,0,0,0,$width,$height,imagesx($img),imagesy($img));
    if ($type == 1) $img = imagegif($dstimg, "thumbnails/$imagename");
    else if ($type == 2) $img = imagejpeg($dstimg, "thumbnails/$imagename");
    else if ($type == 3) $img = imagepng($dstimg, "thumbnails/$imagename");
    }
    PHP:

     
    bobby9101, Oct 20, 2007 IP
  2. Koster

    Koster Guest

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    To fill area, use imagefilledrectangle($dstimg,0,0,width,height,$bgcolor);
     
    Koster, Oct 21, 2007 IP