-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThumbGenerator.class.php
495 lines (458 loc) · 16 KB
/
ThumbGenerator.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
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
use ErrorException as ErrorException;
/**
* ThumbGenerator generate thumbnail from images into working directory
*
* Class offers method to creates image thumbnail, starting from class level folder or given folder.
* Default settings allow to scan children folder of working directory. Can be disabled.
* Image thumbnail will retain original angle ( if image has metadata with relavant informations ).
* Process return logs via proper method or dump whole process into a log file.
* Resize factor default value is 50%. Values can be changed with proper method, working with same ratio
* or different ratios. On generated thumbnail a watermark can be added using proper methods and a watermark file
*
* @requires GD library, PHP 7
* @author Marco Grossi <developer@bigm.it>
* @license GPL-3.0 License
* @version 1.0
*/
class ThumbGenerator
{
// Directories
protected $workingDir = '';
protected $thumbnailDir = '';
protected $searchNested = true;
// Watermark
protected $watermark;
protected $watermarkResource;
// Image resize percentage
protected $widthReduction = 50;
protected $heightReduction = 50;
// Files processed
protected $thumbfiles = [];
protected $cloneFile = [];
// Logs
protected $log = [];
protected $enableDump = false;
const LOG_DIR = __DIR__.DIRECTORY_SEPARATOR;
public function __construct()
{
$this->workingDir = __DIR__.DIRECTORY_SEPARATOR;
$this->thumbnailDir = $this->workingDir.'thumbs'.DIRECTORY_SEPARATOR;
}
/**
* Set working directory where images are read as source
*
* @param string $workingDir Directory path
* @return void
*/
public function setWorkingDir(string $workingDir) : void
{
$workingDir = rtrim($workingDir, DIRECTORY_SEPARATOR);
$this->workingDir = !empty($workingDir) ? $workingDir.DIRECTORY_SEPARATOR : $this->workingDir;
}
/**
* Set thumbnail directory where thumbs are stored
*
* @param string $thumbnailDir Directory path
* @return void
*/
public function setThumbnailDir(string $thumbnailDir) : void
{
$thumbnailDir = rtrim($thumbnailDir, DIRECTORY_SEPARATOR);
$this->thumbnailDir = !empty($thumbnailDir) ? $thumbnailDir.DIRECTORY_SEPARATOR : $this->thumbnailDir;
}
/**
* Disable search into child directories
*
* @return void
*/
public function disableRecursiveSearch() : void
{
$this->searchNested = false;
}
/**
* Return processing log
*
* @return array
*/
public function getLog() : array
{
return $this->log;
}
/**
* Create processing log dump
* Log can grow up easily, use this feature only for debug or for testing
*
* @return void
*/
public function enableLogDump() : void
{
$this->enableDump = true;
}
/**
* Verify permission and directories existence
*
* @return void
*/
public function checkDirectory() : void
{
// Verify working directory existence
if (!is_dir($this->workingDir)) {
// Try to create if not exists
@mkdir($this->workingDir, 0755, true);
if (!is_dir($this->workingDir)) {
throw new ErrorException('ThumbGenerator cannot create directory '.$this->workingDir);
}
}
// Verify thumbnai directory existence
if (!is_dir($this->thumbnailDir)) {
// Try to create if not exists
@mkdir($this->thumbnailDir, 0755, true);
if (!is_dir($this->thumbnailDir)) {
throw new ErrorException('ThumbGenerator cannot create directory '.$this->thumbnailDir);
}
}
}
/**
* Set image resize factor, width and height
*
* @param integer $width Thumb width reduction percentage
* @param integer $height Thumb height reduction percentage
* @return void
*/
public function setResizeFactor(int $width, int $height = null) : void
{
if (!empty($width) || is_numeric($width)) {
$this->widthReduction = $width;
$this->heightReduction = empty($height) || !is_numeric($height) ? $width : $height;
}
}
/**
* Specify file to be used as watermark on image thumbnail
*
* @param string $pathToFile Path to file
* @return void
*/
public function addWatermarkFile(string $pathToFile) : void
{
if (!empty($pathToFile) && file_exists($pathToFile)) {
$this->watermark = $pathToFile;
$this->watermarkResource = $this->getResourceFromFile($this->watermark);
}
}
/**
* Add watermark to file
*
* @param string $pathToFile Path to file
* @param integer $marginRight Watermark right point
* @param integer $marginBottom Watermark bottom point
* @return void
*/
private function addWatermark(string $pathToFile, int $marginRight = null, int $marginBottom = null) : void
{
if (!empty($this->watermark)) {
// Load the stamp and the photo to apply the watermark to
$image = $this->getResourceFromFile($pathToFile);
if ($image && $this->watermarkResource) {
$fileProps = @getimagesize($pathToFile);
// Images dimensions
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
$watermarkWidth = imagesx($this->watermarkResource);
$watermarkHeight = imagesy($this->watermarkResource);
//Watermark positioning
$marginRight = $marginRight ? $marginRight : $imageWidth/2 - $watermarkWidth/2;
$marginBottom = $marginBottom ? $marginBottom : $imageHeight/2 - $watermarkHeight/2;
// Apply watermark to resource
imagecopy(
$image,
$this->watermarkResource,
$imageWidth - $watermarkWidth - $marginRight,
$imageHeight - $watermarkHeight - $marginBottom,
0,
0,
imagesx($this->watermarkResource),
imagesy($this->watermarkResource)
);
// Save file
$this->createImageFileFromResource($fileProps, $image, $pathToFile);
}
}
}
/**
* Create image resource from file
*
* @param string $pathToFile Path to file
* @return resource|null
*/
private function getResourceFromFile(string $pathToFile)
{
if (!file_exists($pathToFile)) {
return null;
}
$fileProps = @getimagesize($pathToFile);
if (!$fileProps || empty($fileProps[2])) {
return null;
}
$imageType = $fileProps[2];
switch($imageType) {
case ( $imageType == IMAGETYPE_JPEG ) :
return imagecreatefromjpeg($pathToFile);
break;
case ( $imageType == IMAGETYPE_GIF ) :
return imagecreatefromgif($pathToFile);
break;
case ( $imageType == IMAGETYPE_PNG ) :
return imagecreatefrompng($pathToFile);
break;
case ( $imageType == IMAGETYPE_BMP ) :
return imagecreatefrombmp($pathToFile);
break;
default: return null;
}
}
/**
* Create image file from resource
*
* @param array $fileProps Array of image properties
* @param resource $imageResource Image resource
* @param string $pathToFile Path to file
* @return bool
*/
private function createImageFileFromResource(array $fileProps, $imageResource, string $pathToFile) : bool
{
if (!is_resource($imageResource)) {
return false;
}
$processed = true;
$imageType = $fileProps[2];
switch($imageType) {
case ( $imageType == IMAGETYPE_JPEG ) :
imagejpeg($imageResource, $pathToFile);
break;
case ( $imageType == IMAGETYPE_GIF ) :
imagegif($imageResource, $pathToFile);
break;
case ( $imageType == IMAGETYPE_PNG ) :
imagepng($imageResource, $pathToFile);
break;
case ( $imageType == IMAGETYPE_BMP ) :
imagebmp($imageResource, $pathToFile);
break;
default:
$processed = false;
break;
}
return $processed;
}
/**
* Create thumbs from images inside the working directory
*
* @return void
*/
public function createThumbsFromDirectory() : bool
{
// Verify directories
$this->checkDirectory();
$this->thumbfiles = scandir($this->thumbnailDir);
// When reaching child folder we need to skip thumb folder
if ($this->workingDir == $this->thumbnailDir) {
return false;
}
// Collect files inside working directory
$files = scandir($this->workingDir);
foreach ($files as $file) {
if (is_dir($this->workingDir.$file)) {
if (
$this->isCurrentFolder($this->workingDir.$file) ||
$this->isParentFolder($this->workingDir.$file)
) {
continue;
}
// Process data from children folder
if ($this->searchNested) {
$this->processNestedDirectory($this->workingDir.$file);
continue;
}
}
// File does not exists, skip
if (!file_exists($this->workingDir.$file)) {
continue;
}
// Process image as thumbnail
$this->createThumbnail($this->workingDir.$file, $file);
}
if ($this->enableDump) {
// Create log dump
error_log(implode("\n", $this->log)."\n", 3, self::LOG_DIR.'thumbGenerator_'.date('Y-m-d-Hi').'.log');
}
return true;
}
/**
* Create thumbnail given file
*
* @param string $pathToFile Path to file
* @param string $fileName Filename
* @return boolean
*/
public function createThumbnail(string $pathToFile, string $fileName) : bool
{
$processed = false;
// Get file properties
$fileProps = @getimagesize($pathToFile);
// If cannot fetch it or type is missing, log and skip
if (!$fileProps || empty($fileProps[2])) {
$this->log[] = date('Y-m-d-H:i:s').': Not an image '.$pathToFile;
return $processed;
}
$thumbName = $this->thumbnailDir.$fileName;
if (file_exists($thumbName)) {
$thumbName = $this->renameDestinationFile($fileName, $this->thumbnailDir.$fileName);
}
// Process images based on type
$processed = $this->processImageFile($thumbName, $pathToFile, $fileProps);
if (!$processed) {
$this->log[] = date('Y-m-d-H:i:s').': Not a valid image format '.$fileName;
}
return $processed;
}
/**
* Process image file based on type
*
* @param string $thumbName Thumbnail name
* @param string $pathToFile Path to file
* @param array $fileProps Array of image props
* @return boolean
*/
private function processImageFile(string $thumbName, string $pathToFile, array $fileProps) : bool
{
$resource = $this->getResourceFromFile($pathToFile);
$imageResource = $this->resizeImage($pathToFile,$resource,$fileProps[0],$fileProps[1]);
$processed = $this->createImageFileFromResource($fileProps, $imageResource, $thumbName);
if (is_resource($imageResource)) {
imagedestroy($imageResource);
}
if ($processed && $this->watermark) {
$this->addWatermark($thumbName);
}
return $processed;
}
/**
* Resize given image based on resize ratio
*
* @param string $sourceFile Path to file
* @param resource $resourceId Resource
* @param integer $width Original width
* @param integer $height Original height
* @return resource
*/
private function resizeImage(string $sourceFile, $resourceId, int $width, int $height)
{
// If is not resource stop process
if (!is_resource($resourceId)) {
return null;
}
// Calculate thumb ratio
$targetWidth = $width*($this->widthReduction/100);
$targetHeight = $height*($this->heightReduction/100);
$imageResource = @imagecreatetruecolor($targetWidth,$targetHeight);
if (!$imageResource) {
return null;
}
imagecopyresampled($imageResource,$resourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);
// Get image details data
$exif = @exif_read_data($sourceFile);
$degree = false;
if (!empty($exif['Orientation'])) {
// Calculate rotation based on image data
switch ($exif['Orientation']) {
case 3: $degree = 180; break;
case 6: $degree = 270; break;
case 8: $degree = 90; break;
default: break;
}
}
// Rotate image if needed
$imageResource = $degree ? imagerotate($imageResource,$degree,0) : $imageResource;
return $imageResource;
}
/**
* Rename file with same existing name
*
* @param string $fileName File name
* @param string $pathToFile Path to file
* @return string
*/
private function renameDestinationFile(string $fileName, string $pathToFile) : string
{
// Search for extension dot position
$typeSplit = strrpos($pathToFile, '.');
// Remove extension from string
$extension = substr($pathToFile, $typeSplit, strlen($pathToFile));
$pathToFile = substr($pathToFile, 0, $typeSplit);
// Detect actual incremental value
if (empty($this->cloneFile[$fileName])) {
$this->cloneFile[$fileName] = $this->calculateClones($fileName);
}
$this->cloneFile[$fileName]++;
// Add incremental value to name and add back extension;
$pathToFile .= "({$this->cloneFile[$fileName]})".$extension;
return $pathToFile;
}
/**
* Calculate clones to rename thumbs
*
* @param string $fileName
* @return integer
*/
private function calculateClones(string $fileName) : int
{
$totalCount = -1;
// Search for extension dot position
$typeSplit = strrpos($fileName, '.');
$rawName = substr($fileName, 0, $typeSplit);
foreach ($this->thumbfiles as $file) {
$pattern = "/(".$rawName."|".$rawName."\([0-9]*\))\.[a-z]{3,4}$/";
if (preg_match($pattern, $file)) {
$totalCount++;
}
}
return $totalCount;
}
/**
* Perform thumbnail creation from child folder
*
* @param string $nestedFolder Path to nested folder
* @return void
*/
private function processNestedDirectory(string $nestedFolder) : void
{
$pattern = "/^".addcslashes($this->workingDir,'/')."/";
if (preg_match($pattern, $nestedFolder)) {
$this->parentFolder[] = $this->workingDir;
$this->workingDir = $nestedFolder.DIRECTORY_SEPARATOR;
$this->createThumbsFromDirectory();
$this->workingDir = array_pop($this->parentFolder);
}
}
/**
* Detect if given folder is current folder (.)
*
* @param string $folder
* @return boolean
*/
private function isCurrentFolder(string $folder) : bool
{
return preg_match("/\/\.\/{0,1}$/", $folder);
}
/**
* Detect if given folder is parent folder (..)
*
* @param string $folder
* @return boolean
*/
private function isParentFolder(string $folder) : bool
{
return preg_match("/\/\.\.\/{0,1}$/", $folder);
}
}