Created
December 12, 2012 20:38
-
-
Save DfKimera/4271405 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 | |
//error_reporting(E_ERROR); | |
$original = imagecreatefrompng("assets/picture.png"); | |
$mask = imagecreatefrompng("assets/mask.png"); | |
$masked = applyAlphaMask( $original, $mask ); | |
function applyAlphaMask($image, $mask) { | |
$pictureWidth = imagesx($image); | |
$pictureHeight = imagesy($image); | |
$canvas = imagecreatetruecolor($pictureWidth, $pictureHeight); | |
imagesavealpha($canvas, true); | |
imagefill($canvas, 0, 0, imagecolorallocatealpha($canvas, 0, 0, 0, 127)); | |
for($x = 0; $x < $pictureWidth; $x++) { | |
for($y = 0; $y < $pictureHeight; $y++) { | |
$alphaAt = imagecolorsforindex($mask, imagecolorat( $mask, $x, $y )); | |
if(($alphaAt['red'] == 0) && ($alphaAt['green'] == 0) && ($alphaAt['blue'] == 0) && ($alphaAt['alpha'] == 0)) { | |
imagesetpixel($canvas, $x, $y, imagecolorallocatealpha($canvas, 0, 0, 0, 127)); | |
} else { | |
$colorAt = imagecolorsforindex($image, imagecolorat($image, $x, $y)); | |
imagesetpixel($canvas, $x, $y, imagecolorallocatealpha($canvas, $colorAt['red'], $colorAt['green'], $colorAt['blue'], $colorAt['alpha'])); | |
} | |
} | |
} | |
return $canvas; | |
} | |
header("Content-type: image/png"); | |
imagepng($masked); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment