-
Notifications
You must be signed in to change notification settings - Fork 0
/
Imgs.php
548 lines (438 loc) · 15.3 KB
/
Imgs.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
declare(strict_types=1);
namespace semmelsamu;
class Imgs
{
# ~
# Class Constants
# ~
/**
* SUPPORTED_FORMATS
*
* Array of formats supported by the class. Applies for reading and outputting images.
* Notice: "jpeg" is not in the list, as it gets converted to "jpg".
*/
const SUPPORTED_FORMATS = ["jpg", "png"];
# ~
# Constructor
# ~
/**
* __construct
*
* @param string $root_path The prefix for where images will be loaded.
* @param ?int $max_size The maximum size of a side of the image. When trying to scale an image
* larger than that, it will be scaled to this. If not set (not recommended),
* the image can be resized as large as PHP can handle it.
* @param bool $enable_cache Specifies if the caching function should be used.
* @param string $cache_path The path to the directory where the rendered images will be stored.
* @param int $max_cache_files The number of maximal images in the cache. If more
* images are added, old ones will be deleted.
*
* @return self
*/
public function __construct(
protected string $root_path = "./",
protected ?int $max_size = 2000,
protected bool $enable_cache = true,
protected string $cache_path = "cache/",
protected int $max_cache_files = 100
)
{
if($this->enable_cache)
{
if(!is_dir($this->cache_path))
mkdir($this->cache_path);
}
}
# ~
# Prepare Functions
# ~
/**
* string
*
* Parses parameters of a string and calls the prepare function.
* @see prepare()
*
* @param mixed $string
* @return self
*/
public function string(string $string)
{
$this->html_src = $string;
$parsed_url = parse_url($string);
parse_str($parsed_url["query"] ?? "", $parsed_string);
if(empty($parsed_url["path"]))
throw new \Exception("At least filename must be given");
$this->prepare(
filename: $parsed_url["path"],
top: isset($parsed_string["t"]) ? intval($parsed_string["t"]) : null,
right: isset($parsed_string["r"]) ? intval($parsed_string["r"]) : null,
bottom: isset($parsed_string["b"]) ? intval($parsed_string["b"]) : null,
left: isset($parsed_string["l"]) ? intval($parsed_string["l"]) : null,
width: isset($parsed_string["w"]) ? intval($parsed_string["w"]) : null,
height: isset($parsed_string["h"]) ? intval($parsed_string["h"]) : null,
quality: isset($parsed_string["q"]) ? intval($parsed_string["q"]) : null,
format: $parsed_string["f"] ?? null
);
return $this;
}
/**
* prepare
*
* Captures and validates every parameter for the image.
*
* @param string $filename The Filename of the image. Will be prefixed with $root_path (@see __construct)
* @param ?int $width The output width of the image. If not set, it will be calculated automatically.
* @param ?int $height The output height of the image. If not set, it will be calculated automatically.
* If neither $width or $height are set, the image will have the original width and height.
* If both $width and $height are set and do not match the original aspect ratio, the image will be cropped.
* @param ?int $quality The quality of the output image.
* E.g. for jpeg quality, @see https://www.php.net/manual/de/function.imagejpeg.php#refsect1-function.imagejpeg-parameters
* @param ?string $format The output format. If not set, the format will be the same as the original source.
* @see SUPPORTED_FORMATS
*
* @return self
*/
public function prepare(
string $filename,
?int $top = null,
?int $right = null,
?int $bottom = null,
?int $left = null,
?int $width = null,
?int $height = null,
?int $quality = null,
?string $format = null
)
{
// Capture Parameters
# Src
if(!isset($this->html_src))
$this->html_src = $filename;
# Path
$this->original_path = $this->root_path . $filename;
# Crop
$this->output_top = $top ?? 0;
$this->output_right = $right ?? 0;
$this->output_bottom = $bottom ?? 0;
$this->output_left = $left ?? 0;
# Original Dimensions
list($this->original_width, $this->original_height) = getimagesize($this->original_path);
# Output Dimensions
$this->output_width = $width;
$this->output_height = $height;
# Quality
$this->output_quality = $quality ?? -1;
# Format
$this->original_format = pathinfo($this->original_path, PATHINFO_EXTENSION);
$this->output_format = $format ?? $this->original_format;
// Validate and Prepare
$this->validate_format();
$this->calculate_rectangles();
return $this;
}
# ~
# Image Function
# ~
/**
* image
*
* If not cached, renders and saves the prepared image to cache.
* Then, outputs the corresponding header and image to the user. Finally, ends the script.
*
* @return void
*/
public function image()
{
if($this->enable_cache)
{
$this->calculate_cached_path();
if(!file_exists($this->cached_path))
{
$this->load_original_image();
$this->render_image();
imagedestroy($this->original_image);
$this->cache_rendered_image();
imagedestroy($this->rendered_image);
$this->invalidate_cache();
}
$this->output_cached_image();
}
else
{
$this->load_original_image();
$this->render_image();
imagedestroy($this->original_image);
$this->output_rendered_image();
imagedestroy($this->rendered_image);
}
exit;
}
# ~
# Helper Functions
# ~
/**
* validate_format
*
* Checks if the format specified is actually supported. If not, throws an exception.
*
* @return void
*/
protected function validate_format()
{
if($this->original_format == "jpeg") $this->original_format = "jpg";
if($this->output_format == "jpeg") $this->output_format = "jpg";
if(!in_array($this->original_format, self::SUPPORTED_FORMATS))
throw new \Exception("Source image format $this->original_format is not supported.");
if(!in_array($this->output_format, self::SUPPORTED_FORMATS))
throw new \Exception("Output image format $this->output_format is not supported.");
}
/**
* calculate_rectangles
*
* Calculates the coordinates and dimensions of the source and destination rectangles,
* used later to resize the image. @see image()
*
* @return void
*/
protected function calculate_rectangles()
{
// Import Values for easier reading
$original_width = $this->original_width;
$original_height = $this->original_height;
$width = $this->output_width;
$height = $this->output_height;
$top = $this->output_top;
$right = $this->output_right;
$bottom = $this->output_bottom;
$left = $this->output_left;
$max_size = $this->max_size;
$src_x = 0;
$src_y = 0;
$src_width = $original_width;
$src_height = $original_height;
// Util variables
$crop_w = $src_width - $left - $right;
$crop_h = $src_height - $top - $bottom;
$src_ratio = $src_height / $src_width;
// Default dimensions
if(!isset($width) && !isset($height))
{
$width = $crop_w;
$height = $crop_h;
}
// Auto calculation missing dimensions
if(isset($height) && !isset($width))
$width = $crop_w * ($height / $crop_h);
else if(isset($width) && !isset($height))
$height = $crop_h * ($width / $crop_w);
// Max size
if(isset($max_size))
{
$ratio = $height / $width;
if($width > $max_size)
{
$width = $max_size;
$height = $width * $ratio;
}
if($height > $max_size)
{
$height = $max_size;
$width = $height / $ratio;
}
}
// Crop image if aspect ratio changes
$dst_ratio = $height / $width;
$cut_w = $crop_w;
$cut_h = $crop_w * $dst_ratio;
if($cut_h > $crop_h)
{
$cut_h = $crop_h;
$cut_w = $crop_h / $dst_ratio;
}
$src_x = $left + ($crop_w - $cut_w) / 2;
$src_y = $top + ($crop_h - $cut_h) / 2;
$src_width = $cut_w;
$src_height = $cut_h;
// Export Values
$this->dst_x = 0;
$this->dst_y = 0;
$this->src_x = intval(round($src_x));
$this->src_y = intval(round($src_y));
$this->dst_width = intval(round($width));
$this->dst_height = intval(round($height));
$this->src_width = intval(round($src_width));
$this->src_height = intval(round($src_height));
}
/**
* calculate_cached_path
*
* Calculates the path to the cached image.
*
* @return void
*/
protected function calculate_cached_path()
{
$key = $this->original_path .
"-" . $this->dst_x .
"-" . $this->dst_y .
"-" . $this->src_x .
"-" . $this->src_y .
"-" . $this->dst_width .
"-" . $this->dst_height .
"-" . $this->src_width .
"-" . $this->src_height .
"-" . $this->output_quality .
"." . $this->output_format;
$this->cached_path = $this->cache_path . urlencode($key);
}
/**
* load_original_image
*
* Loads the original image into a GdImage object.
*
* @return void
*/
protected function load_original_image()
{
switch($this->original_format)
{
case "png":
$this->original_image = imagecreatefrompng($this->original_path);
break;
case "jpg":
default:
$this->original_image = imagecreatefromjpeg($this->original_path);
break;
}
}
/**
* render_image
*
* Renders the image by resampling it.
* @see calculate_rectangles()
*
* @return void
*/
protected function render_image()
{
$this->rendered_image = imagecreatetruecolor($this->dst_width, $this->dst_height);
imagealphablending($this->rendered_image, false);
imagesavealpha($this->rendered_image, true);
imagecopyresampled(
$this->rendered_image,
$this->original_image,
$this->dst_x,
$this->dst_y,
$this->src_x,
$this->src_y,
$this->dst_width,
$this->dst_height,
$this->src_width,
$this->src_height
);
}
/**
* cache_rendered_image
*
* Saves the rendered image to the cache location.
* @see render_image()
*
* @return void
*/
protected function cache_rendered_image()
{
switch($this->output_format)
{
case "png":
imagepng($this->rendered_image, $this->cached_path, $this->output_quality);
break;
case "jpg":
default:
imagejpeg($this->rendered_image, $this->cached_path, $this->output_quality);
break;
}
}
/**
* output_cached_image
*
* Reads the contents of the cached image and outputs it and the corresponding headers to the user.
* @see cache_rendered_image()
*
* @return void
*/
protected function output_cached_image()
{
header('Content-Length: ' . filesize($this->cached_path));
header("Content-Type: " . mime_content_type($this->cached_path));
readfile($this->cached_path);
}
/**
* invalidate_cache
*
* When there are more files in the cache folder than $max_cache_files, they will be deleted.
*
* @return void
*/
protected function invalidate_cache()
{
// Get Files in cache
$all_files = scandir($this->cache_path);
$files = array_diff($all_files, array('.', '..'));
// Sort by last modified
usort($files, function($a, $b) {
return filemtime($this->cache_path . $a) - filemtime($this->cache_path . $b);
});
// If there are more files than $max_cache_files, delete them
$files_to_delete = sizeof($files) - $this->max_cache_files;
for($i = 0; $i < $files_to_delete; $i++)
{
unlink($this->cache_path . array_values($files)[$i]);
}
}
/**
* output_rendered_image
*
* Outputs rendered image and corresponding headers to the user.
* @see render_image()
*
* @return void
*/
protected function output_rendered_image()
{
switch($this->output_format)
{
case "png":
header("Content-Type: image/png");
imagepng($this->rendered_image, quality: $this->output_quality);
break;
case "jpg":
default:
header("Content-Type: image/jpeg");
imagejpeg($this->rendered_image, quality: $this->output_quality);
break;
}
}
/**
* html
*
* Returns a HTML image tag, with the source of the image, and matching the
* width and height of the image after cropping. Useful for preventing layout shifting.
*
* @param ?string $alt The alt text of the image.
* @return string The HTML image tag.
* Example: <img src="image.jpg?w=800" width="800" height="600" alt="Alt text.">
*/
public function html(...$attributes)
{
if(!file_exists($this->original_path))
return;
$src = 'src="' . $this->html_src . '"';
$width = 'width="' . $this->dst_width . '"';
$height = 'height="' . $this->dst_height . '"';
$attribute_string = "";
foreach($attributes as $key => $value)
$attribute_string .= "$key=\"$value\"";
return "<img $src $width $height $attribute_string>";
}
}