-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.php
executable file
·72 lines (55 loc) · 1.86 KB
/
image.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
<?php
use PHPImageWorkshop\Core\ImageWorkshopLayer;
use PHPImageWorkshop\ImageWorkshop;
require __DIR__.'/includes/init.php';
require __DIR__.'/includes/class/PHPImageWorkshop/ImageWorkshop.php';
$id = $request->query->getInt('key');
$width = $request->query->getInt('w') ?: null;
$height = $request->query->getInt('h') ?: null;
$fit = $request->query->get('fit', 'crop');
$cachedFilepath = _getImageCachedFilePath([
'key' => $id,
'w' => $width,
'h' => $height,
'fit' => $fit,
]);
if ( ! file_exists($cachedFilepath)) {
if ( ! $id) {
exit('no image id');
}
$imagePath = __DIR__.'/images/'.$id.'.jpg';
if ( ! file_exists($imagePath)) {
exit('image file not found');
}
$layer = ImageWorkshop::initFromPath($imagePath);
if ($width || $height) {
if ($fit == 'crop') {
$layer->resize(ImageWorkshopLayer::UNIT_PIXEL, $width, $height, ! $width || ! $height);
} else { // fit == clip
if ($width && $layer->getWidth() > $width) {
$layer->resize(ImageWorkshopLayer::UNIT_PIXEL, $width, null, true);
}
if ($height && $layer->getHeight() > $height) {
$layer->resize(ImageWorkshopLayer::UNIT_PIXEL, null, $height, true
);
}
}
}
$folder = dirname($cachedFilepath);
$imageName = basename($cachedFilepath);
$layer->save($folder, $imageName);
}
$binary = file_get_contents($cachedFilepath);
header('Content-Type: image/jpeg');
header('ETag: '.md5($binary));
echo $binary;
function _getImageCachedFilePath ($imageData)
{
$imageName = '';
foreach ($imageData as $attr => $value) {
$imageName .= $attr.$value.'_';
}
$imageName = trim($imageName, '_');
$imageName .= '.jpg';
return sys_get_temp_dir().'/'.$imageName;
}