JBZoo\Image\Text::render PHP Метод

render() публичный статический Метод

Add text to an image
public static render ( mixed $image, string $text, string $fFile, array $params = [] )
$image mixed GD resource
$text string Some text to output on image as watermark
$fFile string TTF font file path
$params array Additional render params
    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));
            }
        }
    }

Usage Example

Пример #1
0
 /**
  * Add text to an image
  *
  * @param mixed  $image    GD resource
  * @param string $text     Some text to output on image as watermark
  * @param string $fontFile TTF font file path
  * @param array  $params
  * @throws \JBZoo\Image\Exception
  */
 public static function text($image, $text, $fontFile, $params = array())
 {
     Text::render($image, $text, $fontFile, $params);
 }