/** * Changes the opacity level of the image * * @param resource $image Image GD resource * @param float|int $opacity 0-1 or 0-100 * * @return mixed */ public static function opacity($image, $opacity) { // Determine opacity $opacity = Helper::opacity($opacity); $width = imagesx($image); $height = imagesy($image); $newImage = imagecreatetruecolor($width, $height); // Set a White & Transparent Background Color $bg = imagecolorallocatealpha($newImage, 0, 0, 0, 127); imagefill($newImage, 0, 0, $bg); // Copy and merge Helper::imageCopyMergeAlpha($newImage, $image, array(0, 0), array(0, 0), array($width, $height), $opacity); imagedestroy($image); return $newImage; }
/** * Overlay an image on top of another, works with 24-bit PNG alpha-transparency * * @param string|Image $overlay An image filename or a Image object * @param string $position center|top|left|bottom|right|top left|top right|bottom left|bottom right * @param float|int $opacity Overlay opacity 0-1 or 0-100 * @param int $globOffsetX Horizontal offset in pixels * @param int $globOffsetY Vertical offset in pixels * * @return $this * @throws Exception */ public function overlay($overlay, $position = 'bottom right', $opacity = 0.4, $globOffsetX = 0, $globOffsetY = 0) { // Load overlay image if (!$overlay instanceof self) { $overlay = new self($overlay); } // Convert opacity $opacity = Helper::opacity($opacity); $globOffsetX = VarFilter::int($globOffsetX); $globOffsetY = VarFilter::int($globOffsetY); // Determine position list($xOffset, $yOffset) = Helper::getInnerCoords($position, array($this->_width, $this->_height), array($overlay->getWidth(), $overlay->getHeight()), array($globOffsetX, $globOffsetY)); // Perform the overlay Helper::imageCopyMergeAlpha($this->_image, $overlay->getImage(), array($xOffset, $yOffset), array(0, 0), array($overlay->getWidth(), $overlay->getHeight()), $opacity); return $this; }