Pop\Image\Gd::text PHP Method

text() public method

Create text within the an image object
public text ( string $str, integer | string $size, integer | string $x, integer | string $y, string $font = null, integer | string $rotate = null, boolean $stroke = false ) : Gd
$str string
$size integer | string
$x integer | string
$y integer | string
$font string
$rotate integer | string
$stroke boolean
return Gd
    public function text($str, $size, $x, $y, $font = null, $rotate = null, $stroke = false)
    {
        // Set the image resource and color.
        $this->createResource();
        $color = $this->setColor($this->fillColor);
        $fontSize = (int) $size;
        // Write the text to the image.
        if (null !== $font && function_exists('imagettftext')) {
            if ($stroke) {
                $stroke = $this->setColor($this->strokeColor);
                imagettftext($this->resource, $size, $rotate, $x, $y - 1, $stroke, $font, $str);
                imagettftext($this->resource, $size, $rotate, $x, $y + 1, $stroke, $font, $str);
                imagettftext($this->resource, $size, $rotate, $x - 1, $y, $stroke, $font, $str);
                imagettftext($this->resource, $size, $rotate, $x + 1, $y, $stroke, $font, $str);
            }
            imagettftext($this->resource, $fontSize, $rotate, $x, $y, $color, $font, $str);
        } else {
            // Cap the system font size between 1 and 5
            if ($fontSize > 5) {
                $fontSize = 5;
            } else {
                if ($fontSize < 1) {
                    $fontSize = 1;
                }
            }
            imagestring($this->resource, $fontSize, $x, $y, $str, $color);
        }
        $this->output = $this->resource;
        return $this;
    }

Usage Example

示例#1
0
 public function testSystemText()
 {
     $i = new Gd(__DIR__ . '/../tmp/test.jpg');
     $i->text('Hello World', 36, 10, 100);
     $this->assertEquals(640, $i->getWidth());
     $i = new Gd(__DIR__ . '/../tmp/test.jpg');
     $i->text('Hello World', 0, 10, 100);
     $this->assertEquals(640, $i->getWidth());
 }