note 32393 added to function.imagecopymerge
| From: | bjorn at rack1 dot php dot net | Date: | Mon, 26 May 2003 16:10:53 +0000 |
| Subject: | note 32393 added to function.imagecopymerge | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-49042@lists.php.net to get a copy of this message | ||
in addition to stefan's posting:
i found that if you use imagecopymerge with png-24 files with an alpha channel, it doesn't work
use imagecopy instead.
it seems that imagecopymerge doesn't respect the alpha channel the way it should (a bug??).
some sample code here to place an image (image.png) on a backgroundcolor or backgroundimage.
<?php
$im = "image.png";
$bg = "ffddee"; // hex representation of the color (i.e. #ffffff for white)
$out = "png"; // or "jpg" for jpg file output
// $backgroundfile = ""; // optional backgroundfile if you don't want to use a color
//
// function to convert hex colorcode to decimal
//
function colordecode($hex){
$code[r] = hexdec(substr($hex, 0 ,2));
$code[g] = hexdec(substr($hex, 2 ,2));
$code[b] = hexdec(substr($hex, 4 ,2));
return $code;
} // end func colordecode
// create the resource id
$image_id = imageCreateFromPNG($im);
// get image size
$im_X = ImageSX($image_id);
$im_Y = ImageSY($image_id);
// create a truecolor background image of the right size
// or use a background image like this
// $backgroundimage = imageCreateFromPNG($backgroundfile);
$backgroundimage = imagecreatetruecolor($im_X, $im_Y);
// get the desired backgroundcolor:
// don't use this if you want to use a background image
$code = colordecode($bg);
$backgroundcolor = ImageColorAllocate($backgroundimage, $code[r], $code[g], $code[b]);
ImageFilledRectangle($backgroundimage, 0, 0, $im_X, $im_Y, $backgroundcolor);
// merge the two together with alphablending on!
ImageAlphaBlending($backgroundimage, true);
imagecopy($backgroundimage, $image_id, 0, 0, 0, 0, $im_X, $im_Y);
// output the image:
if($output == "jpg"){
Header( "Content-type: image/jpeg");
ImageJPEG($backgroundimage);
}
else{
Header( "Content-type: image/png");
ImagePNG($backgroundimage);
}
// destroy the memory
ImageDestroy($backgroundimage);
ImageDestroy($image_id);
?>
----
Manual Page -- http://www.php.net/manual/en/function.imagecopymerge.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32393
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32393&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32393&report=yes