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