Forked from santosahw/PHP GD Use one image to mask another image, including transparency
Created
June 5, 2014 09:57
-
-
Save cyberwani/a6b8bac0f7c81d21a477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Load source and mask | |
$source = imagecreatefrompng( '1.png' ); | |
$mask = imagecreatefrompng( '2.png' ); | |
// Apply mask to source | |
imagealphamask( $source, $mask ); | |
// Output | |
header( "Content-type: image/png"); | |
imagepng( $source ); | |
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 ) ); | |
if(($alpha['red'] == 0) && ($alpha['green'] == 0) && ($alpha['blue'] == 0) && ($alpha['alpha'] == 0)) | |
{ | |
// It's a black part of the mask | |
imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) ); // Stick a black, but totally transparent, pixel in. | |
} | |
else | |
{ | |
// Check the alpha state of the corresponding pixel of the image we're dealing with. | |
$alphaSource = imagecolorsforindex( $source, imagecolorat( $source, $x, $y ) ); | |
if(($alphaSource['alpha'] == 127)) | |
{ | |
imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) ); // Stick a black, but totally transparent, pixel in. | |
} | |
else | |
{ | |
$color = imagecolorsforindex( $source, imagecolorat( $source, $x, $y ) ); | |
imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $color['alpha'] ) ); // Stick the pixel from the source image in | |
} | |
} | |
} | |
} | |
// Copy back to original picture | |
imagedestroy( $picture ); | |
$picture = $newPicture; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment