Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Gd
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
10
100.00% covered (success)
100.00%
1 / 1
 setOpacity
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 text
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Image\Type;
16
17/**
18 * Type class for Gd
19 *
20 * @category   Pop
21 * @package    Pop\Image
22 * @author     Nick Sagona, III <dev@noladev.com>
23 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27class Gd extends AbstractType
28{
29
30    /**
31     * Opacity
32     * @var int|float|null
33     */
34    protected int|float|null $opacity = 0;
35
36    /**
37     * Set the opacity
38     *
39     * @param  int|float $opacity
40     * @return Gd
41     */
42    public function setOpacity(int|float $opacity): Gd
43    {
44        $this->opacity = (int)round((127 - (127 * ($opacity / 100))));
45        return $this;
46    }
47
48    /**
49     * Set and apply the text on the image
50     *
51     * @param  string $string
52     * @return Gd
53     */
54    public function text(string $string): Gd
55    {
56        if ($this->hasImage()) {
57            $fillColor = ($this->image->isIndexed()) ? $this->image->createColor($this->fillColor, null) :
58                $this->image->createColor($this->fillColor, $this->opacity);
59
60            if (($this->font !== null) && function_exists('imagettftext')) {
61                if ($this->strokeColor !== null) {
62                    $strokeColor = ($this->image->isIndexed()) ? $this->image->createColor($this->strokeColor, null) :
63                        $this->image->createColor($this->strokeColor, $this->opacity);
64                    imagettftext($this->image->getResource(), $this->size, $this->rotation, $this->x, ($this->y - 1), $strokeColor, $this->font, $string);
65                    imagettftext($this->image->getResource(), $this->size, $this->rotation, $this->x, ($this->y + 1), $strokeColor, $this->font, $string);
66                    imagettftext($this->image->getResource(), $this->size, $this->rotation, ($this->x - 1), $this->y, $strokeColor, $this->font, $string);
67                    imagettftext($this->image->getResource(), $this->size, $this->rotation, ($this->x + 1), $this->y, $strokeColor, $this->font, $string);
68                }
69                imagettftext($this->image->getResource(), $this->size, $this->rotation, $this->x, $this->y, $fillColor, $this->font, $string);
70            } else {
71                // Cap the system font size between 1 and 5
72                if ($this->size > 5) {
73                    $this->size = 5;
74                } else if ($this->size < 1) {
75                    $this->size = 1;
76                }
77                imagestring($this->image->getResource(), $this->size, $this->x, $this->y, $string, $fillColor);
78            }
79        }
80
81        return $this;
82    }
83
84}