1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image |
4
|
|
|
* |
5
|
|
|
* @Author : Jens Kooij |
6
|
|
|
* @Version: 1.0 |
7
|
|
|
* @package: JNS MVC |
8
|
|
|
* @Licence: http://creativecommons.org/licenses/by-nc-nd/3.0/ Attribution-NonCommercial-NoDerivs 3.0 Unported |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace CloudControl\Cms\images { |
12
|
|
|
|
13
|
|
|
class Image |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var resource |
17
|
|
|
*/ |
18
|
|
|
private $_imageResource; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Load the a image resource into $this->_imageResource |
22
|
|
|
* automagically :-) |
23
|
|
|
* |
24
|
|
|
* @param resource|string $imageContainer |
25
|
|
|
* |
26
|
|
|
* @throws \Exception |
27
|
|
|
*/ |
28
|
|
|
public function loadImage($imageContainer) |
29
|
|
|
{ |
30
|
|
|
if ($this->createImageResourceFromResource($imageContainer) || |
31
|
|
|
$this->createImageResourceFromFile($imageContainer) || |
32
|
|
|
$this->createImageResourceFromString($imageContainer) |
33
|
|
|
) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw new \RuntimeException('Could not create image resource, accepted inputs are: "resource of type (gd)", path_to_image and "string". <br /><pre>' . var_export($imageContainer, |
38
|
|
|
true) . '</pre>'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $imageContainer |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
private function createImageResourceFromResource($imageContainer) |
46
|
|
|
{ |
47
|
|
|
if (is_resource($imageContainer) && get_resource_type($imageContainer) === 'gd') { |
48
|
|
|
$this->_imageResource = $imageContainer; |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $imageContainer |
56
|
|
|
* @return bool |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
private function createImageResourceFromFile($imageContainer) |
60
|
|
|
{ |
61
|
|
|
if (is_string($imageContainer) && file_exists($imageContainer)) { |
62
|
|
|
if ($this->getImageMimeType($imageContainer) === IMAGETYPE_BMP) { |
63
|
|
|
$this->_imageResource = BitmapFactory::createImageFromBmp($imageContainer); |
64
|
|
|
} else { |
65
|
|
|
$imageContent = file_get_contents($imageContainer); |
66
|
|
|
$this->_imageResource = imagecreatefromstring($imageContent); |
67
|
|
|
} |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $imageContainer |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
private function createImageResourceFromString($imageContainer) |
78
|
|
|
{ |
79
|
|
|
if (is_string($imageContainer)) { |
80
|
|
|
$this->_imageResource = imagecreatefromstring($imageContainer); |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Saves the image to a file |
88
|
|
|
* |
89
|
|
|
* @param string $path |
90
|
|
|
* @param int $mimeTypeConstantValue |
91
|
|
|
* @param int $quality |
92
|
|
|
* @param resource $imageResource If no resource is given, uses $this->_imageResource |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
public function saveImage($path, $mimeTypeConstantValue, $quality = 100, $imageResource = null) |
98
|
|
|
{ |
99
|
|
|
if ($imageResource === null) { |
100
|
|
|
$imageResource = $this->getImageResource(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($mimeTypeConstantValue == IMAGETYPE_GIF) { |
104
|
|
|
return imagegif($imageResource, $path); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($mimeTypeConstantValue == IMAGETYPE_JPEG) { |
108
|
|
|
return imagejpeg($imageResource, $path, $quality); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($mimeTypeConstantValue == IMAGETYPE_PNG) { |
112
|
|
|
return imagepng($imageResource, $path, ((int)($quality / 10) - 1)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
throw new \RuntimeException('Not a valid mimetypeconstant given see function documentation'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns either the Mime-Type Constant value |
120
|
|
|
* or the default extension for the detected mime-type; |
121
|
|
|
* |
122
|
|
|
* @see http://www.php.net/manual/en/function.image-type-to-mime-type.php |
123
|
|
|
* |
124
|
|
|
* @param string $imagePath |
125
|
|
|
* @param bool $getExtension |
126
|
|
|
* |
127
|
|
|
* @return integer |
128
|
|
|
*/ |
129
|
|
|
public function getImageMimeType($imagePath, $getExtension = false) |
130
|
|
|
{ |
131
|
|
|
if (function_exists('exif_imagetype')) { |
132
|
|
|
$exif = exif_imagetype($imagePath); |
133
|
|
|
} else { |
134
|
|
|
$exif = false; |
135
|
|
|
if ((list($width, $height, $type, $attr) = getimagesize($imagePath)) !== false) { |
136
|
|
|
$exif = $type; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $getExtension ? image_type_to_extension($exif) : $exif; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the image resource |
147
|
|
|
* @return resource |
148
|
|
|
* @throws \Exception |
149
|
|
|
*/ |
150
|
|
|
final public function getImageResource() |
151
|
|
|
{ |
152
|
|
|
if (is_resource($this->_imageResource) && get_resource_type($this->_imageResource) === 'gd') { |
153
|
|
|
return $this->_imageResource; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
throw new \RuntimeException('Image resource is not set. Use $this->LoadImage to load an image into the resource'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |