/** * Add text to an image * * @param mixed $image GD resource * @param string $text Some text to output on image as watermark * @param string $fFile TTF font file path * @param array $params Additional render params * * @throws Exception */ public static function render($image, $text, $fFile, array $params = array()) { // Set vars $params = array_merge(self::$_default, $params); $angle = Helper::rotate($params['angle']); $position = Helper::position($params['position']); $fSize = $params['font-size']; $color = $params['color']; $offsetX = $params['offset-x']; $offsetY = $params['offset-y']; $strokeColor = $params['stroke-color']; $strokeSize = $params['stroke-size']; $strokeSpacing = $params['stroke-spacing']; $imageWidth = imagesx($image); $imageHeight = imagesy($image); $colorArr = self::_getColor($image, $color); list($textWidth, $textHeight) = self::_getTextboxSize($fSize, $angle, $fFile, $text); list($textX, $textY) = Helper::getInnerCoords($position, array($imageWidth, $imageHeight), array($textWidth, $textHeight), array($offsetX, $offsetY)); if ($strokeColor && $strokeSize) { if (is_array($color) || is_array($strokeColor)) { // Multi colored text and/or multi colored stroke $strokeColor = self::_getColor($image, $strokeColor); $chars = str_split($text, 1); foreach ($chars as $key => $char) { if ($key > 0) { $textX = self::_getStrokeX($fSize, $angle, $fFile, $chars, $key, $strokeSpacing, $textX); } // If the next letter is empty, we just move forward to the next letter if ($char === ' ') { continue; } self::_renderStroke($image, $char, array($fFile, $fSize, current($colorArr), $angle), array($textX, $textY), array($strokeSize, current($strokeColor))); // #000 is 0, black will reset the array so we write it this way if (next($colorArr) === false) { reset($colorArr); } // #000 is 0, black will reset the array so we write it this way if (next($strokeColor) === false) { reset($strokeColor); } } } else { $rgba = Helper::normalizeColor($strokeColor); $strokeColor = imagecolorallocatealpha($image, $rgba[0], $rgba[1], $rgba[2], $rgba[3]); self::_renderStroke($image, $text, array($fFile, $fSize, current($colorArr), $angle), array($textX, $textY), array($strokeSize, $strokeColor)); } } else { if (is_array($color)) { // Multi colored text $chars = str_split($text, 1); foreach ($chars as $key => $char) { if ($key > 0) { $textX = self::_getStrokeX($fSize, $angle, $fFile, $chars, $key, $strokeSpacing, $textX); } // If the next letter is empty, we just move forward to the next letter if ($char === ' ') { continue; } $fontInfo = array($fFile, $fSize, current($colorArr), $angle); self::_render($image, $char, $fontInfo, array($textX, $textY)); // #000 is 0, black will reset the array so we write it this way if (next($colorArr) === false) { reset($colorArr); } } } else { self::_render($image, $text, array($fFile, $fSize, $colorArr[0], $angle), array($textX, $textY)); } } }
/** * 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; }