Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Imagick
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
7 / 7
20
100.00% covered (success)
100.00%
1 / 1
 hue
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 saturation
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 brightness
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 hsb
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 level
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 contrast
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 desaturate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
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\Adjust;
16
17/**
18 * Adjust class for Imagick
19 *
20 * @category   Pop
21 * @package    Pop\Image
22 * @author     Nick Sagona, III <dev@noladev.com>
23 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27class Imagick extends AbstractAdjust
28{
29
30    /**
31     * Method to adjust the hue of the image.
32     *
33     * @param  int $amount
34     * @return Imagick
35     */
36    public function hue(int $amount): Imagick
37    {
38        if ($this->hasImage()) {
39            $this->image->getResource()->modulateImage(100, 100, $amount);
40        }
41        return $this;
42    }
43
44    /**
45     * Method to adjust the saturation of the image.
46     *
47     * @param  int $amount
48     * @return Imagick
49     */
50    public function saturation(int $amount): Imagick
51    {
52        if ($this->hasImage()) {
53            $this->image->getResource()->modulateImage(100, $amount, 100);
54        }
55        return $this;
56    }
57
58    /**
59     * Adjust the image brightness
60     *
61     * @param  int $amount
62     * @return Imagick
63     */
64    public function brightness(int $amount): Imagick
65    {
66        if ($this->hasImage()) {
67            $this->image->getResource()->modulateImage($amount, 100, 100);
68        }
69        return $this;
70    }
71
72    /**
73     * Method to adjust the HSB of the image altogether.
74     *
75     * @param  int $h
76     * @param  int $s
77     * @param  int $b
78     * @return Imagick
79     */
80    public function hsb(int $h, int $s, int $b): Imagick
81    {
82        if ($this->hasImage()) {
83            $this->image->getResource()->modulateImage($h, $s, $b);
84        }
85        return $this;
86    }
87
88    /**
89     * Method to adjust the levels of the image using a 0 - 255 range.
90     *
91     * @param  int   $black
92     * @param  float $gamma
93     * @param  int   $white
94     * @return Imagick
95     */
96    public function level(int $black, float $gamma, int $white): Imagick
97    {
98        if ($this->hasImage()) {
99            $quantumRange = $this->image->getResource()->getQuantumRange();
100
101            if ($black < 0) {
102                $black = 0;
103            }
104            if ($white > 255) {
105                $white = 255;
106            }
107
108            $blackPoint = ($black / 255) * $quantumRange['quantumRangeLong'];
109            $whitePoint = ($white / 255) * $quantumRange['quantumRangeLong'];
110
111            $this->image->getResource()->levelImage($blackPoint, $gamma, $whitePoint);
112        }
113
114        return $this;
115    }
116
117    /**
118     * Adjust the image contrast
119     *
120     * @param  int $amount
121     * @return Imagick
122     */
123    public function contrast(int $amount): Imagick
124    {
125        if ($this->hasImage()) {
126            if ($amount > 0) {
127                for ($i = 1; $i <= $amount; $i++) {
128                    $this->image->getResource()->contrastImage(true);
129                }
130            } else if ($amount < 0) {
131                for ($i = -1; $i >= $amount; $i--) {
132                    $this->image->getResource()->contrastImage(false);
133                }
134            }
135        }
136        return $this;
137    }
138
139    /**
140     * Adjust the image desaturate
141     *
142     * @return Imagick
143     */
144    public function desaturate(): Imagick
145    {
146        if ($this->hasImage()) {
147            $this->image->getResource()->modulateImage(100, 0, 100);
148        }
149        return $this;
150    }
151
152}