Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.59% covered (success)
92.59%
50 / 54
83.33% covered (success)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Gd
92.59% covered (success)
92.59%
50 / 54
83.33% covered (success)
83.33%
5 / 6
20.16
0.00% covered (danger)
0.00%
0 / 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
83.33% covered (success)
83.33%
20 / 24
0.00% covered (danger)
0.00%
0 / 1
6.17
 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-2023 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\Image\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-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    3.4.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                  $w
36     * @param  int                  $h
37     * @return Gd
38     */
39    public function border(Color\ColorInterface $color, $w, $h = null)
40    {
41        if ($this->hasImage()) {
42            $h = (null === $h) ? $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)
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)
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     * @return Gd
124     */
125    public function verticalGradient(Color\ColorInterface $color1, Color\ColorInterface $color2)
126    {
127        return $this->linearGradient($color1, $color2, true);
128    }
129
130    /**
131     * Flood the image with a vertical color gradient.
132     *
133     * @param  Color\ColorInterface $color1
134     * @param  Color\ColorInterface $color2
135     * @return Gd
136     */
137    public function horizontalGradient(Color\ColorInterface $color1, Color\ColorInterface $color2)
138    {
139        return $this->linearGradient($color1, $color2, false);
140    }
141
142    /**
143     * Flood the image with a color gradient.
144     *
145     * @param  Color\ColorInterface $color1
146     * @param  Color\ColorInterface $color2
147     * @param  boolean              $vertical
148     * @throws Exception
149     * @return Gd
150     */
151    public function linearGradient(Color\ColorInterface $color1, Color\ColorInterface $color2, $vertical = true)
152    {
153        if ($this->hasImage()) {
154            $tween = ($vertical) ? $this->image->getHeight() : $this->image->getWidth();
155            $blend = $this->getBlend($color1, $color2, $tween);
156
157            foreach ($blend['r'] as $i => $v) {
158                $r = $v;
159                $g = $blend['g'][$i];
160                $b = $blend['b'][$i];
161                $color = ($this->image->isIndexed()) ? $this->image->createColor(new Color\Rgb($r, $g, $b), null) :
162                    $this->image->createColor(new Color\Rgb($r, $g, $b), 0);
163                if ($vertical) {
164                    imageline($this->image->getResource(), 0, $i, $this->image->getWidth(), $i, $color);
165                } else {
166                    imageline($this->image->getResource(), $i, 0, $i, $this->image->getHeight(), $color);
167                }
168            }
169        }
170
171        return $this;
172    }
173
174}