note 102844 added to function.imagecopymerge

From: Date: Wed, 09 Mar 2011 20:56:28 +0000
Subject: note 102844 added to function.imagecopymerge
Groups: php.notes 
Request: Send a blank email to php-notes+get-177725@lists.php.net to get a copy of this message
This is what I use for merging two images while respecting the alpha channel (formulas are taken from the wikipedia article on alpha compositing; they're not too pretty as I didn't really try to make them look so, but instead just work and make sense when checked out months from now), with a quirk - an additional parameter (that defaults to NULL to be ignored) to manually specify a "transparent" color, which won't be copied over from the source to the destination. In comparison to the other implementation here, only one pass over $src_im is done, but more calculations are performed: <?php function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct, $trans = NULL) { $dst_w = imagesx($dst_im); $dst_h = imagesy($dst_im); // bounds checking $src_x = max($src_x, 0); $src_y = max($src_y, 0); $dst_x = max($dst_x, 0); $dst_y = max($dst_y, 0); if ($dst_x + $src_w > $dst_w) $src_w = $dst_w - $dst_x; if ($dst_y + $src_h > $dst_h) $src_h = $dst_h - $dst_y; for($x_offset = 0; $x_offset < $src_w; $x_offset++) for($y_offset = 0; $y_offset < $src_h; $y_offset++) { // get source & dest color $srccolor = imagecolorsforindex($src_im, imagecolorat($src_im, $src_x + $x_offset, $src_y + $y_offset)); $dstcolor = imagecolorsforindex($dst_im, imagecolorat($dst_im, $dst_x + $x_offset, $dst_y + $y_offset)); // apply transparency if (is_null($trans) || ($srccolor !== $trans)) { $src_a = $srccolor['alpha'] * $pct / 100; // blend $src_a = 127 - $src_a; $dst_a = 127 - $dstcolor['alpha']; $dst_r = ($srccolor['red'] * $src_a + $dstcolor['red'] * $dst_a * (127 - $src_a) / 127) / 127; $dst_g = ($srccolor['green'] * $src_a + $dstcolor['green'] * $dst_a * (127 - $src_a) / 127) / 127; $dst_b = ($srccolor['blue'] * $src_a + $dstcolor['blue'] * $dst_a * (127 - $src_a) / 127) / 127; $dst_a = 127 - ($src_a + $dst_a * (127 - $src_a) / 127); $color = imagecolorallocatealpha($dst_im, $dst_r, $dst_g, $dst_b, $dst_a); // paint if (!imagesetpixel($dst_im, $dst_x + $x_offset, $dst_y + $y_offset, $color)) return false; imagecolordeallocate($dst_im, $color); } } return true; } // use it like this (identical to imagecopymerge) function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) // or this function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct, array('red' => 1, 'green' => 2, 'blue' => 3, 'alpha' =>4)) ?> ---- Server IP: 69.147.83.197 Probable Submitter: 86.126.4.71 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopymerge.php Edit -- https://master.php.net/note/edit/102844 Del: integrated -- https://master.php.net/note/delete/102844/integrated Del: useless -- https://master.php.net/note/delete/102844/useless Del: bad code -- https://master.php.net/note/delete/102844/bad+code Del: spam -- https://master.php.net/note/delete/102844/spam Del: non-english -- https://master.php.net/note/delete/102844/non-english Del: in docs -- https://master.php.net/note/delete/102844/in+docs Del: other reasons-- https://master.php.net/note/delete/102844 Reject -- https://master.php.net/note/reject/102844 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177725) next »