/** * (non-PHPdoc) * @see Imagine\ImageInterface::applyMask() */ public function applyMask(ImageInterface $mask) { if (!$mask instanceof self) { throw new InvalidArgumentException('Cannot mask non-gd images'); } $size = $this->getSize(); $maskSize = $mask->getSize(); if ($size != $maskSize) { throw new InvalidArgumentException(sprintf('The given mask doesn\'t match current image\'s sise, Current ' . 'mask\'s dimensions are %s, while image\'s dimensions are %s', $maskSize, $size)); } for ($x = 0; $x < $size->getWidth(); $x++) { for ($y = 0; $y < $size->getHeight(); $y++) { $color = imagecolorat($this->resource, $x, $y); $info = imagecolorsforindex($this->resource, $color); $maskColor = $color = imagecolorat($mask->resource, $x, $y); $maskInfo = imagecolorsforindex($mask->resource, $maskColor); if (false === imagesetpixel($this->resource, $x, $y, imagecolorallocatealpha($this->resource, $info['red'], $info['green'], $info['blue'], round((127 - $info['alpha']) * $maskInfo['red'] / 255)))) { throw new RuntimeException('Apply mask operation failed'); } } } return $this; }
/** * (non-PHPdoc) * @see Imagine\ImageInterface::applyMask() */ public function applyMask(ImageInterface $mask) { if (!$mask instanceof self) { throw new InvalidArgumentException('Can only apply instances of Imagine\\Gmagick\\Image as masks'); } $size = $this->getSize(); $maskSize = $mask->getSize(); if ($size != $maskSize) { throw new InvalidArgumentException(sprintf('The given mask doesn\'t match current image\'s sise, current ' . 'mask\'s dimensions are %s, while image\'s dimensions are %s', $maskSize, $size)); } try { $mask = $mask->copy(); $this->gmagick->compositeimage($mask->gmagick, \Gmagick::COMPOSITE_DEFAULT, 0, 0); } catch (\Exception $e) { throw new RuntimeException('Apply mask operation failed', $e->getCode(), $e); } return $this; }
/** * @param Imagine\ImageInterface $image * * @return array */ private function normalize(ImageInterface $image) { $step = (int) round(255 / $this->buckets); $red = $green = $blue = $alpha = array(); for ($i = 1; $i <= $this->buckets; $i++) { $range = new Range(($i - 1) * $step, $i * $step); $red[] = new Bucket($range); $green[] = new Bucket($range); $blue[] = new Bucket($range); $alpha[] = new Bucket($range); } foreach ($image->histogram() as $color) { foreach ($red as $bucket) { $bucket->add($color->getRed()); } foreach ($green as $bucket) { $bucket->add($color->getGreen()); } foreach ($blue as $bucket) { $bucket->add($color->getBlue()); } foreach ($alpha as $bucket) { $bucket->add($color->getAlpha()); } } $total = $image->getSize()->square(); $callback = function (Bucket $bucket) use($total) { return count($bucket) / $total; }; return array(array_map($callback, $red), array_map($callback, $green), array_map($callback, $blue), array_map($callback, $alpha)); }