note 32392 added to function.imagecopy

From: Date: Mon, 26 May 2003 16:07:52 +0000
Subject: note 32392 added to function.imagecopy
Groups: php.notes 
Request: Send a blank email to php-notes+get-49041@lists.php.net to get a copy of this message
in reply to mbostrom at paragee dot com It is a lot better to make your image transparent [using your favorite graphics editor ;)] This way you're able to get a much nicer look for your graphics if you use e.q. a drop shadow or glow etc. To use this you have to save your image as PNG-24 (24 bits), this includes an alpha-channel with all the transparency info. This way the two images are blended nicely and are also truecolor. here's some sample code: <?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.imagecopy.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+32392 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+32392&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+32392&report=yes

« previous php.notes (#49041) next »