Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
54.05% covered (warning)
54.05%
20 / 37
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Imagick
54.05% covered (warning)
54.05%
20 / 37
50.00% covered (warning)
50.00%
1 / 2
36.82
0.00% covered (danger)
0.00%
0 / 1
 setOpacity
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 text
51.43% covered (warning)
51.43%
18 / 35
0.00% covered (danger)
0.00%
0 / 1
36.46
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\Type;
16
17use ImagickException;
18use ImagickDrawException;
19
20/**
21 * Type class for Imagick
22 *
23 * @category   Pop
24 * @package    Pop\Image
25 * @author     Nick Sagona, III <dev@noladev.com>
26 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    5.0.0
29 */
30class Imagick extends AbstractType
31{
32
33    /**
34     * Opacity
35     * @var int|float|null
36     */
37    protected int|float|null $opacity = 100;
38
39    /**
40     * Set the opacity
41     *
42     * @param  int|float $opacity
43     * @return Imagick
44     */
45    public function setOpacity(int|float $opacity): Imagick
46    {
47        $this->opacity = $opacity;
48        return $this;
49    }
50
51    /**
52     * Set and apply the text on the image
53     *
54     * @param string $string
55     * @throws Exception|ImagickException|ImagickDrawException
56     * @return Imagick
57     */
58    public function text(string $string): Imagick
59    {
60        if ($this->hasImage()) {
61            $draw = new \ImagickDraw();
62
63            // Set the font if passed
64            if ($this->font !== null) {
65                if (!$draw->setFont($this->font)) {
66                    throw new Exception("Error: The font '" . $this->font . "' is not recognized by the Imagick extension.");
67                }
68                // Else, attempt to set a basic, default system font
69            } else {
70                $fonts = $this->image->getResource()->queryFonts();
71                if (in_array('Arial', $fonts)) {
72                    $this->font = 'Arial';
73                } else if (in_array('Helvetica', $fonts)) {
74                    $this->font = 'Helvetica';
75                } else if (in_array('Tahoma', $fonts)) {
76                    $this->font = 'Tahoma';
77                } else if (in_array('Verdana', $fonts)) {
78                    $this->font = 'Verdana';
79                } else if (in_array('System', $fonts)) {
80                    $this->font = 'System';
81                } else if (in_array('Fixed', $fonts)) {
82                    $this->font = 'Fixed';
83                } else if (in_array('system', $fonts)) {
84                    $this->font = 'system';
85                } else if (in_array('fixed', $fonts)) {
86                    $this->font = 'fixed';
87                } else if (isset($fonts[0])) {
88                    $this->font = $fonts[0];
89                } else {
90                    throw new Exception('Error: No default font could be found by the Imagick extension.');
91                }
92            }
93
94            $draw->setFont($this->font);
95            $draw->setFontSize($this->size);
96            $draw->setFillColor($this->image->createColor($this->fillColor, (int)$this->opacity));
97
98            $draw->rotate($this->rotation);
99
100            if ($this->strokeColor !== null) {
101                $draw->setStrokeColor($this->image->createColor($this->strokeColor, (int)$this->opacity));
102                $draw->setStrokeWidth((int)$this->strokeWidth);
103            }
104
105            $draw->annotation($this->x, $this->y, $string);
106            $this->image->getResource()->drawImage($draw);
107        }
108
109        return $this;
110    }
111
112}