forked from mdlama/com_partners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimghandler.php
417 lines (362 loc) · 9.91 KB
/
imghandler.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
<?php
/**
* HUBzero CMS
*
* Copyright 2005-2015 HUBzero Foundation, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* HUBzero is a registered trademark of Purdue University.
*
* @package hubzero-cms
* @author Shawn Rice <zooley@purdue.edu>
* @copyright Copyright 2005-2015 HUBzero Foundation, LLC.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Components\Partners\Helpers;
use Hubzero\Base\Object;
use Filesystem;
use Lang;
/**
* Image manipulation class
*/
class ImgHandler extends Object
{
/**
* Description for 'path'
*
* @var unknown
*/
var $path = NULL;
/**
* Description for 'image'
*
* @var unknown
*/
var $image = NULL;
/**
* Description for 'maxWidth'
*
* @var integer
*/
var $maxWidth = 186;
/**
* Description for 'maxHeight'
*
* @var integer
*/
var $maxHeight = 186;
/**
* Description for 'cropratio'
*
* @var unknown
*/
var $cropratio = NULL;
/**
* Description for 'quality'
*
* @var integer
*/
var $quality = 90;
/**
* Description for 'color'
*
* @var boolean
*/
var $color = false;
/**
* Description for 'overwrite'
*
* @var boolean
*/
var $overwrite = true;
/**
* Description for 'outputName'
*
* @var unknown
*/
var $outputName = NULL;
/**
* Description for '_MEMORY_TO_ALLOCATE'
*
* @var string
*/
var $_MEMORY_TO_ALLOCATE = '100M';
/**
* Process an image
*
* @return boolean True if no errors
*/
public function process()
{
$docRoot = $this->path;
$image = $this->image;
$cropratio = $this->cropratio;
$quality = $this->quality;
$color = $this->color;
// Make sure that the requested file is actually an image
if (!$image)
{
$this->setError(Lang::txt('No image set.'));
return false;
}
// Make sure that the requested file is actually an image
if (!$docRoot)
{
$this->setError(Lang::txt('No image path set.'));
return false;
}
// Strip the possible trailing slash off the document root
//$docRoot = preg_replace('/\/$/', '', $docRoot);
if (!is_file($docRoot . $image))
{
$this->setError(Lang::txt('File/path not found.'));
return false;
}
// Get the size and MIME type of the requested image
$size = GetImageSize($docRoot . $image);
$mime = $size['mime'];
// Make sure that the requested file is actually an image
if (substr($mime, 0, 6) != 'image/')
{
$this->setError(Lang::txt('File is not an image.'));
return false;
}
$width = $size[0];
$height = $size[1];
$maxWidth = $this->maxWidth;
$maxHeight = $this->maxHeight;
if ($maxWidth >= $width && $maxHeight >= $height)
{
return true;
}
if ($color)
{
$color = preg_replace('/[^0-9a-fA-F]/', '', (string) $color);
}
else
{
$color = FALSE;
}
// Ratio cropping
$offsetX = 0;
$offsetY = 0;
if ($cropratio)
{
$cropRatio = explode(':', (string) $cropratio);
if (count($cropRatio) == 2)
{
$ratioComputed = $width / $height;
$cropRatioComputed = (float) $cropRatio[0] / (float) $cropRatio[1];
if ($ratioComputed < $cropRatioComputed)
{
// Image is too tall so we will crop the top and bottom
$origHeight = $height;
$height = $width / $cropRatioComputed;
$offsetY = ($origHeight - $height) / 2;
}
else if ($ratioComputed > $cropRatioComputed)
{
// Image is too wide so we will crop off the left and right sides
$origWidth = $width;
$width = $height * $cropRatioComputed;
$offsetX = ($origWidth - $width) / 2;
}
}
}
// Setting up the ratios needed for resizing. We will compare these below to determine how to
// resize the image (based on height or based on width)
$xRatio = $maxWidth / $width;
$yRatio = $maxHeight / $height;
if ($xRatio * $height < $maxHeight)
{
// Resize the image based on width
$tnHeight = ceil($xRatio * $height);
$tnWidth = $maxWidth;
}
else
{
// Resize the image based on height
$tnWidth = ceil($yRatio * $width);
$tnHeight = $maxHeight;
}
// Before we actually do any crazy resizing of the image, we want to make sure that we
// haven't already done this one at these dimensions. To the cache!
// Note, cache must be world-readable
// We store our cached image filenames as a hash of the dimensions and the original filename
$resizedImageSource = $tnWidth . 'x' . $tnHeight . 'x' . $quality;
if ($cropratio)
{
$resizedImageSource .= 'x' . (string) $cropratio;
}
$resizedImageSource .= '-' . $image;
$resizedImage = $resizedImageSource; //md5($resizedImageSource);
$resized = $docRoot . $resizedImage;
// We don't want to run out of memory
ini_set('memory_limit', $this->_MEMORY_TO_ALLOCATE);
// Set up a blank canvas for our resized image (destination)
$dst = imagecreatetruecolor($tnWidth, $tnHeight);
// Set up the appropriate image handling functions based on the original image's mime type
switch ($size['mime'])
{
case 'image/gif':
// We will be converting GIFs to PNGs to avoid transparency issues when resizing GIFs
// This is maybe not the ideal solution, but IE6 can suck it
$creationFunction = 'ImageCreateFromGif';
$outputFunction = 'ImagePng';
$mime = 'image/png'; // We need to convert GIFs to PNGs
$doSharpen = FALSE;
$quality = round(10 - ($quality / 10)); // We are converting the GIF to a PNG and PNG needs a compression level of 0 (no compression) through 9
break;
case 'image/x-png':
case 'image/png':
$creationFunction = 'ImageCreateFromPng';
$outputFunction = 'ImagePng';
$doSharpen = FALSE;
$quality = round(10 - ($quality / 10)); // PNG needs a compression level of 0 (no compression) through 9
break;
default:
$creationFunction = 'ImageCreateFromJpeg';
$outputFunction = 'ImageJpeg';
$doSharpen = TRUE;
break;
}
// Read in the original image
$src = $creationFunction($docRoot . $image);
if (in_array($size['mime'], array('image/gif', 'image/png')))
{
if (!$color)
{
// If this is a GIF or a PNG, we need to set up transparency
imagealphablending($dst, false);
imagesavealpha($dst, true);
}
else
{
// Fill the background with the specified color for matting purposes
if ($color[0] == '#')
{
$color = substr($color, 1);
}
$background = FALSE;
if (strlen($color) == 6)
{
$background = imagecolorallocate($dst, hexdec($color[0].$color[1]), hexdec($color[2].$color[3]), hexdec($color[4].$color[5]));
}
else if (strlen($color) == 3)
{
$background = imagecolorallocate($dst, hexdec($color[0].$color[0]), hexdec($color[1].$color[1]), hexdec($color[2].$color[2]));
}
if ($background)
{
imagefill($dst, 0, 0, $background);
}
}
}
// Resample the original image into the resized canvas we set up earlier
ImageCopyResampled($dst, $src, 0, 0, $offsetX, $offsetY, $tnWidth, $tnHeight, $width, $height);
if ($doSharpen)
{
// Sharpen the image based on two things:
// (1) the difference between the original size and the final size
// (2) the final size
$sharpness = $this->findSharp($width, $tnWidth);
$sharpenMatrix = array(
array(-1, -2, -1),
array(-2, $sharpness + 12, -2),
array(-1, -2, -1)
);
$divisor = $sharpness;
$offset = 0;
if (function_exists('imageconvolution'))
{
imageconvolution($dst, $sharpenMatrix, $divisor, $offset);
}
}
// Write the resized image to the cache
$outputFunction($dst, $resized, $quality);
// Yes - remove it
$overwrite = $this->overwrite;
if ($overwrite)
{
$outputName = $this->outputName;
if ($outputName)
{
$image = $outputName;
}
if (file_exists($resized))
{
if (file_exists($docRoot . $image))
{
if (!Filesystem::delete($docRoot . $image))
{
$this->setError(Lang::txt('UNABLE_TO_DELETE_FILE'));
return false;
}
}
if (!Filesystem::move($resized, $docRoot . $image))
{
$this->setError(Lang::txt('UNABLE_TO_DELETE_FILE'));
return false;
}
}
}
return true;
}
/**
* Create a thumbnail name
*
* @param string $image Image name
* @param string $tn Thumbnail prefix
* @return string
*/
public function createThumbName($image=null, $tn='_thumb')
{
if (!$image)
{
$image = $this->image;
}
if (!$image)
{
$this->setError(Lang::txt('No image set.'));
return false;
}
$ext = Filesystem::extension($image);
return Filesystem::name($image) . $tn . '.' . $ext;
}
/**
* Sharpen the image based on two things:
*
* (1) the difference between the original size and the final size
* (2) the final size
*
* @param number $orig Original size
* @param number $final Final size
* @return integer
*/
private function findSharp($orig, $final)
{
$final = $final * (750.0 / $orig);
$a = 52;
$b = -0.27810650887573124;
$c = .00047337278106508946;
$result = $a + $b * $final + $c * $final * $final;
return max(round($result), 0);
}
}