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