-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageresize.class
More file actions
289 lines (239 loc) · 10.4 KB
/
Copy pathimageresize.class
File metadata and controls
289 lines (239 loc) · 10.4 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
<?php
// XXX: Make CalculateSize a stub to be implemented in subclasses, for limitless possibilities
class ImageResize {
private $Width = NULL;
private $Height = NULL;
private $MaxWidth = NULL;
private $MaxHeight = NULL;
private $WorkingWidth = NULL;
private $WorkingHeight = NULL;
// Quality of output image, as a percentage, 0-100
public $Quality = 90;
// Applies to $Width, $Height
public $KeepAspectRatio = true;
// Scale image to $Width/$Height, keeping aspect ratio, cropping excess
public $AutoCrop = false;
const IMAGETYPE_JPEG = IMAGETYPE_JPEG;
const IMAGETYPE_PNG = IMAGETYPE_PNG;
const IMAGETYPE_GD = 31337;
public $OutputFormat = ImageResize::IMAGETYPE_JPEG;
public function __construct($width=NULL, $height=NULL) {
if ($width != NULL)
$this->SetWidth($width);
if ($height != NULL)
$this->SetHeight($height);
}
public function SetWidth($width) {
if (is_numeric($width))
$this->Width = (int)$width;
else
throw new Exception('Destination image width must be numeric.');
}
public function SetHeight($height) {
if (is_numeric($height))
$this->Height = (int)$height;
else
throw new Exception('Destination height must be numeric');
}
public function SetMaxWidth($width) {
if (is_numeric($width))
$this->MaxWidth = (int)$width;
else
throw new Exception('Destination image width must be numeric.');
}
public function SetMaxHeight($height) {
if (is_numeric($height))
$this->MaxHeight = (int)$height;
else
throw new Exception('Destination height must be numeric');
}
public function SetDimensions($width, $height) {
$this->SetWidth($width);
$this->SetHeight($height);
}
public function SetMaxDimensions($width, $height) {
$this->SetMaxWidth($width);
$this->SetMaxHeight($height);
}
public function SetQuality($quality) {
if (is_numeric($quality)) {
$quality = (int)$quality;
if ($quality <= 0)
throw new Exception('Quality of destination image may not be less than 0');
else if ($quality >= 100)
throw new Exception('Quality of destination image may not be greater than 100');
else
$this->Quality = $quality;
} else {
throw new Exception('Thumbnail quality level must be numeric');
}
}
public function KeepAspectRatio($bool_ar) {
if ($bool_ar == true || $bool_ar == false)
$this->KeepAspectRatio = $bool_ar;
else
throw new Exception('Argument must be boolean');
}
public function AutoCrop($bool_ac) {
if ($bool_ac == true || $bool_ac == false)
$this->AutoCrop = $bool_ac;
else
throw new Exception('Argument must be boolean');
}
public function LoadFromFile($image_path) {
if (!file_exists($image_path))
throw new Exception('Image file does not exist');
$image_data = file_get_contents($image_path);
if ($image_data == false)
throw new Exception('Could not load image from file');
$gd_image_resource = imagecreatefromstring($image_data);
if ($gd_image_resource == false)
throw new Exception('Could not create image from data contained in file, ' .
'it is possible that the image type is unsupported, ' .
'the data is not in a recognised format, or the image is corrupt');
return $gd_image_resource;
}
public function LoadFromString($image_data) {
$gd_image_resource = imagecreatefromstring($image_data);
if ($gd_image_resource == false)
throw new Exception('Could not create image from supplied data, it is possible that ' .
'the image type is unsupported, the data is not in a recognised ' .
'format, or the image is corrupt');
return $gd_image_resource;
}
public function OutputFile($image_data, $filename) {
if (!file_put_contents($filename, $image_data)) {
throw new exception('Could not write file');
}
}
public function FreeResource($gd_image_resource) {
imagedestroy($gd_image_resource);
}
public function Resize($gd_image_resource) {
$orig_width = imagesx($gd_image_resource);
$orig_height = imagesy($gd_image_resource);
$this->WorkingWidth = NULL;
$this->WorkingHeight = NULL;
$icr_result = true;
$gd_output_res = NULL;
if ($this->AutoCrop == true) {
$ac_size_arr = $this->CalculateFixedSize($orig_width, $orig_height);
$gd_output_res = imagecreatetruecolor($this->Width, $this->Height);
$icr_result = imagecopyresampled($gd_output_res, $gd_image_resource, 0, 0,
$ac_size_arr[0], $ac_size_arr[1],
$this->Width, $this->Height,
$ac_size_arr[2], $ac_size_arr[3]);
} else {
$this->CalculateRelativeSize($orig_width, $orig_height);
$gd_output_res = imagecreatetruecolor($this->WorkingWidth, $this->WorkingHeight);
$icr_result = imagecopyresampled($gd_output_res, $gd_image_resource, 0, 0, 0, 0,
$this->WorkingWidth, $this->WorkingHeight,
$orig_width, $orig_height);
}
if (!$icr_result)
throw new Exception('imagecopyresampled failed, XXX: Fixme');
return $this->GetImageData($gd_output_res);
if (!$status)
throw new Exception('Could not build output image, imagejpeg failure');
return $image_data;
}
public function GetImageData($gd_output_res) {
ob_start();
if ($this->OutputFormat == ImageResize::IMAGETYPE_JPEG) {
$status = imagejpeg($gd_output_res, '', $this->Quality);
}
elseif ($this->OutputFormat == ImageResize::IMAGETYPE_PNG) {
$status = imagepng($gd_output_res, '');
ex_debug('Quality settings not honored for PNG thumbnails', 3);
}
elseif ($this->OutputFormat == ImageResize::IMAGETYPE_GD) {
ob_end_clean();
return $gd_output_res;
} else {
ob_end_clean();
throw new Exception('Unrecognized or Invalid output format');
}
$image_data = ob_get_contents();
ob_end_clean();
return $image_data;
}
private function CalculateFixedSize($orig_width, $orig_height) {
if ($orig_width >= $this->Width && $orig_height >= $this->Height) {
if ($orig_height < $orig_width) {
$ratio = (double)($orig_height / $this->Height);
$copy_width = round($this->Width * $ratio);
if ($copy_width > $orig_width) {
$ratio = (double)($orig_width / $this->Width);
$copy_width = $orig_width;
$copy_height = round($this->Height * $ratio);
$x_offset = 0;
$y_offset = round(($orig_height - $copy_height) / 2);
} else {
$copy_height = $orig_height;
$x_offset = round(($orig_width - $copy_width) / 2);
$y_offset = 0;
}
} else {
$ratio = (double)($orig_width / $this->Width);
$copy_height = round($this->Height * $ratio);
if ($copy_height > $orig_height) {
$ratio = (double)($orig_height / $this->Height);
$copy_height = $orig_height;
$copy_width = round($this->Width * $ratio);
$x_offset = round(($orig_width - $copy_width) / 2);
$y_offset = 0;
} else {
$copy_width = $orig_width;
$x_offset = 0;
$y_offset = round(($orig_height - $copy_height) / 2);
}
}
return array($x_offset, $y_offset, $copy_width, $copy_height);
} else {
throw new Exception('Target has x or y dimensions smaller than original');
}
}
private function CalculateRelativeSize($orig_width, $orig_height) {
if ($this->WorkingWidth == NULL)
if ($this->Width == NULL)
$this->WorkingWidth = $this->MaxWidth;
else
$this->WorkingWidth = $this->Width;
if ($this->WorkingHeight == NULL)
if ($this->Height == NULL)
$this->WorkingHeight = $this->MaxHeight;
else
$this->WorkingHeight = $this->Height;
if ($this->WorkingWidth == NULL && $this->WorkingHeight == NULL)
throw new Exception('Unable to scale image without a target width or height');
if ($this->KeepAspectRatio == true) {
if ($this->WorkingWidth == NULL)
$this->WorkingWidth = (int)$this->WorkingHeight/($orig_height/$orig_width);
elseif ($this->WorkingHeight == NULL)
$this->WorkingHeight = (int)$this->WorkingWidth/($orig_width/$orig_height);
else {
$width_ratio = $this->WorkingWidth/$orig_width;
$height_ratio = $this->WorkingHeight/$orig_height;
if ($width_ratio > $height_ratio)
$this->WorkingWidth = (int)$this->WorkingHeight/($orig_height/$orig_width);
elseif ($height_ratio > $width_ratio)
$this->WorkingHeight = (int)$this->WorkingWidth/($orig_width/$orig_height);
}
} else {
if ($this->WorkingWidth == NULL)
$this->WorkingWidth = $this->WorkingHeight;
elseif ($this->WorkingHeight == NULL)
$this->WorkingHeight == $this->WorkingWidth;
}
/*
if ($this->MaxWidth > $this->WorkingWidth) {
$this->WorkingWidth = $this->MaxWidth;
$this->CalculateRelativeSize($orig_width, $orig_height);
} elseif ($this->MaxHeight > $this->WorkingHeight) {
$this->WorkingHeight = $this->MaxHeight;
$this->CalculateRelativeSize($orig_width, $orig_height);
}
*/
}
}
?>