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