note 102496 added to function.imagecopymerge

From: Date: Thu, 17 Feb 2011 17:27:12 +0000
Subject: note 102496 added to function.imagecopymerge
Groups: php.notes 
Request: Send a blank email to php-notes+get-177084@lists.php.net to get a copy of this message
Anyone wanting to create a reflecion of an image. Simple process that copies, line by line, from the bottom of the image, a given number of pixels. Each line gets gradually more transparent. Outputs PNG to screen. This is pretty hot-coded - all four input variables (input image, reflection height, starting transparency, gap between image and reflection) are set manually here. <?php $in = imagecreatefromjpeg('C:\test.jpg'); $reflection_strength = 120; // starting transparency (0-127, 0 being opaque) $reflection_height = 40; // height of reflection in pixels $gap = 10; // gap between image and reflection $orig_height = imagesy($in); // store height of original image $orig_width = imagesx($in); // store height of original image $output_height = $orig_height + $reflection_height + $gap; // calculate height of output image // create new image to use for output. fill with transparency. ALPHA BLENDING MUST BE FALSE $out = imagecreatetruecolor($orig_width, $output_height); imagealphablending($out, false); $bg = imagecolortransparent($out, imagecolorallocatealpha($out, 255, 255, 255, 127)); imagefill($out, 0, 0, $bg); imagefilledrectangle($out, 0, 0, imagesx($in), imagesy($in), $bg1); // copy original image onto new one, leaving space underneath for reflection and 'gap' imagecopyresampled ( $out , $in , 0, 0, 0, 0, imagesx($in), imagesy($in), imagesx($in), imagesy($in)); // create new single-line image to act as buffer while applying transparency $reflection_section = imagecreatetruecolor(imagesx($in), 1); imagealphablending($reflection_section, false); $bg1 = imagecolortransparent($reflection_section, imagecolorallocatealpha($reflection_section, 255, 255, 255, 127)); imagefill($reflection_section, 0, 0, $bg1); // 1. copy each line individually, starting at the 'bottom' of the image, working upwards. // 2. set transparency to vary between reflection_strength and 127 // 3. copy line back to mirrored position in original for ($y = 0; $y<$reflection_height;$y++) { $t = ((127-$reflection_strength) + ($reflection_strength*($y/$reflection_height))); imagecopy($reflection_section, $out, 0, 0, 0, imagesy($in) - $y, imagesx($in), 1); imagefilter($reflection_section, IMG_FILTER_COLORIZE, 0, 0, 0, $t); imagecopyresized($out, $reflection_section, $a, imagesy($in) + $y + $gap, 0, 0, imagesx($in) - (2*$a), 1, imagesx($in), 1); } // output image to view header('Content-type: image/png'); imagesavealpha($out,true); imagepng($out); ?> ---- Server IP: 87.124.126.137 Probable Submitter: 212.219.92.36 (proxied: 212.219.92.36) ---- Manual Page -- http://www.php.net/manual/en/function.imagecopymerge.php Edit -- https://master.php.net/note/edit/102496 Del: integrated -- https://master.php.net/note/delete/102496/integrated Del: useless -- https://master.php.net/note/delete/102496/useless Del: bad code -- https://master.php.net/note/delete/102496/bad+code Del: spam -- https://master.php.net/note/delete/102496/spam Del: non-english -- https://master.php.net/note/delete/102496/non-english Del: in docs -- https://master.php.net/note/delete/102496/in+docs Del: other reasons-- https://master.php.net/note/delete/102496 Reject -- https://master.php.net/note/reject/102496 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#177084) next »