Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Gd
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
6 / 6
20
100.00% covered (success)
100.00%
1 / 1
 border
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 fill
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 radialGradient
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
6
 verticalGradient
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 horizontalGradient
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 linearGradient
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
6
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\Effect;
16
17use Pop\Color\Color;
18
19/**
20 * Effect class for Gd
21 *
22 * @category   Pop
23 * @package    Pop\Image
24 * @author     Nick Sagona, III <dev@noladev.com>
25 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    5.0.0
28 */
29class Gd extends AbstractEffect
30{
31
32    /**
33     * Draw a border around the image.
34     *
35     * @param  Color\ColorInterface $color
36     * @param  int|float            $w
37     * @param  int|float|null       $h
38     * @return Gd
39     */
40    public function border(Color\ColorInterface $color, int|float $w, int|float|null $h = null): Gd
41    {
42        if ($this->hasImage()) {
43            $h = ($h === null) ? $w : $h;
44            $width = $this->image->getWidth();
45            $height = $this->image->getHeight();
46
47            $this->image->draw()->setFillColor($color);
48            $this->image->draw()->rectangle(0, 0, $width, $h);
49            $this->image->draw()->rectangle(0, ($height - $h), $width, $height);
50            $this->image->draw()->rectangle(0, 0, $w, $height);
51            $this->image->draw()->rectangle(($width - $w), 0, $width, $height);
52        }
53
54        return $this;
55    }
56
57    /**
58     * Flood the image with a color fill.
59     *
60     * @param  Color\ColorInterface $color
61     * @return Gd
62     */
63    public function fill(Color\ColorInterface $color): Gd
64    {
65        if ($this->hasImage()) {
66            if ($this->image->isIndexed()) {
67                imagefill($this->image->getResource(), 0, 0, $this->image->createColor($color));
68            } else {
69                imagefill($this->image->getResource(), 0, 0, $this->image->createColor($color, 0));
70            }
71        }
72        return $this;
73    }
74
75    /**
76     * Flood the image with a vertical color gradient.
77     *
78     * @param  Color\ColorInterface $color1
79     * @param  Color\ColorInterface $color2
80     * @return Gd
81     */
82    public function radialGradient(Color\ColorInterface $color1, Color\ColorInterface $color2): Gd
83    {
84        if ($this->hasImage()) {
85            if ($this->image->getHeight() > $this->image->getWidth()) {
86                $tween = $this->image->getHeight();
87                $tween = (int)round($tween * ($this->image->getHeight() / $this->image->getWidth()));
88            } else if ($this->image->getWidth() > $this->image->getHeight()) {
89                $tween = $this->image->getWidth();
90                $tween = (int)round($tween * ($this->image->getWidth() / $this->image->getHeight()));
91            } else {
92                $tween = $this->image->getWidth();
93                $tween = (int)round($tween * 1.5);
94            }
95
96            $blend = $this->getBlend($color1, $color2, $tween);
97
98            $x = (int)round($this->image->getWidth() / 2);
99            $y = (int)round($this->image->getHeight() / 2);
100            $w = $tween;
101            $h = $tween;
102
103            foreach ($blend['r'] as $i => $v) {
104                $r = $v;
105                $g = $blend['g'][$i];
106                $b = $blend['b'][$i];
107                $color = ($this->image->isIndexed()) ? $this->image->createColor(new Color\Rgb($r, $g, $b), null) :
108                    $this->image->createColor(new Color\Rgb($r, $g, $b), 0);
109
110                imagefilledellipse($this->image->getResource(), $x, $y, $w, $h, $color);
111                $w--;
112                $h--;
113            }
114        }
115
116        return $this;
117    }
118
119    /**
120     * Flood the image with a vertical color gradient.
121     *
122     * @param Color\ColorInterface $color1
123     * @param Color\ColorInterface $color2
124     * @throws Exception
125     * @return Gd
126     */
127    public function verticalGradient(Color\ColorInterface $color1, Color\ColorInterface $color2): Gd
128    {
129        return $this->linearGradient($color1, $color2, true);
130    }
131
132    /**
133     * Flood the image with a vertical color gradient.
134     *
135     * @param  Color\ColorInterface $color1
136     * @param  Color\ColorInterface $color2
137     * @return Gd
138     */
139    public function horizontalGradient(Color\ColorInterface $color1, Color\ColorInterface $color2): Gd
140    {
141        return $this->linearGradient($color1, $color2, false);
142    }
143
144    /**
145     * Flood the image with a color gradient.
146     *
147     * @param  Color\ColorInterface $color1
148     * @param  Color\ColorInterface $color2
149     * @param  bool                 $vertical
150     * @throws Exception
151     * @return Gd
152     */
153    public function linearGradient(Color\ColorInterface $color1, Color\ColorInterface $color2, bool $vertical = true): Gd
154    {
155        if ($this->hasImage()) {
156            $tween = ($vertical) ? $this->image->getHeight() : $this->image->getWidth();
157            $blend = $this->getBlend($color1, $color2, $tween);
158
159            foreach ($blend['r'] as $i => $v) {
160                $r = $v;
161                $g = $blend['g'][$i];
162                $b = $blend['b'][$i];
163                $color = ($this->image->isIndexed()) ? $this->image->createColor(new Color\Rgb($r, $g, $b), null) :
164                    $this->image->createColor(new Color\Rgb($r, $g, $b), 0);
165                if ($vertical) {
166                    imageline($this->image->getResource(), 0, $i, $this->image->getWidth(), $i, $color);
167                } else {
168                    imageline($this->image->getResource(), $i, 0, $i, $this->image->getHeight(), $color);
169                }
170            }
171        }
172
173        return $this;
174    }
175
176}