note 94986 added to function.imagesavealpha

From: Date: Mon, 07 Dec 2009 02:07:53 +0000
Subject: note 94986 added to function.imagesavealpha
Groups: php.notes 
Request: Send a blank email to php-notes+get-163631@lists.php.net to get a copy of this message
As I needed a function that enables me to put a separate mask into the alpha channel of a picture, I found there is no function to allow this directly via PHP/GD, hence I wrote a quick'n'dirty function that does the trick. For the mask any format is fine (jpg, png, gif, ...). Black will be transparent, white will be opaque - greyscale makes it half transparent, like with any alpha channel. The alpha channel is only 7 bit, hence there are just 128 variations of transparency possible. I use the red channel, for the transparency, so you can make it either greyscale or redscale, doesn't matter. <?php function imagealphamask( &$picture, $mask ) { // Get sizes and set up new picture $xSize = imagesx( $picture ); $ySize = imagesy( $picture ); $newPicture = imagecreatetruecolor( $xSize, $ySize ); imagesavealpha( $newPicture, true ); imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) ); // Resize mask if necessary if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) { $tempPic = imagecreatetruecolor( $xSize, $ySize ); imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) ); imagedestroy( $mask ); $mask = $tempPic; } // Perform pixel-based alpha map application for( $x = 0; $x < $xSize; $x++ ) { for( $y = 0; $y < $ySize; $y++ ) { $alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) ); $alpha = 127 - floor( $alpha[ 'red' ] / 2 ); $color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) ); imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) ); } } // Copy back to original picture imagedestroy( $picture ); $picture = $newPicture; } ?> To use the function is rather simple: <?php // Load source and mask $source = imagecreatefrompng( 'myPicture.png' ); $mask = imagecreatefrompng( 'myMask.png' ); // Apply mask to source imagealphamask( $source, $mask ); // Output header( "Content-type: image/png"); imagepng( $source ); ?> Hope it helps! ---- Server IP: 69.147.83.197 Probable Submitter: 82.82.116.60 ---- Manual Page -- http://www.php.net/manual/en/function.imagesavealpha.php Edit -- https://master.php.net/note/edit/94986 Del: integrated -- https://master.php.net/note/delete/94986/integrated Del: useless -- https://master.php.net/note/delete/94986/useless Del: bad code -- https://master.php.net/note/delete/94986/bad+code Del: spam -- https://master.php.net/note/delete/94986/spam Del: non-english -- https://master.php.net/note/delete/94986/non-english Del: in docs -- https://master.php.net/note/delete/94986/in+docs Del: other reasons-- https://master.php.net/note/delete/94986 Reject -- https://master.php.net/note/reject/94986 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163631) next »