Forum Moderators: coopster
<?php
//create the image
$image = imagecreatefrompng('landscape.png');
//define the foreground
$fground = imagecolorallocatealpha($image, 0, 0, 0, 127);
//define the background
$bground = imagecolorallocate($image, 0, 0, 0);
//make the background transparent
imagecolortransparent($image, $bground);
//draw a triangle and fill with $image data
imagefilledpolygon($image, array(0,0, 120,0, 120,120), 3, $fground);
//save the image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
// Polygonal Masking using ImageMagick
$pContent = "Content-type: image/png"; // MIME-type for PNG files
$old_image = 'landscape.png'; // Original image file.
// Always output .png
$theContent = $pContent;
$theWMType = 'png:-';
// Get original image dimensions
$image = imagecreatefrompng($old_image);
$width = imagesx($image);
$height = imagesy($image);
// Create the mask image file - same size as original
// "black" in a mask is transparent, white is opaque
$wmCmd = "convert -size " . $width . "x" . $height . " xc:black";
// Set up drawing color to create mask
$wmCmd .= " -fill white -stroke white";
// -draw the desired mask
$wmCmd .= " -draw \"polygon 0,0, 120,0, 120,120\"";
// Output the result to file
$wmCmd .= " mask.png";
system($wmCmd);
// Composite the mask with the image
$wmCmd = "convert $old_image mask.png -alpha Off -compose CopyOpacity -composite";
// Crop to fit (optional for this example only!)
$wmCmd .= " -crop 120x120+0+0"; // (width)x(height)(x-offset)(y-offset)
$wmCmd .= " $theWMType";
header($theContent);
passthru($wmCmd); // use passthru() to output binary image data to browser
exit;
<?php
// Polygonal masking using GD
// imagealphamask() Courtesy of author at http://www.php.net/manual/en/function.imagesavealpha.php#94986
// Load the image
$image = imagecreatefrompng('landscape.png');
$width = imagesx($image); // get dimensions
$height = imagesy($image);
// Create mask
$mask = imagecreatetruecolor($width, $height);
// "black" in a mask is transparent, while white is opaque
$opaque = imagecolorallocate($mask, 255, 255, 255);
imagefilledpolygon($mask, array(0,0, 120,0, 120,120), 3, $opaque);
// Apply mask to source
imagealphamask( $image, $mask );
// Output image and free memory
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($mask);
exit;
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;
} // end imagealphamask()
?>