Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
10 / 10
CRAP
100.00% covered (success)
100.00%
1 / 1
Image
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
10 / 10
13
100.00% covered (success)
100.00%
1 / 1
 getAvailableAdapters
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 isAvailable
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 loadGd
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadImagick
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadGdFromString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadImagickFromString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createGd
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createGdIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createImagick
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createImagickIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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;
16
17/**
18 * Image factory class
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 Image
28{
29
30    /**
31     * Get the available image adapters
32     *
33     * @return array
34     */
35    public static function getAvailableAdapters(): array
36    {
37        return [
38            'gd'      => function_exists('gd_info'),
39            'imagick' => (class_exists('Imagick', false))
40        ];
41    }
42
43    /**
44     * Determine if the adapter is available
45     *
46     * @param  string $adapter
47     * @return bool
48     */
49    public static function isAvailable(string $adapter): bool
50    {
51        $result = false;
52
53        switch (strtolower($adapter)) {
54            case 'gd':
55                $result = function_exists('gd_info');
56                break;
57            case 'imagemagick':
58            case 'imagick':
59                $result = (class_exists('Imagick', false));
60                break;
61        }
62
63        return $result;
64    }
65
66    /**
67     * Load the image resource from the existing image file into a Gd object
68     *
69     * @param  string $image
70     * @return Adapter\Gd
71     */
72    public static function loadGd(string $image): Adapter\Gd
73    {
74        return Gd::load($image);
75    }
76
77    /**
78     * Load the image resource from the existing image file into a Imagick object
79     *
80     * @param  string $image
81     * @return Adapter\Imagick
82     */
83    public static function loadImagick(string $image): Adapter\Imagick
84    {
85        return Imagick::load($image);
86    }
87
88    /**
89     * Load the image resource from data into a Gd object
90     *
91     * @param  string  $data
92     * @param  ?string $name
93     * @throws Adapter\Exception
94     * @return Adapter\Gd
95     */
96    public static function loadGdFromString(string $data, ?string $name = null): Adapter\Gd
97    {
98        return Gd::loadFromString($data, $name);
99    }
100
101    /**
102     * Load the image resource from data into a Imagick object
103     *
104     * @param  string  $data
105     * @param  ?string $name
106     * @return Adapter\Imagick
107     */
108    public static function loadImagickFromString(string $data, ?string $name = null): Adapter\Imagick
109    {
110        return Imagick::loadFromString($data, $name);
111    }
112
113    /**
114     * Create a new image resource and load it into a Gd object
115     *
116     * @param  int    $width
117     * @param  int    $height
118     * @param  ?string $image
119     * @return Adapter\Gd
120     */
121    public static function createGd(int $width, int $height, ?string $image = null): Adapter\Gd
122    {
123        return Gd::create($width, $height, $image);
124    }
125
126    /**
127     * Create a new indexed image resource and load it into a Gd object
128     *
129     * @param  int     $width
130     * @param  int     $height
131     * @param  ?string $image
132     * @throws Adapter\Exception
133     * @return Adapter\Gd
134     */
135    public static function createGdIndex(int $width, int $height, ?string $image = null): Adapter\Gd
136    {
137        return Gd::createIndex($width, $height, $image);
138    }
139
140    /**
141     * Create a new image resource and load it into a Imagick object
142     *
143     * @param  int     $width
144     * @param  int     $height
145     * @param  ?string $image
146     * @return Adapter\Imagick
147     */
148    public static function createImagick(int $width, int $height, ?string $image = null): Adapter\Imagick
149    {
150        return Imagick::create($width, $height, $image);
151    }
152
153    /**
154     * Create a new indexed image resource and load it into a Imagick object
155     *
156     * @param  int     $width
157     * @param  int     $height
158     * @param  ?string $image
159     * @return Adapter\Imagick
160     */
161    public static function createImagickIndex(int $width, int $height, ?string $image = null): Adapter\Imagick
162    {
163        return Imagick::createIndex($width, $height, $image);
164    }
165
166}