PHP Gd Use One Image to Mask Another Image, Including Transparency

PHP GD Use one image to mask another image, including transparency

Matt,

If you make your png with the oval white fill on black background instead of black fill with transparent background the following function does it.

<?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 ) );
$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;
}

?>

image GD and png masking

One way to do it is using phpThumb.

Basic reference here: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x31

If creating the new image on the fly it would be something as simple as:

<img src="../phpThumb.php?src=path/to/image/image.jp&fltr[]=mask|path/to/mask/mask.png&f=png" alt="Sample Image">

To output into a png.

If doing this after an image upload to create a new image to be stored on the server, first figure out the basics of phpThumb and then set the mask parameters with all the rest:

For example:

...
require_once('phpThumb/phpthumb.class.php');

//Begin phpThumb work to resize image and create thumbnail
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $destination;
$uploadfile = $uploaddir . $file;

$phpThumb = new phpThumb();

// set data source -- do this first, any settings must be made AFTER this call
$phpThumb->setSourceFilename($uploadfile);

$phpThumb->setParameter('w', 360); //change to update the picture size
$phpThumb->setParameter('h', 470); //change to update the picture size

$phpThumb->setParameter('fltr[]', 'mask|path/to/mask/mask.png'); //set mask
$phpThumb->setParameter('f', 'png'); //set png output format

$outputdir = $_SERVER['DOCUMENT_ROOT'] . $destination;

$output_filename = $outputdir . "masked" . $file;

$phpThumb->setParameter('config_allow_src_above_docroot', true);

if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it!

if ($phpThumb->RenderToFile($output_filename)) {

...

Transparent hexagon-masked collage using PHP GD

You need

imagesavealpha( $dest, true );

And if you plan to overlap the transparent bits of hexagon,

imagealphablending( $dest, true );


Related Topics



Leave a reply



Submit