Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
53 / 53
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Imagick
100.00% covered (success)
100.00%
53 / 53
100.00% covered (success)
100.00%
6 / 6
20
100.00% covered (success)
100.00%
1 / 1
 border
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 fill
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 radialGradient
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 verticalGradient
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 horizontalGradient
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
 linearGradient
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
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;
17use ImagickException;
18
19/**
20 * Effect class for Imagick
21 *
22 * @category   Pop
23 * @package    Pop\Image
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    4.0.0
28 */
29class Imagick 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 Imagick
39     */
40    public function border(Color\ColorInterface $color, int|float $w = 1, int|float|null $h = null): Imagick
41    {
42        if ($this->hasImage()) {
43            $h = ($h === null) ? $w : $h;
44            $this->image->getResource()->borderImage($this->image->createColor($color), $w, $h);
45        }
46        return $this;
47    }
48
49    /**
50     * Flood the image with a color fill.
51     *
52     * @param  Color\ColorInterface $color
53     * @return Imagick
54     */
55    public function fill(Color\ColorInterface $color): Imagick
56    {
57        if ($this->hasImage()) {
58            $draw = new \ImagickDraw();
59            $draw->setFillColor($this->image->createColor($color));
60            $draw->rectangle(0, 0, $this->image->getWidth(), $this->image->getHeight());
61            $this->image->getResource()->drawImage($draw);
62        }
63        return $this;
64    }
65
66    /**
67     * Flood the image with a vertical color gradient.
68     *
69     * @param  Color\ColorInterface $color1
70     * @param  Color\ColorInterface $color2
71     * @throws ImagickException
72     * @return Imagick
73     */
74    public function radialGradient(Color\ColorInterface $color1, Color\ColorInterface $color2): Imagick
75    {
76        if ($this->hasImage()) {
77            $im = new \Imagick();
78            $width = round($this->image->getWidth() * 1.25);
79            $height = round($this->image->getHeight() * 1.25);
80
81            if (!($color1 instanceof Color\Rgb)) {
82                $color1 = $color1->toRgb();
83            }
84            if (!($color2 instanceof Color\Rgb)) {
85                $color2 = $color2->toRgb();
86            }
87
88            $im->newPseudoImage($width, $height, 'radial-gradient:' . $color1->toHex() . '-' . $color2->toHex());
89            $this->image->getResource()->compositeImage(
90                $im, \Imagick::COMPOSITE_ATOP,
91                0 - round(($width - $this->image->getWidth()) / 2),
92                0 - round(($height - $this->image->getHeight()) / 2)
93            );
94        }
95
96        return $this;
97    }
98
99    /**
100     * Flood the image with a vertical color gradient.
101     *
102     * @param  Color\ColorInterface $color1
103     * @param  Color\ColorInterface $color2
104     * @throws ImagickException
105     * @return Imagick
106     */
107    public function verticalGradient(Color\ColorInterface $color1, Color\ColorInterface $color2): Imagick
108    {
109        if ($this->hasImage()) {
110            $im = new \Imagick();
111
112            if (!($color1 instanceof Color\Rgb)) {
113                $color1 = $color1->toRgb();
114            }
115            if (!($color2 instanceof Color\Rgb)) {
116                $color2 = $color2->toRgb();
117            }
118
119            $im->newPseudoImage(
120                $this->image->getWidth(), $this->image->getHeight(), 'gradient:' . $color1->toHex() . '-' . $color2->toHex()
121            );
122            $this->image->getResource()->compositeImage($im, \Imagick::COMPOSITE_ATOP, 0, 0);
123        }
124
125        return $this;
126    }
127
128    /**
129     * Flood the image with a vertical color gradient.
130     *
131     * @param  Color\ColorInterface $color1
132     * @param  Color\ColorInterface $color2
133     * @throws ImagickException
134     * @return Imagick
135     */
136    public function horizontalGradient(Color\ColorInterface $color1, Color\ColorInterface $color2): Imagick
137    {
138        if ($this->hasImage()) {
139            $im = new \Imagick();
140
141            if (!($color1 instanceof Color\Rgb)) {
142                $color1 = $color1->toRgb();
143            }
144            if (!($color2 instanceof Color\Rgb)) {
145                $color2 = $color2->toRgb();
146            }
147
148            $im->newPseudoImage(
149                $this->image->getHeight(), $this->image->getWidth(), 'gradient:' . $color1->toHex() . '-' . $color2->toHex()
150            );
151            $im->rotateImage('rgb(255, 255, 255)', -90);
152            $this->image->getResource()->compositeImage($im, \Imagick::COMPOSITE_ATOP, 0, 0);
153        }
154
155        return $this;
156    }
157
158    /**
159     * Flood the image with a color gradient.
160     *
161     * @param  Color\ColorInterface $color1
162     * @param  Color\ColorInterface $color2
163     * @param  bool                 $vertical
164     * @throws ImagickException
165     * @return Imagick
166     */
167    public function linearGradient(Color\ColorInterface $color1, Color\ColorInterface $color2, bool $vertical = true): Imagick
168    {
169        if ($this->hasImage()) {
170            if ($vertical) {
171                $this->verticalGradient($color1, $color2);
172            } else {
173                $this->horizontalGradient($color1, $color2);
174            }
175        }
176
177        return $this;
178    }
179
180}