-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththumbnailer.php
More file actions
293 lines (247 loc) · 9.77 KB
/
thumbnailer.php
File metadata and controls
293 lines (247 loc) · 9.77 KB
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
<?php
/*
Programmer : Shishir Raven
Purpose : Class to create thumbnail at runtime for Bigger images
How it works
a) Whenever we have to display thumbnail call a object as $a = new thumbnail('image.jpg','200',300,'images')
Parameter 1 - Image Name
Parameter 2 - Image width
Parameter 3 - Image height
Parameter 4 - Image foldoer
b) This code first checks the Thumbnail folder inside the Image folder to see if the thumbnail of that size if already available
If thumbnail is avialable it displays the same thumbail
If thumbail is not avialbale it creates one.
The name of the thumbail would be like image_200_300.jpg for dimensions 200 X 300
Note : Create a folder named thumb and give it wirte rights before you use the class.
*/
/* Example of how to use the class
include("../thumbnailer.php");
$config['image']="sample.jpg";
$config['folder']="images";
$config['width'] ="100";
$config['height']="300";
$config['compression']="100";
$config['fit_to_box']=true;
$config['fit_to_scale']=true;
$thumb1 = new thumbnailer($config);
echo "<img src='".$thumb1->create_thumb()."'/>";
*/
Class thumbnailer
{
var $image="";
var $folder="";
var $width="";
var $height="";
var $compression="100";
var $new_image_type="";
var $new_image="";
var $old_image_width="";
var $old_image_height="";
var $fit_to_box=false;
var $fit_to_scale=false;
var $fit_aspect_ratio=false;
var $orignal_width="";
var $orignal_height="";
//Initializer function - We pass a array. Matches array keys with the class variable. if they match sets class vaiable value equal to the array key value.
function initializer($config= array())
{
if(count($config)>0) // Checking to see if the array is empty or not
{
foreach($config as $key=> $value) // Looping a config array extracting array key and its value into $key and $value
{
if(isset($this->$key)) // Is a Class variable there corresponing to the array key
{
$this->$key=$value; // Setting Class Variable to the array Key Value
}
}
}
$this->orignal_width=$this->width;
$this->orignal_height=$this->height;
}
// Constructor
function thumbnailer($config)
{
// running initializer with the configration array
$this->initializer($config);
// $imagepath =$this->folder."/".$this->image;
$imagepath =$this->folder."/".$this->image;
if(file_exists($imagepath) && !is_dir($imagepath)) {
$this->create_new_image();
$this->check_dimensions();
}
}
// The following function checks to see if the image thumnail is already build
function create_thumb()
{
if($this->fit_to_scale === true)
{
$thumbpath = $this->folder."/thumbs/fts_".$this->width."_".$this->height."_".$this->image;
}
else
{
$thumbpath = $this->folder."/thumbs/ftb_".$this->width."_".$this->height."_".$this->image;
}
if(!file_exists($thumbpath)) // Checking to see it the file already exists or not
{
$imagepath = $this->folder."/".$this->image;
if(file_exists($imagepath) && !is_dir($imagepath)) {
$this->resize();
$this->save($this->new_image_type);
}else{
return '';
}
}
return $thumbpath;
}
//Function to create a New image form the image path and store it into $new_image identifier
function create_new_image()
{
$blending = true;
$imagepath =$this->folder."/".$this->image;
$image_info = getimagesize($imagepath);
$this->new_image_type = $image_info[2];
if($this->new_image_type == IMAGETYPE_JPEG) {
$this->new_image = imagecreatefromjpeg($imagepath);
} elseif( $this->new_image_type == IMAGETYPE_GIF ) {
$this->new_image = imagecreatefromgif($imagepath);
} elseif( $this->new_image_type == IMAGETYPE_PNG ) {
$blending = false;
$this->new_image = imagecreatefrompng($imagepath);
}
// Finding width and height of current image
$this->old_image_width=imagesx($this->new_image);
$this->old_image_height=imagesy($this->new_image);
}
// The following function finds out new width or height according to aspect ratio
function check_dimensions()
{
if($this->width=="")
{
$ratio = $this->height / $this->old_image_height;
$this->width = round($this->old_image_width * $ratio);
}
if($this->height=="")
{
$ratio = $this->width / $this->old_image_width;
$this->height = round($this->old_image_height * $ratio);
}
}
// Resizing Thumbnail
function resize() {
$crop_x_offset = 0;
$crop_y_offset = 0;
if($this->fit_to_scale === true)
{
$source_image_path=$this->folder.$this->image;
$thumbnail_image_path=$this->folder."thumbs/".$this->width."_".$this->height."_".$this->image;
$source_image_width=$this->old_image_width ;
$source_image_height=$this->old_image_height;
$source_aspect_ratio = $source_image_width / $source_image_height;
$thumbnail_aspect_ratio = $this->width / $this->height;
if ($source_image_width <= $this->width && $source_image_height <= $this->height)
{
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
}
elseif ($thumbnail_aspect_ratio > $source_aspect_ratio)
{
$thumbnail_image_width = (int) ($this->height * $source_aspect_ratio);
$thumbnail_image_height = $this->height;
}
else
{
$thumbnail_image_width = $this->width;
$thumbnail_image_height = (int) ($this->width / $source_aspect_ratio);
}
if ($this->fit_aspect_ratio == true) {
$thumbnail_gd_image = imagecreatetruecolor($this->old_image_width, $this->old_image_height);
}
else{
$thumbnail_gd_image = imagecreatetruecolor($this->width, $this->height);
}
$dest_starting_x_corrdinate=round(($this->width-$thumbnail_image_width)/2);
$dest_starting_y_corrdinate=round(($this->height-$thumbnail_image_height)/2);
imagealphablending($thumbnail_gd_image, FALSE);
imagesavealpha($thumbnail_gd_image, TRUE);
$whiteBackground = imagecolorallocatealpha($thumbnail_gd_image,255, 255, 255, 127);
imagefill($thumbnail_gd_image,0,0,$whiteBackground);
if ($this->fit_aspect_ratio == true) {
imagecopyresampled($thumbnail_gd_image, $this->new_image, $dest_starting_x_corrdinate, $dest_starting_y_corrdinate, 0, 0, $this->old_image_width, $this->old_image_height, $source_image_width, $source_image_height);
}
else{
imagecopyresampled($thumbnail_gd_image, $this->new_image, $dest_starting_x_corrdinate, $dest_starting_y_corrdinate, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
}
$this->new_image = $thumbnail_gd_image;
}
else
{
if($this->fit_to_box === true)
{
// Fit to box code. // this code will be responisble for fitting the image to box.
$image_aspect = $this->old_image_width / $this->old_image_height;
$thumb_aspect = $this->width / $this->height;
if ($image_aspect > $thumb_aspect) {
$crop_height = $this->old_image_height;
$crop_width = round($crop_height * $thumb_aspect);
$crop_x_offset = round(($this->old_image_width - $crop_width) / 2);
} else {
$crop_width = $this->old_image_width;
$crop_height = round($crop_width / $thumb_aspect);
$crop_y_offset = round(($this->old_image_height - $crop_height) / 2);
}
// crop parameter
//$crop_size = $crop_width.'x'.$crop_height.'+'.$crop_x_offset.'+'.$crop_y_offset;
$this->old_image_width = $crop_width;
$this->old_image_height = $crop_height;
// echo $crop_width."<br/>";
// echo $crop_height."<br/>";
}
// code ends here
if ($this->fit_aspect_ratio == true) {
$new_image_final = imagecreatetruecolor($this->old_image_width, $this->old_image_height);
}
else{
$new_image_final = imagecreatetruecolor($this->width, $this->height);
}
// preserve transparency for PNG and GIF images
if ($this->new_image_type == 3 || $this->new_image_type == 1){
// allocate a color for thumbnail
$background_black = imagecolorallocate($new_image_final, 0, 0, 0);
// define a color as transparent
imagecolortransparent($new_image_final, $background_black);
// set the blending mode for thumbnail
imagealphablending($new_image_final, false);
// set the flag to save alpha channel
imagesavealpha($new_image_final, true);
}
if ($this->fit_aspect_ratio == true) {
imagecopyresampled($new_image_final, $this->new_image,0, 0, $crop_x_offset, $crop_y_offset, $this->old_image_width, $this->old_image_height, $this->old_image_width, $this->old_image_height);
}
else{
imagecopyresampled($new_image_final, $this->new_image,0, 0, $crop_x_offset, $crop_y_offset, $this->width, $this->height, $this->old_image_width, $this->old_image_height);
}
//imagecopyresampled($new_image, $this->new_image, 0,0,0, 0, $this->width, $this->height, $this->old_image_width, $this->old_image_height);
$this->new_image = $new_image_final;
}
}
// Saving Thumbail
function save($image_type)
{
if($this->fit_to_scale === true)
{
$filename =$this->folder."/thumbs/fts_".$this->orignal_width."_".$this->orignal_height."_".$this->image;
}
else
{
$filename =$this->folder."/thumbs/ftb_".$this->orignal_width."_".$this->orignal_height."_".$this->image;
}
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->new_image,$filename,$this->compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->new_image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->new_image,$filename);
}
}
}
?>