Jump to content

GD png black background, turning it to white?


sammysmee

Recommended Posts

Hi there, i currently have a perfect gd code running on my stock art site, i have it so it renders the png with a watermark across it, however due to it being a png it puts a black background behind the thumbnail and the resized image, i would like to make this white, but i do not know GD (i was given the gd code) could any one help? I tried solving it but with no luck.

 

the code is below..

 

//$filename should be a JPG and $watermark a PNG-24 with alpha transparency. $quality is 1-100 JPG quality on output.

function watermark($srcfilename, $newname, $watermark, $quality) {

$imageInfo = getimagesize($srcfilename); 

$width = $imageInfo[0]; 

$height = $imageInfo[1]; 

$logoinfo = getimagesize($watermark);

$logowidth = $logoinfo[0];

$logoheight = $logoinfo[1];

$horizextra =$width - $logowidth;

$vertextra =$height - $logoheight;

$horizmargin =  round($horizextra / 2);

$vertmargin =  round($vertextra / 2);

$photoImage = ImageCreateFromPNG($srcfilename);

ImageAlphaBlending($photoImage, true);

$logoImage = ImageCreateFromPNG($watermark);

$logoW = ImageSX($logoImage);

$logoH = ImageSY($logoImage);

ImageCopy($photoImage, $logoImage, $horizmargin, $vertmargin, 0, 0, $logoW, $logoH);

//ImageJPEG($photoImage); // output to browser

ImageJPEG($photoImage,$newname, $quality); 

ImageDestroy($photoImage);

ImageDestroy($logoImage);

}



function createthumb($name,$filename,$new_w,$new_h)

{

$system=explode(".",$name);

if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}

if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}

    if (preg_match("/gif/",$system[1])){$src_img=imagecreatefromgif($name);}

    if (preg_match("/bmp/",$system[1])){$src_img=imagecreatefromwbmp($name);}

    	$old_x=imageSX($src_img);

$old_y=imageSY($src_img);

if ($old_x > $old_y) 

{

	$thumb_w=$new_w;

	//$thumb_h=$old_y*($new_h/$old_x);

        $thumb_h=$new_h;

}

if ($old_x < $old_y) 

{

	//$thumb_w=$old_x*($new_w/$old_y);

        $thumb_w=$new_w;

	$thumb_h=$new_h;

}

if ($old_x == $old_y) 

{

	$thumb_w=$new_w;

	$thumb_h=$new_h;

}

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

if (preg_match("/png/",$system[1]))

{

	imagepng($dst_img,$filename); 

} else if (preg_match("/jpg|jpeg/",$system[1])) {

	imagejpeg($dst_img,$filename); 

}

    else if (preg_match("/bmp/",$system[1]))

    {

        imagewbmp($dst_img,$filename);

    }

    else

    {

        imagegif($dst_img,$filename);     

    }

imagedestroy($dst_img); 

imagedestroy($src_img); 

}   

Link to comment
Share on other sites

  • 2 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.