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 library\images |
12
|
|
|
{ |
13
|
|
|
class Image |
14
|
|
|
{ |
15
|
|
|
private $_imageResource; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Load the a image resource into $this->_imageResource |
19
|
|
|
* automagically :-) |
20
|
|
|
* |
21
|
|
|
* @param resource | string | path |
22
|
|
|
* @throws \Exception |
23
|
|
|
*/ |
24
|
|
|
public function LoadImage($imageContainer) |
25
|
|
|
{ |
26
|
|
|
if (is_resource($imageContainer) && get_resource_type($imageContainer) === 'gd') { |
27
|
|
|
$this->_imageResource = $imageContainer; |
28
|
|
|
} elseif (is_string($imageContainer) && file_exists($imageContainer)) { |
29
|
|
|
if ($this->GetImageMimeType($imageContainer) == IMAGETYPE_BMP) { |
30
|
|
|
$this->_imageResource = $this->CreateImageFromBmp($imageContainer); |
31
|
|
|
} else { |
32
|
|
|
$imageContent = file_get_contents($imageContainer); |
33
|
|
|
$this->_imageResource = imagecreatefromstring($imageContent); |
34
|
|
|
} |
35
|
|
|
} elseif(is_string($imageContainer)) { |
36
|
|
|
$this->_imageResource = imagecreatefromstring($imageContainer); |
37
|
|
|
} else { |
38
|
|
|
throw new \Exception('Could not create image resource, accepted inputs are: "resource of type (gd)", path_to_image and "string". <br /><pre>' . var_export($imageContainer, true) . '</pre>'); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Saves the image to a file |
44
|
|
|
* |
45
|
|
|
* @param string $path |
46
|
|
|
* @param int $mimeTypeConstantValue |
47
|
|
|
* @param int $quality |
48
|
|
|
* @param null $imageResource If no resource is given, uses $this->_imageResource |
49
|
|
|
* @return bool |
50
|
|
|
* @throws \Exception |
51
|
|
|
*/ |
52
|
|
|
public function SaveImage($path, $mimeTypeConstantValue, $quality = 100, $imageResource = null) |
53
|
|
|
{ |
54
|
|
|
if ($imageResource == null) { |
55
|
|
|
$imageResource = $this->GetImageResource(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($mimeTypeConstantValue == IMAGETYPE_GIF) { |
59
|
|
|
return imagegif($imageResource, $path); |
60
|
|
|
} elseif ($mimeTypeConstantValue == IMAGETYPE_JPEG) { |
61
|
|
|
return imagejpeg($imageResource, $path, $quality); |
62
|
|
|
} elseif ($mimeTypeConstantValue == IMAGETYPE_PNG) { |
63
|
|
|
return imagepng($imageResource, $path, (intval($quality / 10) -1)); |
64
|
|
|
} |
65
|
|
|
else { |
66
|
|
|
throw new \Exception('Not a valid mimetypeconstant given see function documentation'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns either the Mime-Type Constant value |
72
|
|
|
* or the default extension for the detected mime-type; |
73
|
|
|
* |
74
|
|
|
* @see http://www.php.net/manual/en/function.image-type-to-mime-type.php |
75
|
|
|
* @param string $imagePath |
76
|
|
|
* @param bool $getExtension |
77
|
|
|
* @return bool|int|string |
78
|
|
|
*/ |
79
|
|
|
public function GetImageMimeType($imagePath, $getExtension = false) |
80
|
|
|
{ |
81
|
|
|
if (function_exists('exif_imagetype')) { |
82
|
|
|
$exif = exif_imagetype($imagePath); |
83
|
|
|
} else { |
84
|
|
|
if ((list($width, $height, $type, $attr) = getimagesize($imagePath)) !== false ) { |
85
|
|
|
$exif = $type; |
86
|
|
|
} else { |
87
|
|
|
$exif = false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return $getExtension ? image_type_to_extension($exif) : $exif; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create Image resource from Bitmap |
95
|
|
|
* |
96
|
|
|
* @see http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214 |
97
|
|
|
* @author alexander at alexauto dot nl |
98
|
|
|
* @param string $p_sFile |
99
|
|
|
* @return resource |
100
|
|
|
*/ |
101
|
|
|
public function CreateImageFromBmp($p_sFile) |
102
|
|
|
{ |
103
|
|
|
// Load the image into a string |
104
|
|
|
$file = fopen($p_sFile,"rb"); |
105
|
|
|
$read = fread($file,10); |
106
|
|
|
while(!feof($file)&&($read<>"")) |
107
|
|
|
$read .= fread($file,1024); |
108
|
|
|
|
109
|
|
|
$temp = unpack("H*",$read); |
110
|
|
|
$hex = $temp[1]; |
111
|
|
|
$header = substr($hex,0,108); |
112
|
|
|
$width=null; |
113
|
|
|
$height=null; |
114
|
|
|
|
115
|
|
|
// Process the header |
116
|
|
|
// Structure: http://www.fastgraph.com/help/bmp_header_format.html |
117
|
|
|
if (substr($header,0,4)=="424d") |
118
|
|
|
{ |
119
|
|
|
// Cut it in parts of 2 bytes |
120
|
|
|
$header_parts = str_split($header,2); |
121
|
|
|
|
122
|
|
|
// Get the width 4 bytes |
123
|
|
|
$width = hexdec($header_parts[19].$header_parts[18]); |
124
|
|
|
|
125
|
|
|
// Get the height 4 bytes |
126
|
|
|
$height = hexdec($header_parts[23].$header_parts[22]); |
127
|
|
|
|
128
|
|
|
// Unset the header params |
129
|
|
|
unset($header_parts); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Define starting X and Y |
133
|
|
|
$x = 0; |
134
|
|
|
$y = 1; |
135
|
|
|
|
136
|
|
|
// Create newimage |
137
|
|
|
$image = imagecreatetruecolor($width,$height); |
138
|
|
|
|
139
|
|
|
// Grab the body from the image |
140
|
|
|
$body = substr($hex,108); |
141
|
|
|
|
142
|
|
|
// Calculate if padding at the end-line is needed |
143
|
|
|
// Divided by two to keep overview. |
144
|
|
|
// 1 byte = 2 HEX-chars |
145
|
|
|
$body_size = (strlen($body)/2); |
146
|
|
|
$header_size = ($width*$height); |
147
|
|
|
|
148
|
|
|
// Use end-line padding? Only when needed |
149
|
|
|
$usePadding = ($body_size>($header_size*3)+4); |
150
|
|
|
|
151
|
|
|
// Using a for-loop with index-calculation instaid of str_split to avoid large memory consumption |
152
|
|
|
// Calculate the next DWORD-position in the body |
153
|
|
|
for ($i=0;$i<$body_size;$i+=3) |
154
|
|
|
{ |
155
|
|
|
// Calculate line-ending and padding |
156
|
|
|
if ($x>=$width) |
157
|
|
|
{ |
158
|
|
|
// If padding needed, ignore image-padding |
159
|
|
|
// Shift i to the ending of the current 32-bit-block |
160
|
|
|
if ($usePadding) |
161
|
|
|
$i += $width%4; |
162
|
|
|
|
163
|
|
|
// Reset horizontal position |
164
|
|
|
$x = 0; |
165
|
|
|
|
166
|
|
|
// Raise the height-position (bottom-up) |
167
|
|
|
$y++; |
168
|
|
|
|
169
|
|
|
// Reached the image-height? Break the for-loop |
170
|
|
|
if ($y>$height) |
171
|
|
|
break; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// Calculation of the RGB-pixel (defined as BGR in image-data) |
175
|
|
|
// Define $i_pos as absolute position in the body |
176
|
|
|
$i_pos = $i*2; |
177
|
|
|
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]); |
178
|
|
|
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]); |
179
|
|
|
$b = hexdec($body[$i_pos].$body[$i_pos+1]); |
180
|
|
|
|
181
|
|
|
// Calculate and draw the pixel |
182
|
|
|
$color = imagecolorallocate($image,$r,$g,$b); |
183
|
|
|
imagesetpixel($image,$x,$height-$y,$color); |
184
|
|
|
|
185
|
|
|
// Raise the horizontal position |
186
|
|
|
$x++; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Unset the body / free the memory |
190
|
|
|
unset($body); |
191
|
|
|
|
192
|
|
|
// Return image-object |
193
|
|
|
return $image; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the image resource |
198
|
|
|
* @return resource |
199
|
|
|
* @throws \Exception |
200
|
|
|
*/ |
201
|
|
|
final public function GetImageResource() |
202
|
|
|
{ |
203
|
|
|
if (is_resource($this->_imageResource) && get_resource_type($this->_imageResource) === 'gd') { |
204
|
|
|
return $this->_imageResource; |
205
|
|
|
} else { |
206
|
|
|
throw new \Exception('Image resource is not set. Use $this->LoadImage to load an image into the resource'); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |