You are here

function imageapi_gd_image_imagemask in ImageCache Actions 6.2

Convert the image to PNG and apply an image-based transparency mask using GD

&$image is an array expected to contain the details of the image to be masked $mask is an array expected to contain the details of the mask image

File

canvasactions/canvasactions.inc, line 98

Code

function imageapi_gd_image_imagemask(&$image, $mask = array()) {
  $newPicture = imagecreatetruecolor($image->info['width'], $image->info['height']);
  imagesavealpha($newPicture, true);
  imagealphablending($newPicture, true);
  imagefill($newPicture, 0, 0, imagecolorallocatealpha($newPicture, 0, 0, 0, 127));

  // Perform pixel-based alpha map application
  for ($x = 0; $x < $image->info['width']; $x++) {
    for ($y = 0; $y < $image->info['height']; $y++) {
      $alpha = imagecolorsforindex($mask->resource, imagecolorat($mask->resource, $x, $y));
      $alpha = 127 - floor($alpha['red'] / 2);
      $color = imagecolorsforindex($image->resource, imagecolorat($image->resource, $x, $y));
      imagesetpixel($newPicture, $x, $y, imagecolorallocatealpha($newPicture, $color['red'], $color['green'], $color['blue'], $alpha));
    }
  }

  // Copy back to original picture
  imagedestroy($image->resource);
  $image->resource = $newPicture;

  // ensure the final image is a PNG due to transparency:
  $image->info['extension'] = 'png';
  $image->info['mime_type'] = 'image/png';
  return TRUE;
}