You are here

function image_gd_imagemask in ImageCache Actions 7

Same name and namespace in other branches
  1. 8 canvasactions/canvasactions.inc \image_gd_imagemask()

GD toolkit specific implementation of the image mask effect.

Parameters

stdClass $image: Image object containing the GD image resource to operate on.

stdClass $mask: An image object containing the image to use as mask.

Return value

bool true on success, false otherwise.

File

canvasactions/canvasactions.inc, line 103

Code

function image_gd_imagemask(stdClass $image, stdClass $mask) {
  $newPicture = imagecreatetruecolor($image->info['width'], $image->info['height']);
  imagesavealpha($newPicture, TRUE);
  imagealphablending($newPicture, TRUE);
  $transparent = imagecolorallocatealpha($newPicture, 0, 0, 0, 127);
  imagefill($newPicture, 0, 0, $transparent);

  // Perform pixel-based alpha map application.
  for ($x = 0; $x < $image->info['width']; $x++) {
    for ($y = 0; $y < $image->info['height']; $y++) {

      // Deal with images with mismatched sizes
      if ($x >= $mask->info['width'] || $y >= $mask->info['height']) {
        imagesetpixel($newPicture, $x, $y, $transparent);
      }
      else {
        $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;
  return TRUE;
}