/web/htdocs/www.micro-la.com/home/open/docs/res/get_image.class.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
    
/**
    * GetImage Class, version: 1.0
    * Author - valiano - http://codecanyon.net/user/valiano
    * Release date: 23/11/2010
    */
    
    
function dmp($str){
        echo 
"<pre>";
        echo 
$str;
        echo 
"</pre>";
    }
    
    class 
GetImage{        
        const 
TYPE_EXACT "exact";
        const 
TYPE_EXACT_TOP "exacttop"
        
        private 
$dirImages "";
        private 
$dirCache "";
        private 
$pathErrorImage "";
        private 
$jpeg_quality 100;
        
        public function 
__construct(){
            
//validate GD library
            
$this->validateGD();
        }
        
        
/**
         * 
         * Show error message
         * @param $str error string
         */        
        
private function throwError($str){
            echo 
$str;
            exit();
        }
        
        
/**
         * 
         * Set jpeg quality
         * @param $folderCache
         */
        
public function setJpegQuality($quality){
            
$this->jpeg_quality $quality;
        }
        
        
        
/**
         * 
         * Set cache folder
         * @param $folderCache
         */
        
public function setCacheFolder($folderCache){
            
//add '/' sign at the end
            
if(!preg_match("/^.*\//"$folderCache)) $folderCache.="/";
            
$this->dirCache $folderCache;
        }
        
        
/**
         * 
         * Set images folder
         * @param $folderImages
         */
        
public function setImagesFolder($folderImages){
            
//add '/' sign at the end
            
if(!preg_match("/^.*\//"$folderImages)) $folderImages.="/";
            
$this->dirImages $folderImages;
        }
        
        
/**
         * 
         * Set path to (image not found) image
         * @param $pathErrorImage
         */
        
public function setErrorImagePath($pathErrorImage){
            
$this->pathErrorImage $pathErrorImage;
        }
        
        
/**
         * Validates that GD library exists. If not - print error
         */
        
private function validateGD(){
            if(
function_exists("gd_info") == false){
                echo 
"PHP GD library not found. Please enable it in php.ini";
                exit();
            }            
        }
        
        
/**
         * Validate if the type is "exact" and there is valid width and height.
         */
        
private function validateExact($type,$width,$height){
            if(
$type == self::TYPE_EXACT || $type == self::TYPE_EXACT_TOP){
                if(
$width <= || $height <= 0){
                    echo 
"For <b>'$type'</b> type you must specify both - the width and the height";
                    exit();
                }
            }
        }
        
        
/**
         * 
         * Validate that error image path exists
         */
        
private function validateErrorImagePath(){
            if(!
is_file($this->pathErrorImage)){
                echo 
"No 'error image' found by this path:".$this->pathErrorImage;
                exit();
            }                
        }
        
        
/**
         * get path info of certain path with all needed fields
         * @param $filepath
         */
        
private function getPathInfo($filepath){
            
$info pathinfo($filepath);
            
            
//fix the filename problem
            
if(!isset($info["filename"])){
                
$filename $info["basename"];
                if(isset(
$info["extension"]))
                    
$filename substr($info["basename"],0,(-strlen($info["extension"])-1));
                
$info["filename"] = $filename;
            }
                        
            return(
$info);
        }            
        
        
/**
         * 
         * Create thumbnail filename for saving.
         * @param string $filename
         * @param string $filetime - timestamp image update/create    
         * @param number $width
         * @param number $height
         * @param string $type
         */
        
private function getThumbFilename($filename,$filetime,$width,$height,$type=""){
            
$info pathInfo($filename);
            
$ext $info["extension"];
            
$name $info["filename"];
            
$width ceil($width);
            
$height ceil($height);
            
$thumbFilename $name."_".$width."x".$height;        
            if(
$type != ""$thumbFilename .= "_" $type;
            
$thumbFilename .= "_".$filetime;
            
$thumbFilename .= ".".$ext;
            return(
$thumbFilename);
        }
        
        
/**
         * 
         * Get thumbnail filepath
         * @param string $filename
         * @param string $filetime - timestamp image update/create
         * @param number $width
         * @param number $height
         * @param string $type
         */
        
private function getThumbFilepath($filename,$filetime,$width,$height,$type=""){
            
$filename $this->getThumbFilename($filename,$filetime,$width,$height,$type);
            
$filepath $this->dirCache.$filename;
            return(
$filepath);
        }
            
        
/**
         * 
         * Output image from filepath
         * @param string $filepath
         */
        
private function outputImage($filepath){
            
$info $this->getPathInfo($filepath);
            
$ext $info["extension"];                
            
$filetime filemtime($filepath);
            
            if(
strtolower($ext) == "jpg")
                
$ext "jpeg";
            
            
$numExpires 31536000;    //one year
            
$strExpires date('D, d M Y H:i:s',time()+$numExpires);
            
$strModified date('D, d M Y H:i:s',$filetime);
            
            
$contents file_get_contents($filepath);
            
$filesize strlen($contents);                
            
header("Last-Modified: $strModified GMT");
            
header("Expires: $strExpires GMT");
            
header("Cache-Control: public");
            
header("Content-Type: image/$ext");
            
header("Content-Length: $filesize");
            echo 
$contents;
            exit();
        }
        
        
/**
         * 
         * Output image with download headers from filepath
         * @param string $filepath
         * @param string $filename - the new filename
         * @param string $mimeType - the mime type of the file
         */
        
private function outputImageForDownload($filepath,$filename,$mimeType=""){        
            
$contents file_get_contents($filepath);
            
$filesize strlen($contents);
            
            if(
$mimeType == ""){
                
$info $this->getPathInfo($filepath);
                
$ext $info["extension"];
                
$mimeType "image/$ext";
            }
            
            
header("Content-Type: $mimeType");    
            
header("Content-Disposition: attachment; filename=\"$filename\"");
            
header("Content-Length: $filesize");        
            echo 
$contents;
            exit();
        }
        
        
/**
         * 
         * Download image from images folder
         * @param string $filename
         */
        
private function downloadImage($filename){
            
$filepath $this->dirImages."/".$filename;
            if(!
is_file($filepath)) {
                echo 
"file doesn't exists";
                exit();
            }
            
$this->outputImageForDownload($filepath,$filename);
        }
        
        
/**
         * 
         * get src image from filepath according the image type
         * @param string $filepath
         * @param string $type
         */
        
private function getGdSrcImage($filepath,$type){
            
// create the image
            
$src_img false;
            switch(
$type){
                case 
IMAGETYPE_JPEG:
                    
$src_img = @imagecreatefromjpeg($filepath);
                break;
                case 
IMAGETYPE_PNG:
                    
$src_img = @imagecreatefrompng($filepath);
                break;
                case 
IMAGETYPE_GIF:
                    
$src_img = @imagecreatefromgif($filepath);
                break;
                case 
IMAGETYPE_WBMP:
                    
$src_img = @imagecreatefromwbmp($filepath);
                break;
                default:
                    
$this->throwError("wrong image format, can't resize");
                break;
            }
            
            if(
$src_img == false$this->throwError("Can't resize image: $filepath");
            return(array(
"success"=>true,"image"=>$src_img));
        }
        
        
/**
         * 
         * save gd image to some filepath. return if success or not
         * @param img $dst_img
         * @param string $filepath
         * @param string $type
         */
        
private function saveGdImage($dst_img,$filepath,$type){
            
$successSaving false;
            switch(
$type){
                case 
IMAGETYPE_JPEG:
                    
$successSaving imagejpeg($dst_img,$filepath,$this->jpeg_quality);
                break;
                case 
IMAGETYPE_PNG:
                    
$successSaving imagepng($dst_img,$filepath);
                break;
                case 
IMAGETYPE_GIF:
                    
$successSaving imagegif($dst_img,$filepath);
                break;
                case 
IMAGETYPE_WBMP:
                    
$successSaving imagewbmp($dst_img,$filepath);
                break;
            }
            
            return(
$successSaving);
        }
        
        
/**
         * 
         * crop image to specifix height and width , and save it to new path
         * @param string $filepath
         * @param number $cropWidth
         * @param number $cropHeight
         * @param string $filepathNew
         * @param string $type
         */
        
private function cropImageSaveNew($filepath,$cropWidth,$cropHeight,$filepathNew,$type){
            
            
$imgInfo getimagesize($filepath);
            
$imgType $imgInfo[2];
            
            
$response $this->getGdSrcImage($filepath,$imgType);
            if(
$response["success"] == false) return($response);
            
            
$src_img $response["image"];        
            
            
$width imageSX($src_img);
            
$height imageSY($src_img);
            
            
//crop the image from the top
            
$startx 0;
            
$starty 0;
            
            
//find precrop width and height:
            
$percent $cropWidth $width;
            
$newWidth $cropWidth;
            
$newHeight ceil($percent*$height);
            
            if(
$type == self::TYPE_EXACT){     //crop the image from the middle
                //$startx = ($width-$cropWidth)/2;
                
$startx 0;
                
$starty = ($newHeight-$cropHeight)/$percent;
            }
            
            if(
$newHeight $cropHeight){    //by width
                
$percent $cropHeight $height;
                
$newHeight $cropHeight;
                
$newWidth ceil($percent*$width);
                
                if(
$type == self::TYPE_EXACT){     //crop the image from the middle
                    
$startx = ($newWidth $cropWidth)/$percent;    //the startx is related to big image
                    
$starty 0;
                }
            }
            
            
//resize the picture:
            
$tmp_img ImageCreateTrueColor($newWidth,$newHeight);
            
            
$this->handleTransparency($tmp_img,$imgType,$newWidth,$newHeight);
            
            
imagecopyresampled($tmp_img,$src_img,0,0,$startx,$starty,$newWidth,$newHeight,$width,$height);
            
            
//crop the picture:
            
$dst_img ImageCreateTrueColor($cropWidth,$cropHeight);
            
            
$this->handleTransparency($dst_img,$imgType,$cropWidth,$cropHeight);
            
            
imagecopy($dst_img$tmp_img0000$newWidth$newHeight);
            
            
//save the picture
            
$this->saveGdImage($dst_img,$filepathNew,$imgType);
            
            
imagedestroy($dst_img);
            
imagedestroy($src_img);
            
imagedestroy($tmp_img);
            
            return(array(
"success"=>true));        
        }
        
        
/**
         * 
         * if the images are png or gif - handle image transparency
         * @param img $dst_img
         * @param string $imgType
         * @param number $newWidth
         * @param number $newHeight
         */
        
private function handleTransparency(&$dst_img,$imgType,$newWidth,$newHeight){
            
//handle transparency:
            
if($imgType == IMAGETYPE_PNG || $imgType == IMAGETYPE_GIF){
              
imagealphablending($dst_imgfalse);
              
imagesavealpha($dst_img,true);
              
$transparent imagecolorallocatealpha($dst_img255,  255255127);
              
imagefilledrectangle($dst_img0,  0$newWidth$newHeight,  $transparent);
            }
        }
        
        
/**
         * 
         * resize image and save it to new path
         * @param string $filepath
         * @param number $maxWidth
         * @param number $maxHeight
         * @param string $filepathNew
         */
        
private function resizeImageSaveNew($filepath,$maxWidth,$maxHeight,$filepathNew){
            
            
$imgInfo getimagesize($filepath);
            
$imgType $imgInfo[2];
            
            
$response $this->getGdSrcImage($filepath,$imgType);
            if(
$response["success"] == false) return($response);
            
            
$src_img $response["image"];                
             
            
$width imageSX($src_img);
            
$height imageSY($src_img);
            
            
$newWidth $width;
            
$newHeight $height;
    
            
            
//find new width
            
if($height $maxHeight){
                
$procent $maxHeight $height;
                
$newWidth ceil($width $procent);
                
$newHeight $maxHeight;
            }
            
            
//if the new width is grater than max width, find new height, and remain the width.
            
if($newWidth $maxWidth){
                
$procent $maxWidth $newWidth;
                
$newHeight ceil($newHeight $procent);
                
$newWidth $maxWidth;
            }
            
            
//if the image don't need to be resized, just copy it from source to destanation.
            
if($newWidth == $width && $newHeight == $height){
                
$success copy($filepath,$filepathNew);
                if(
$success == false$this->throwError("can't copy the image from one path to another");
            }
            else{        
//else create the resized image, and save it to new path:
                
$dst_img=ImageCreateTrueColor($newWidth,$newHeight);            
                
                
$this->handleTransparency($dst_img,$imgType,$newWidth,$newHeight);
                
                
//copy the new resampled image:
                
imagecopyresampled($dst_img,$src_img,0,0,0,0,$newWidth,$newHeight,$width,$height);
                
                
$this->saveGdImage($dst_img,$filepathNew,$imgType);
                
imagedestroy($dst_img);
            }
            
            
imagedestroy($src_img);
            
$result = array();
            
$result["success"] = true;
            return(
$result);
        }
        
        
        
/**
         * 
         * Show image from the images path, with saving to cache
         * @param string $filename
         * @param number $maxWidth
         * @param number $maxHeight
         * @param string $type
         */
        
public function showImage($filename,$maxWidth=-1,$maxHeight=-1,$type=""){
            
$this->validateErrorImagePath();
            
$this->validateExact($type,$maxWidth,$maxHeight);
            
            
$filepath $this->dirImages.$filename;
            
            if(!
is_file($filepath)) $filepath $this->pathErrorImage;
            
            
//get image filetime
            
$filetime filemtime($filepath);
                        
            if(
is_numeric($maxWidth) == false || is_numeric($maxHeight) == false$this->outputImage($filepath);
            if(
$maxWidth <= && $maxHeight <= 0$this->outputImage($filepath);
            
            if(
$maxWidth <= 0$maxWidth 1000000;
            if(
$maxHeight <= 0$maxHeight 100000;
            
            
$filepathNew $this->getThumbFilepath($filename,$filetime,$maxWidth,$maxHeight,$type);
            
            if(
$type == self::TYPE_EXACT || $type == self::TYPE_EXACT_TOP)
                
$response $this->cropImageSaveNew($filepath,$maxWidth,$maxHeight,$filepathNew,$type);
            else 
                
$response $this->resizeImageSaveNew($filepath,$maxWidth,$maxHeight,$filepathNew);
            
            if(
$response["success"] == false$this->outputImage($filepath);
            if(
is_file($filepathNew)) $this->outputImage($filepathNew);
            else 
$this->outputImage($filepath);
            exit();
        }
    }
?>