Re: [PHP] GD - JPEG to PNG with transparency and color

[email protected] (tedd)
Newsgroups php.general
Message-ID <p06240804c4475abddc09@[192.168.1.101]>
At 12:16 PM -0400 5/6/08, Travis L. Font wrote:
>Following files:
>bg.png - Clear Transparent Image
>14416fed5d4f78.jpg - Normal Jpeg Image
>
>
>The Code:
>
>header('content-type: image/png'); 
>
>$watermark = imagecreatefromjpeg('14416fed5d4f78.jpg');
>
>$watermark_width = imagesx($watermark); 
>$watermark_height = imagesy($watermark); 
>
>$image = imagecreatetruecolor($watermark_width, $watermark_height); 
>$image = imagecreatefrompng('bg.png'); 
>
>$size = getimagesize('bg.png'); 
>
>$dest_x = $size[0] - $watermark_width - 5; 
>$dest_y = $size[1] - $watermark_height - 5; 
>
>imagecopymerge($image, $watermark, $dest_x, 
>$dest_y, 0, 0, $watermark_width, 
>$watermark_height, 100);
>
>imagepng($image);
>
>imagedestroy($image); 
>imagedestroy($watermark);
>
>
>The Problem:
>
>The code above works fine in sense of syntax! 
>bg.png acts as a transparent border for 
>14416fed5d4f78.jpg to keep the size of bg.png 
>and not the size of 14416fed5d4f78.jpg so it 
>doesn't blow up 14416fed5d4f78.jpg. However, the 
>image comes out black/white and has lost its 
>color!
>I looked all over Google and I've found nothing 
>so far that's functional to give the new created 
>png file the correct colors as the original 
>14416fed5d4f78.jpg. I figure that I'm missing 
>some small elements to the process of creating 
>the pngŠ
>Anyone have any ideas, pointers, advice, or 
>correct solution to make this possible?

Travis:

Try this:

$original=imagecreatefromjpeg("mydog.jpg");
$watermark=imagecreatefrompng("copyright.png");

$osx=imagesx($original);
$osy=imagesy($original);
$wsx=imagesx($watermark);
$wsy=imagesy($watermark);

imagecopy($original, $watermark, ($osx-$wsx)/2, 
($osy-$wsy)/2, 0, 0, $wsx, $wsy); //center

imagepng($original, "trans.png");

The code works here:

http://webbytedd.com/b/watermark/

Cheers,

tedd

-- 
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.