function canvasactions_mask in ImageCache Actions 5.2
Same name and namespace in other branches
- 5.3 canvasactions.inc \canvasactions_mask()
Given a mask image resource object in the $action, use the alpha values from it to set transparency on the source image.
Note that the returned image has transparency set BUT if it's a jpeg it may not remember that channel. Need to switch formats or flatten before saving, or the transparency will be lost.
1 call to canvasactions_mask()
- canvasactions_roundedcorners_image in ./
canvasactions.inc - Create a rounded corner mask and alpha-merge it with the image.
File
- ./
canvasactions.inc, line 605
Code
function canvasactions_mask(&$image, $mask) {
// I do not believe there is a func for this, so I'll do it pixel-by-pixel
// Slow, I know.
$info = $image->info;
if (!$info) {
return FALSE;
}
$img =& $image->resource;
$msk =& $mask->resource;
imagesavealpha($image->resource, TRUE);
imagealphablending($image->resource, FALSE);
// Support indexed color (gif) if I have to
$transparent_ix = imagecolortransparent($image->resource);
$width = imagesx($img);
// Use the actual, not claimed image size.
$height = imagesy($img);
for ($i = 0; $i < $height; $i++) {
//this loop traverses each row in the image
for ($j = 0; $j < $width; $j++) {
//this loop traverses each pixel of each row
// Get the color & alpha info of the current pixel
$color_ix = imagecolorat($img, $j, $i);
// an index
$rgba_array = imagecolorsforindex($img, $color_ix);
// support indexed trans
if ($color_ix == $transparent_ix) {
$rgba_array['alpha'] = 127;
}
// Get the alpha of the corresponding mask pixel
$mask_color_ix = imagecolorat($msk, $j, $i);
// an index
$msk_rgba_array = imagecolorsforindex($msk, $mask_color_ix);
// Calculate the total alpha value of this pixel
$rgba_array['alpha'] = max($msk_rgba_array['alpha'], $rgba_array['alpha']);
//paint the pixel
if ($image->info['mime_type'] == 'image/gif') {
// indexed color - re-use existing pallette
$color_to_paint = imagecolorclosestalpha($image->resource, $rgba_array['red'], $rgba_array['green'], $rgba_array['blue'], $rgba_array['alpha']);
}
else {
$color_to_paint = imagecolorallocatealpha($image->resource, $rgba_array['red'], $rgba_array['green'], $rgba_array['blue'], $rgba_array['alpha']);
}
imagesetpixel($image->resource, $j, $i, $color_to_paint);
}
}
return TRUE;
}