├── test
├── image1.jpg
├── image2.jpg
├── image3.jpg
├── image4.jpg
├── image5.jpg
├── image6.jpg
├── image7.jpg
├── image8.jpg
├── pngWithTransparency.png
├── pngTest.php
└── test.php
├── examples
├── example1.php
├── example5.php
├── example2.php
├── index.htm
├── example6.php
├── example4.php
└── example3.php
├── .gitignore
├── README.md
└── class.Images.php
/test/image1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image1.jpg
--------------------------------------------------------------------------------
/test/image2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image2.jpg
--------------------------------------------------------------------------------
/test/image3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image3.jpg
--------------------------------------------------------------------------------
/test/image4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image4.jpg
--------------------------------------------------------------------------------
/test/image5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image5.jpg
--------------------------------------------------------------------------------
/test/image6.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image6.jpg
--------------------------------------------------------------------------------
/test/image7.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image7.jpg
--------------------------------------------------------------------------------
/test/image8.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/image8.jpg
--------------------------------------------------------------------------------
/test/pngWithTransparency.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprain/class.Images.php/HEAD/test/pngWithTransparency.png
--------------------------------------------------------------------------------
/examples/example1.php:
--------------------------------------------------------------------------------
1 | rotate(90);
9 |
10 | //and we can resize and crop it to have a nice thumbnail
11 | $image->resize(150,150,'crop');
12 |
13 | //lets see what we have!
14 | $image->display();
--------------------------------------------------------------------------------
/test/pngTest.php:
--------------------------------------------------------------------------------
1 | rotate(90);
9 |
10 | //and we can resize and crop it to have a nice thumbnail
11 | $image->resize(150,150,'crop');
12 |
13 | //lets see what we have!
14 | $image->display();
--------------------------------------------------------------------------------
/examples/example5.php:
--------------------------------------------------------------------------------
1 | checkRatio(1,2)){
10 | print 'Yes, you made it!';
11 | }else{
12 | print 'Sorry dude, the actual ratio is 1:' . round($image->getRatioWidthToHeight(), 2);
13 | }
--------------------------------------------------------------------------------
/examples/example2.php:
--------------------------------------------------------------------------------
1 | rotate(90);
7 | $image->resize(150,150,'crop');
8 |
9 | /*
10 | Now save the file. First give a filename (without file extension, the class will figure it out!)
11 | Then, optionally, give path to the folder where you want the image to be saved.
12 | Of course, this directory will have to be writable! */
13 | $image->save("newFilename", "../test");
14 |
15 | /*
16 | and... oh pretty wow! If the path of the image is accessible to the browser
17 | you can now even display the whole HTML code of the
-Tag */
18 | $image->displayHTML();
--------------------------------------------------------------------------------
/examples/index.htm:
--------------------------------------------------------------------------------
1 |
Some examples for class.Images.php
2 |
3 | Here are some examples of what you can do with this class. Make sure to check the source code of those files as well as of the class itself as well as there are plenty of more parameters and options.
4 |
5 |
--------------------------------------------------------------------------------
/examples/example6.php:
--------------------------------------------------------------------------------
1 | getWidth().'
';
9 | print 'Height: '.$image->getHeight().'
';
10 | print 'Extension: '.$image->getExtension().'
';
11 | print 'Mime: '.$image->getMimeType().'
';
12 | print 'Type: '.$image->getType().'
';
13 | print 'Filesize in Bytes: '.$image->getFileSizeInBytes().'
';
14 | print 'Filesize in kBytes: '.$image->getFileSizeInKiloBytes().'
';
15 | print 'Filesize readable: '.$image->getFileSize().'
';
16 | print 'Ratio Width:Height: '.$image->getRatioWidthToHeight().'
';
17 | print 'Ratio Height:Width: '.$image->getRatioHeightToWidth().'
';
18 | print 'Is it RGB?: '.$image->isRGB();
19 |
--------------------------------------------------------------------------------
/test/test.php:
--------------------------------------------------------------------------------
1 | resize($newWidth, $newHeight, 'crop', 'l', 't');
9 | $Image->save('testimage_square_'.$i);
10 | $Image->displayHTML();
11 | }//for
12 |
13 | for ($i = 1; $i <= 8; $i++) {
14 | $Image = new Image('image'.$i.'.jpg');
15 | $newWidth = 600;
16 | $newHeight = 200;
17 | $Image->resize($newWidth, $newHeight, 'crop', 'l', 't');
18 | $Image->save('testimage_landscape_'.$i);
19 | $Image->displayHTML();
20 | }//for
21 |
22 | for ($i = 1; $i <= 8; $i++) {
23 | $Image = new Image('image'.$i.'.jpg');
24 | $newWidth = 200;
25 | $newHeight = 600;
26 | $Image->resize($newWidth, $newHeight, 'crop', 'l', 't');
27 | $Image->save('testimage_portrait_'.$i);
28 | $Image->displayHTML();
29 | }//for
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /test/testimage_square_8.jpg
2 | /test/testimage_square_7.jpg
3 | /test/testimage_square_6.jpg
4 | /test/testimage_square_5.jpg
5 | /test/testimage_square_4.jpg
6 | /test/testimage_square_3.jpg
7 | /test/testimage_square_2.jpg
8 | /test/testimage_square_1.jpg
9 | /test/testimage_portrait_8.jpg
10 | /test/testimage_portrait_7.jpg
11 | /test/testimage_portrait_6.jpg
12 | /test/testimage_portrait_5.jpg
13 | /test/testimage_portrait_4.jpg
14 | /test/testimage_portrait_3.jpg
15 | /test/testimage_portrait_2.jpg
16 | /test/testimage_portrait_1.jpg
17 | /test/testimage_landscape_8.jpg
18 | /test/testimage_landscape_7.jpg
19 | /test/testimage_landscape_6.jpg
20 | /test/testimage_landscape_5.jpg
21 | /test/testimage_landscape_4.jpg
22 | /test/testimage_landscape_3.jpg
23 | /test/testimage_landscape_2.jpg
24 | /test/testimage_landscape_1.jpg
25 | /test/test.php
26 | /test/right-top.jpg
27 | /test/newFilename.png
28 | /test/left-bottom.jpg
29 | /test/image1.png
30 | /test/fit.jpg
31 | /test/fill.jpg
32 | /test/crop.jpg
33 | /test/center-center.jpg
--------------------------------------------------------------------------------
/examples/example4.php:
--------------------------------------------------------------------------------
1 | resize(400, 400, 'crop', 'l', 'b');
19 | $image->save('left-bottom', $pathToSaveFiles);
20 | $image->displayHTML();
21 |
22 | $image = new Image($mainImage);
23 | $image->resize(400, 400, 'crop', 'r', 't');
24 | $image->save('right-top', $pathToSaveFiles);
25 | $image->displayHTML();
26 |
27 | $image = new Image($mainImage);
28 | $image->resize(400, 400, 'crop', 'c', 'c');
29 | $image->save('center-center', $pathToSaveFiles);
30 | $image->displayHTML();
--------------------------------------------------------------------------------
/examples/example3.php:
--------------------------------------------------------------------------------
1 | resize(300,100);
16 | $image->save('fit', $pathToSaveFiles);
17 | $image->displayHTML();
18 |
19 |
20 | /*
21 | If you set the option 'fill' the image will fill the whole width and
22 | height you provided. But it will loose it aspect ratio. So be ready
23 | to see your image deformed.*/
24 |
25 | $image = new Image($mainImage);
26 | $image->resize(300,100, 'fill');
27 | $image->save('fill', $pathToSaveFiles);
28 | $image->displayHTML();
29 |
30 |
31 | /*
32 | If you choose to 'crop' the image, it won't lose its aspect ratio but
33 | parts of the image will be cut off. */
34 |
35 | $image = new Image($mainImage);
36 | $image->resize(300, 100, 'crop');
37 | $image->save('crop', $pathToSaveFiles);
38 | $image->displayHTML();
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # class.Images.php
2 |
3 | Yes, I know. It is yet another image manipulation class for PHP. But so far I haven’t found the one that really matches my needs. Either they are too complicated or bound to another system. So I simply created my own class with just the functions I need. So it’s the perfect class for me. If you like it, too – even better! Feel free to contribute and make it better.
4 |
5 | *Please note:* This class has been developed some years ago. There are probably better solutions out there by now. If I ever find the time I might redo this class. In the meantime however, feel free to contribute and improve it.
6 |
7 | ## Examples
8 |
9 | Check out the examples folder for more examples.
10 |
11 | ### Basic example
12 | Here’s a basic example of what you can do and this is what you’ll get with it.
13 |
14 | rotate(90);
22 |
23 | //and we can resize and crop it to have a nice thumbnail
24 | $image->resize(150,150,'crop');
25 |
26 | //lets see what we have!
27 | $image->display();
28 |
29 |
30 | ## Contact
31 |
32 | Manuel Reinhard, ·
33 | ·
34 |
35 |
36 |
37 | ## License
38 |
39 | Copyright (c) 2010 until today, Manuel Reinhard
40 |
41 | Permission is hereby granted, free of charge, to any person obtaining a copy
42 | of this software and associated documentation files (the "Software"), to deal
43 | in the Software without restriction, including without limitation the rights
44 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
45 | copies of the Software, and to permit persons to whom the Software is furnished
46 | to do so, subject to the following conditions:
47 |
48 | The above copyright notice and this permission notice shall be included in all
49 | copies or substantial portions of the Software.
50 |
51 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
52 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
53 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
54 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
55 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
56 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
57 | THE SOFTWARE.
58 |
--------------------------------------------------------------------------------
/class.Images.php:
--------------------------------------------------------------------------------
1 | setPathToTempFiles(sys_get_temp_dir());
35 | }else{
36 | $this->setPathToTempFiles($_SERVER["DOCUMENT_ROOT"]);
37 | }
38 |
39 | $this->isSourceImage = (bool)$isSourceImage;
40 |
41 | if(file_exists($image)){
42 | $this->image = $image;
43 | $this->readImageInfo();
44 | }else{
45 | throw new Exception("File does not exist: ".$image);
46 | }
47 | }
48 |
49 | /**
50 | * Destructor of this class
51 | * @param string $image (path to image)
52 | */
53 | public function __destruct()
54 | {
55 | if(file_exists($this->tmpfile)){
56 | unlink($this->tmpfile);
57 | }
58 | }
59 |
60 | /**
61 | * Sets response expiry header value
62 | * @param integer $expires (seconds)
63 | */
64 | public function setExpires($expires=0){
65 | $this->expires = intval($expires);
66 | }//function
67 |
68 |
69 | /**
70 | * Read and set some basic info about the image
71 | * @param string $image (path to image)
72 | */
73 | protected function readImageInfo()
74 | {
75 | $data = getimagesize($this->image);
76 |
77 | $this->imageInfo["width"] = $data[0];
78 | $this->imageInfo["height"] = $data[1];
79 | $this->imageInfo["imagetype"] = $data[2];
80 | $this->imageInfo["htmlWidthAndHeight"] = $data[3];
81 | $this->imageInfo["mime"] = $data["mime"];
82 | $this->imageInfo["channels"] = ( isset($data["channels"]) ? $data["channels"] : NULL );
83 | $this->imageInfo["bits"] = $data["bits"];
84 | if($this->isSourceImage && filemtime($this->image)!==time()){
85 | $this->lastModified = filemtime($this->image);
86 | }
87 | return true;
88 | }
89 |
90 | /************************************
91 | /* SETTERS
92 | /************************************
93 |
94 | /**
95 | * Sets path to temp files
96 | * @param string $path
97 | */
98 | public function setPathToTempFiles($path)
99 | {
100 | $path = realpath($path).DIRECTORY_SEPARATOR;
101 | $this->pathToTempFiles = $path;
102 | $this->tmpfile = tempnam($this->pathToTempFiles, "classImagePhp_");
103 |
104 | return true;
105 | }
106 |
107 | /**
108 | * Sets new image type
109 | * @param string $newFileType (jpeg, png, bmp, gif, vnd.wap.wbmp, xbm)
110 | */
111 | public function setNewFileType($newFileType)
112 | {
113 | $this->newFileType = strtolower( $newFileType );
114 |
115 | return true;
116 | }
117 |
118 | /**
119 | * Sets new main image
120 | * @param string $pathToImage
121 | */
122 | protected function setNewMainImage($pathToImage)
123 | {
124 | $this->image = $pathToImage;
125 | $this->readImageInfo();
126 |
127 | return true;
128 | }
129 |
130 | /************************************
131 | /* ACTIONS
132 | /************************************
133 |
134 | /**
135 | * Resizes an image
136 | * Some portions of this function as found on
137 | * http://www.bitrepository.com/resize-an-image-keeping-its-aspect-ratio-using-php-and-gd.html
138 | * @param int $max_width
139 | * @param int $max_height
140 | * @param string $method
141 | * fit = Fits image into width and height while keeping original aspect ratio. Expect your image not to use the full area.
142 | * crop = Crops image to fill the area while keeping original aspect ratio. Expect your image to get, well, cropped.
143 | * fill = Fits image into the area without taking care of any ratios. Expect your image to get deformed.
144 | *
145 | * @param string $cropAreaLeftRight
146 | * l = left
147 | * c = center
148 | * r = right
149 | * array( x-coordinate, width)
150 | *
151 | * @param string $cropAreaBottomTop
152 | * t = top
153 | * c = center
154 | * b = bottom
155 | * array( y-coordinate, height)
156 | */
157 | public function resize($max_width, $max_height, $method="fit", $cropAreaLeftRight="c", $cropAreaBottomTop="c", $jpgQuality=75, $enlarge=false)
158 | {
159 | $width = $this->getWidth();
160 | $height = $this->getHeight();
161 |
162 | $newImage_width = $max_width;
163 | $newImage_height = $max_height;
164 | $srcX = 0;
165 | $srcY = 0;
166 |
167 | //Get ratio of max_width : max_height
168 | $ratioOfMaxSizes = $max_width / $max_height;
169 |
170 | //Want to fit in the area?
171 | if($method == "fit"){
172 |
173 | if($ratioOfMaxSizes >= $this->getRatioWidthToHeight()){
174 | $max_width = $max_height * $this->getRatioWidthToHeight();
175 | }else{
176 | $max_height = $max_width * $this->getRatioHeightToWidth();
177 | }
178 |
179 | //set image data again
180 | $newImage_width = $max_width;
181 | $newImage_height = $max_height;
182 |
183 |
184 | //or want to crop it?
185 | }elseif($method == "crop"){
186 |
187 | //set new max height or width
188 | if($ratioOfMaxSizes > $this->getRatioWidthToHeight()){
189 | $max_height = $max_width * $this->getRatioHeightToWidth();
190 | }else{
191 | $max_width = $max_height * $this->getRatioWidthToHeight();
192 | }
193 |
194 | //which area to crop?
195 | if (is_array($cropAreaLeftRight)) {
196 | $srcX = $cropAreaLeftRight[0];
197 | if($ratioOfMaxSizes > $this->getRatioWidthToHeight()){
198 | $width = $cropAreaLeftRight[1];
199 | }else{
200 | $width = $cropAreaLeftRight[1] * $this->getRatioWidthToHeight();
201 | }
202 | } elseif ($cropAreaLeftRight == "r") {
203 | $srcX = $width - (($newImage_width / $max_width) * $width);
204 | } elseif ($cropAreaLeftRight == "c") {
205 | $srcX = ($width/2) - ((($newImage_width / $max_width) * $width) / 2);
206 | }
207 |
208 | if (is_array($cropAreaBottomTop)) {
209 | $srcY = $cropAreaBottomTop[0];
210 | if ($ratioOfMaxSizes > $this->getRatioWidthToHeight()) {
211 | $height = $cropAreaBottomTop[1] * $this->getRatioHeightToWidth();
212 | } else {
213 | $height = $cropAreaBottomTop[1];
214 | }
215 | } elseif ($cropAreaBottomTop == "b") {
216 | $srcY = $height - (($newImage_height / $max_height) * $height);
217 | } elseif ($cropAreaBottomTop == "c") {
218 | $srcY = ($height/2) - ((($newImage_height / $max_height) * $height) / 2);
219 | }
220 | }
221 |
222 | if(!$enlarge && ($newImage_width>$width || $newImage_height>$height)){
223 | $newImage_width = $width;
224 | $max_width = $width;
225 | $newImage_height = $height;
226 | $max_height = $height;
227 | }
228 |
229 | //Let's get it on, create image!
230 | list($image_create_func, $image_save_func) = $this->getFunctionNames();
231 |
232 | // check if it is a jpg and if there are exif data about Orientation (e.g. on uploading an image from smartphone)
233 | if( $this->getMimeType() == "image/jpg" || $this->getMimeType() == "image/jpeg")
234 | {
235 | $exif = exif_read_data($this->image);
236 | if(!empty($exif['Orientation'])) {
237 | switch($exif['Orientation']) {
238 | case 8:
239 | $this->rotate(90, $jpgQuality);
240 | break;
241 | case 3:
242 | $this->rotate(180, $jpgQuality);
243 | break;
244 | case 6:
245 | $this->rotate(-90, $jpgQuality);
246 | break;
247 | }
248 | }
249 | }
250 |
251 | $imageC = ImageCreateTrueColor($newImage_width, $newImage_height);
252 | $newImage = $image_create_func($this->image);
253 |
254 | if($image_save_func == 'ImagePNG'){
255 | //http://www.akemapa.com/2008/07/10/php-gd-resize-transparent-image-png-gif/
256 | imagealphablending($imageC, false);
257 | imagesavealpha($imageC, true);
258 | $transparent = imagecolorallocatealpha($imageC, 255, 255, 255, 127);
259 | imagefilledrectangle($imageC, 0, 0, $newImage_width, $newImage_height, $transparent);
260 | }
261 | ImageCopyResampled($imageC, $newImage, 0, 0, $srcX, $srcY, $max_width, $max_height, $width, $height);
262 |
263 | //Set image
264 | if($image_save_func == "imageJPG" || $image_save_func == "ImageJPEG"){
265 | if(!$image_save_func($imageC, $this->tmpfile, $jpgQuality)){
266 | throw new Exception("Cannot save file ".$this->tmpfile);
267 | }
268 | }else{
269 | if(!$image_save_func($imageC, $this->tmpfile)){
270 | throw new Exception("Cannot save file ".$this->tmpfile);
271 | }
272 | }
273 |
274 | //Set new main image
275 | $this->setNewMainImage($this->tmpfile);
276 |
277 | //Free memory!
278 | imagedestroy($imageC);
279 | }
280 |
281 | /**
282 | * Adds a watermark
283 | */
284 | public function addWatermark($imageWatermark)
285 | {
286 | $this->Watermark = new self($imageWatermark, false);
287 | $this->Watermark->setPathToTempFiles($this->pathToTempFiles);
288 |
289 | return $this->Watermark;
290 | }
291 |
292 |
293 | /**
294 | * Writes Watermark to the File
295 | * @param int $oapcity
296 | * @param int $marginH (margin in pixel from base image horizontally)
297 | * @param int $marginV (margin in pixel from base image vertically)
298 | *
299 | * @param string $positionWatermarkLeftRight
300 | * l = left
301 | * c = center
302 | * r = right
303 | *
304 | * @param string $positionWatermarkTopBottom
305 | * t = top
306 | * c = center
307 | * b = bottom
308 | */
309 | public function writeWatermark($opacity=50, $marginH=0, $marginV=0, $positionWatermarkLeftRight="c", $positionWatermarkTopBottom="c")
310 | {
311 | //add Watermark
312 | list($image_create_func, $image_save_func) = $this->Watermark->getFunctionNames();
313 | $watermark = $image_create_func($this->Watermark->getImage());
314 |
315 | //get base image
316 | list($image_create_func, $image_save_func) = $this->getFunctionNames();
317 | $baseImage = $image_create_func($this->image);
318 |
319 | //Calculate margins
320 | if($positionWatermarkLeftRight == "r"){
321 | $marginH = imagesx($baseImage) - imagesx($watermark) - $marginH;
322 | }
323 |
324 | if($positionWatermarkLeftRight == "c"){
325 | $marginH = (imagesx($baseImage)/2) - (imagesx($watermark)/2) - $marginH;
326 | }
327 |
328 | if($positionWatermarkTopBottom == "b"){
329 | $marginV = imagesy($baseImage) - imagesy($watermark) - $marginV;
330 | }
331 |
332 | if($positionWatermarkTopBottom == "c"){
333 | $marginV = (imagesy($baseImage)/2) - (imagesy($watermark)/2) - $marginV;
334 | }
335 |
336 | //****************************
337 | //Add watermark and keep alpha channel of pngs.
338 | //The following lines are based on the code found on
339 | //http://ch.php.net/manual/en/function.imagecopymerge.php#92787
340 | //****************************
341 |
342 | // creating a cut resource
343 | $cut = imagecreatetruecolor(imagesx($watermark), imagesy($watermark));
344 |
345 | // copying that section of the background to the cut
346 | imagecopy($cut, $baseImage, 0, 0, $marginH, $marginV, imagesx($watermark), imagesy($watermark));
347 |
348 | // placing the watermark now
349 | imagecopy($cut, $watermark, 0, 0, 0, 0, imagesx($watermark), imagesy($watermark));
350 | imagecopymerge($baseImage, $cut, $marginH, $marginV, 0, 0, imagesx($watermark), imagesy($watermark), $opacity);
351 |
352 | //****************************
353 | //****************************
354 |
355 | //Set image
356 | if(!$image_save_func($baseImage, $this->tmpfile)){
357 | throw new Exception("Cannot save file ".$this->tmpfile);
358 | }
359 |
360 | //Set new main image
361 | $this->setNewMainImage($this->tmpfile);
362 |
363 | //Free memory!
364 | imagedestroy($baseImage);
365 | unset($Watermark);
366 | }
367 |
368 | /**
369 | * Roates an image
370 | */
371 | public function rotate($degrees, $jpgQuality=75)
372 | {
373 | list($image_create_func, $image_save_func) = $this->getFunctionNames();
374 |
375 | $source = $image_create_func($this->image);
376 | if(function_exists("imagerotate")){
377 | $imageRotated = imagerotate($source, $degrees, 0, true);
378 | }else{
379 | $imageRotated = $this->rotateImage($source, $degrees);
380 | }
381 |
382 | if($image_save_func == "ImageJPEG"){
383 | if(!$image_save_func($imageRotated, $this->tmpfile, $jpgQuality)){
384 | throw new Exception("Cannot save file ".$this->tmpfile);
385 | }
386 | }else{
387 | if(!$image_save_func($imageRotated, $this->tmpfile)){
388 | throw new Exception("Cannot save file ".$this->tmpfile);
389 | }
390 | }
391 |
392 | //Set new main image
393 | $this->setNewMainImage($this->tmpfile);
394 |
395 | return true;
396 | }
397 |
398 | /**
399 | * Sends image data to browser
400 | */
401 | public function display()
402 | {
403 | $mime = $this->getMimeType();
404 | header("Content-Type: ".$mime);
405 | header("Cache-Control: public");
406 | header("Expires: ". date("r",time() + ($this->expires)));
407 | if($this->lastModified>0){
408 | header("Last-Modified: ".gmdate("D, d M Y H:i:s", $this->lastModified)." GMT");
409 | }
410 | readfile($this->image);
411 | }
412 |
413 | /**
414 | * Prints html code to display image
415 | */
416 | public function displayHTML($alt=false, $title=false, $class=false, $id=false, $extras=false)
417 | {
418 | print $this->getHTML($alt, $title, $class, $id, $extras);
419 | }
420 |
421 | /**
422 | * Creates html code to display image
423 | */
424 | public function getHTML($alt=false, $title=false, $class=false, $id=false, $extras=false)
425 | {
426 | $path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $this->image);
427 |
428 | $code = '
getExtension(true);
447 | }else{
448 | $filename .= ".".$extension;
449 | }
450 |
451 | //add trailing slash if necessary
452 | if($path != ""){
453 | $path = realpath($path).DIRECTORY_SEPARATOR;
454 | }
455 |
456 | //create full path
457 | $fullPath = $path.$filename;
458 |
459 | //Copy file
460 | if(!copy($this->image, $fullPath)){
461 | throw new Exception("Cannot save file ".$fullPath);
462 | }
463 |
464 | //Set new main image
465 | $this->setNewMainImage($fullPath);
466 |
467 | return true;
468 | }
469 |
470 | /************************************
471 | /* CHECKERS
472 | /************************************
473 |
474 | /**
475 | * Checks whether image is RGB
476 | * @return bool
477 | */
478 | public function isRGB()
479 | {
480 | if($this->imageInfo["channels"] == 3){
481 | return true;
482 | }
483 | return false;
484 | }
485 |
486 | /**
487 | * Checks whether image is RGB
488 | * @return bool
489 | */
490 | public function isCMYK()
491 | {
492 | if($this->imageInfo["channels"] == 4){
493 | return true;
494 | }
495 | return false;
496 | }
497 |
498 | /**
499 | * Checks ratio width:height
500 | * Examples:
501 | * Ratio must be 4:3 > checkRatio(4,3)
502 | * Ratio must be 4:3 or 3:4 > checkRatio(4,3, true)
503 | * @return bool
504 | */
505 | public function checkRatio($ratio1, $ratio2, $ignoreOrientation=false)
506 | {
507 | $actualRatioWidthToHeight = $this->getRatioWidthToHeight();
508 | $shouldBeRatio = $ratio1 / $ratio2;
509 |
510 | if($actualRatioWidthToHeight == $shouldBeRatio){
511 | return true;
512 | }
513 |
514 | $actualRatioHeightToWidth = $this->getRatioHeightToWidth();
515 | if($ignoreOrientation && $actualRatioHeightToWidth == $shouldBeRatio){
516 | return true;
517 | }
518 |
519 | return false;
520 | }
521 |
522 | /************************************
523 | /* GETTERS
524 | /************************************
525 |
526 | /**
527 | * Returns function names
528 | */
529 | protected function getFunctionNames()
530 | {
531 | if (null == $this->newFileType) {
532 | $this->setNewFileType($this->getType());
533 | }
534 |
535 | switch ($this->getType()) {
536 | case 'jpg':
537 | case 'jpeg':
538 | $image_create_func = 'ImageCreateFromJPEG';
539 | break;
540 |
541 | case 'png':
542 | $image_create_func = 'ImageCreateFromPNG';
543 | break;
544 |
545 | case 'bmp':
546 | $image_create_func = 'ImageCreateFromBMP';
547 | break;
548 |
549 | case 'gif':
550 | $image_create_func = 'ImageCreateFromGIF';
551 | break;
552 |
553 | case 'vnd.wap.wbmp':
554 | $image_create_func = 'ImageCreateFromWBMP';
555 | break;
556 |
557 | case 'xbm':
558 | $image_create_func = 'ImageCreateFromXBM';
559 | break;
560 |
561 | default:
562 | $image_create_func = 'ImageCreateFromJPEG';
563 | }
564 |
565 | switch ($this->newFileType) {
566 | case 'jpg':
567 | case 'jpeg':
568 | $image_save_func = 'ImageJPEG';
569 | break;
570 |
571 | case 'png':
572 | $image_save_func = 'ImagePNG';
573 | break;
574 |
575 | case 'bmp':
576 | $image_save_func = 'ImageBMP';
577 | break;
578 |
579 | case 'gif':
580 | $image_save_func = 'ImageGIF';
581 | break;
582 |
583 | case 'vnd.wap.wbmp':
584 | $image_save_func = 'ImageWBMP';
585 | break;
586 |
587 | case 'xbm':
588 | $image_save_func = 'ImageXBM';
589 | break;
590 |
591 | default:
592 | $image_save_func = 'ImageJPEG';
593 | }
594 |
595 | return array($image_create_func, $image_save_func);
596 | }
597 |
598 | /**
599 | * returns the image
600 | */
601 | protected function getImage()
602 | {
603 | return $this->image;
604 | }
605 |
606 | /**
607 | * return info about the image
608 | */
609 | public function getImageInfo()
610 | {
611 | return $this->imageInfo;
612 | }
613 |
614 | /**
615 | * return info about the file
616 | */
617 | public function getFileInfo()
618 | {
619 | return $this->fileInfo;
620 | }
621 |
622 | /**
623 | * Gets width of image
624 | * @return int
625 | */
626 | public function getWidth()
627 | {
628 | return $this->imageInfo["width"];
629 | }
630 |
631 | /**
632 | * Gets height of image
633 | * @return int
634 | */
635 | public function getHeight()
636 | {
637 | return $this->imageInfo["height"];
638 | }
639 |
640 | /**
641 | * Gets type of image
642 | * @return string
643 | */
644 | public function getExtension($withDot=false)
645 | {
646 | $extension = image_type_to_extension($this->imageInfo["imagetype"]);
647 | $extension = str_replace("jpeg", "jpg", $extension);
648 | if(!$withDot){
649 | $extension = substr($extension, 1);
650 | }
651 |
652 | return $extension;
653 | }
654 |
655 | /**
656 | * Gets mime type of image
657 | * @return string
658 | */
659 | public function getMimeType()
660 | {
661 | return $this->imageInfo["mime"];
662 | }
663 |
664 | /**
665 | * Gets mime type of image
666 | * @return string
667 | */
668 | public function getType()
669 | {
670 | return substr(strrchr($this->imageInfo["mime"], '/'), 1);
671 | }
672 |
673 | /**
674 | * Get filesize
675 | * @return string
676 | */
677 | public function getFileSizeInBytes()
678 | {
679 | return filesize($this->image);
680 | }
681 |
682 | /**
683 | * Get filesize
684 | * @return string
685 | */
686 | public function getFileSizeInKiloBytes()
687 | {
688 | $size = $this->getFileSizeInBytes();
689 | return $size/1024;
690 | }
691 |
692 | /**
693 | * Returns a human readable filesize
694 | * @author wesman20 (php.net)
695 | * @author Jonas John
696 | * @author Manuel Reinhard
697 | * @link http://www.jonasjohn.de/snippets/php/readable-filesize.htm
698 | * @link http://www.php.net/manual/en/function.filesize.php
699 | */
700 | public function getFileSize()
701 | {
702 | $size = $this->getFileSizeInBytes();
703 |
704 | $mod = 1024;
705 | $units = explode(' ','B KB MB GB TB PB');
706 | for ($i = 0; $size > $mod; $i++) {
707 | $size /= $mod;
708 | }
709 |
710 | //round differently depending on unit to use
711 | if($i < 2){
712 | $size = round($size);
713 | }else{
714 | $size = round($size, 2);
715 | }
716 |
717 | return $size . ' ' . $units[$i];
718 | }
719 |
720 | /**
721 | * Gets ratio width:height
722 | * @return float
723 | */
724 | public function getRatioWidthToHeight()
725 | {
726 | return $this->imageInfo["width"] / $this->imageInfo["height"];
727 | }
728 |
729 | /**
730 | * Gets ratio height:width
731 | * @return float
732 | */
733 | public function getRatioHeightToWidth()
734 | {
735 | return $this->imageInfo["height"] / $this->imageInfo["width"];
736 | }
737 |
738 | /************************************
739 | /* OTHER STUFF
740 | /************************************
741 |
742 | /**
743 | * Replacement for imagerotate if it doesn't exist
744 | * As found on http://www.php.net/manual/de/function.imagerotate.php#93692
745 | */
746 | protected function rotateImage($img, $rotation)
747 | {
748 | $width = imagesx($img);
749 | $height = imagesy($img);
750 | switch($rotation) {
751 | case 90: $newimg= @imagecreatetruecolor($height , $width );break;
752 | case 180: $newimg= @imagecreatetruecolor($width , $height );break;
753 | case 270: $newimg= @imagecreatetruecolor($height , $width );break;
754 | case 0: return $img;break;
755 | case 360: return $img;break;
756 | }
757 |
758 | if($newimg) {
759 | for($i = 0;$i < $width ; $i++) {
760 | for($j = 0;$j < $height ; $j++) {
761 | $reference = imagecolorat($img,$i,$j);
762 | switch($rotation) {
763 | case 90: if(!@imagesetpixel($newimg, ($height - 1) - $j, $i, $reference )){return false;}break;
764 | case 180: if(!@imagesetpixel($newimg, $width - $i, ($height - 1) - $j, $reference )){return false;}break;
765 | case 270: if(!@imagesetpixel($newimg, $j, $width - $i, $reference )){return false;}break;
766 | }
767 | }
768 | }
769 | return $newimg;
770 | }
771 | return false;
772 | }
773 | }
774 |
--------------------------------------------------------------------------------