-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThumbnailImage.php
129 lines (127 loc) · 4.06 KB
/
ThumbnailImage.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
<?php
//requires GD 2.0.1 or higher
//note about gif
class ThumbnailImage{
private $image;
//not applicable to gif or png
private $quality = 100;
private $mimetype;
private $imageproperties;
private $initialfilesize;
////////////////////////////////////////////////////////
//constructor
////////////////////////////////////////////////////////
public function __construct($file, $thumbnailsize = 100){
//check path
is_file($file) or die ("File: $file doesn't exist.");
$this->initialfilesize = filesize($file);
$this->imageproperties = getimagesize($file) or die ("Incorrect file type.");
// new function image_type_to_mime_type
$this->mimetype = image_type_to_mime_type($this->imageproperties[2]);
//create image
switch($this->imageproperties[2]){
case IMAGETYPE_JPEG:
// echo("JPEG<br/>");
$this->image = imagecreatefromJPEG($file);
break;
case IMAGETYPE_GIF:
// echo("GIF<br/>");
$this->image = imagecreatefromgif($file);
break;
case IMAGETYPE_PNG:
// echo("PNG<br/>");
$this->image = imagecreatefromPNG($file);
break;
default:
die("Couldn't create image.");
}
$this->createThumb($thumbnailsize);
}
////////////////////////////////////////////////////////
//destructor
////////////////////////////////////////////////////////
public function __destruct(){
if(isset($this->image)){
imagedestroy($this->image);
}
}
////////////////////////////////////////////////////////
//public methods
////////////////////////////////////////////////////////
public function getImage(){
header("Content-type: $this->mimetype");
switch($this->imageproperties[2]){
case IMAGETYPE_JPEG:
imagejpeg($this->image,"",$this->quality);
break;
case IMAGETYPE_GIF:
imagegif($this->image);
break;
case IMAGETYPE_PNG:
imagepng($this->image);
break;
default:
die("Couldn't create image.");
}
}
////////////////////////////////////////////////////////
public function getMimeType(){
return $this->mimetype;
}
////////////////////////////////////////////////////////
public function getQuality(){
$quality = null;
if($this->imageproperties[2] == IMAGETYPE_JPEG){
$quality = $this->quality;
}
return $quality;
}
////////////////////////////////////////////////////////
public function setQuality($quality){
if($quality > 100 || $quality < 1){
$quality = 75;
}
if($this->imageproperties[2] == IMAGETYPE_JPEG){
$this->quality = $quality;
}
}
////////////////////////////////////////////////////////
public function getInitialFileSize(){
return $this->initialfilesize;
}
////////////////////////////////////////////////////////
//private methods
////////////////////////////////////////////////////////
private function createThumb($thumbnailsize){
//array elements
$srcW = $this->imageproperties[0];
$srcH = $this->imageproperties[1];
//only adjust if larger than reduction size
if($srcW >$thumbnailsize || $srcH > $thumbnailsize){
$reduction = $this->calculateReduction($thumbnailsize);
//get proportions
$desW = $srcW/$reduction;
$desH = $srcH/$reduction;
$copy = imagecreatetruecolor($desW, $desH);
imagecopyresampled($copy,$this->image,0,0,0,0,$desW, $desH, $srcW, $srcH)
or die ("Image copy failed.");
//destroy original
imagedestroy($this->image);
$this->image = $copy;
}
}
////////////////////////////////////////////////////////
private function calculateReduction($thumbnailsize){
//adjust
$srcW = $this->imageproperties[0];
$srcH = $this->imageproperties[1];
if($srcW < $srcH){
$reduction = round($srcH/$thumbnailsize);
}else{
$reduction = round($srcW/$thumbnailsize);
}
return $reduction;
}
}//end class
////////////////////////////////////////////////////////
?>