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