note 83282 added to function.imagealphablending

From: Date: Mon, 19 May 2008 05:57:56 +0000
Subject: note 83282 added to function.imagealphablending
Groups: php.notes 
Request: Send a blank email to php-notes+get-139511@lists.php.net to get a copy of this message
I have written a function that takes an image as parameter and returns the same image with a reflection effect (often seen in WEB 2.0 sites). I have not performance-tested this with large image files, for thumbnails it works fine (requires PHP 4.3.2 or above, or PHP5). <?php function imagereflection($src_img) { $src_height = imagesy($src_img); $src_width = imagesx($src_img); $dest_height = $src_height + ($src_height / 2); $dest_width = $src_width; $reflected = imagecreatetruecolor($dest_width, $dest_height); imagealphablending($reflected, false); imagesavealpha($reflected, true); imagecopy($reflected, $src_img, 0, 0, 0, 0, $src_width, $src_height); $reflection_height = $src_height / 2; $alpha_step = 80 / $reflection_height; for ($y = 1; $y <= $reflection_height; $y++) { for ($x = 0; $x < $dest_width; $x++) { // copy pixel from x / $src_height - y to x / $src_height + y $rgba = imagecolorat($src_img, $x, $src_height - $y); $alpha = ($rgba & 0x7F000000) >> 24; $alpha = max($alpha, 47 + ($y * $alpha_step)); $rgba = imagecolorsforindex($src_img, $rgba); $rgba = imagecolorallocatealpha($reflected, $rgba['red'], $rgba['green'], $rgba['blue'], $alpha); imagesetpixel($reflected, $x, $src_height + $y - 1, $rgba); } } return $reflected; } ?> This is rather hot-coded. You could go on and extract some of the values as parameters (80 is the transparency start value, $height / 2 is the reflection area...). Cheers, Christian ---- Server IP: 217.13.201.10 Probable Submitter: 149.211.153.96 ---- Manual Page -- http://www.php.net/manual/en/function.imagealphablending.php Edit -- https://master.php.net/note/edit/83282 Del: integrated -- https://master.php.net/note/delete/83282/integrated Del: useless -- https://master.php.net/note/delete/83282/useless Del: bad code -- https://master.php.net/note/delete/83282/bad+code Del: spam -- https://master.php.net/note/delete/83282/spam Del: non-english -- https://master.php.net/note/delete/83282/non-english Del: in docs -- https://master.php.net/note/delete/83282/in+docs Del: other reasons-- https://master.php.net/note/delete/83282 Reject -- https://master.php.net/note/reject/83282 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139511) next »