-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkshop.php
86 lines (73 loc) · 2.4 KB
/
workshop.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
<?php
include 'main.php';
$path = $_GET['path'];
if (false === file_exists($path)) {
$path = 'assets/default/images/transparent.png';
}
$ratio = $_GET['ratio'];
$denomination = explode('x', $ratio);
$method = $_GET['method'];
if (count($denomination) > 1) {
$width = $denomination[0];
$height = $denomination[1];
}
$ext = pathinfo($path, PATHINFO_EXTENSION);
$file = pathinfo($path, PATHINFO_FILENAME) . '.' . $ext;
$layer = PHPImageWorkshop\ImageWorkshop::initFromPath($path);
$sourceWidth = $layer->getWidth();
$sourceHeight = $layer->getHeight();
if ($sourceWidth > $sourceHeight) {
$padding = (($sourceWidth - $sourceHeight) * 2);
} else {
$padding = (($sourceHeight - $sourceWidth) * 2);
}
switch ($method) {
case 'resize':
$layer->resizeByLargestSideInPixel($width, $height);
break;
case 'square':
if ($ratio > $sourceWidth || $ratio > $sourceHeight) {
if ($width >= $height) {
// Landscape
$layer->resizeInPixel($ratio + $sourceWidth + $padding, null, true, 0, 0, 'MM');
} else {
// Portrait
$layer->resizeInPixel(null, $ratio + $sourceHeight + $padding, true, 0, 0, 'MM');
}
}
$layer->cropInPixel($ratio, $ratio, 0, 0, 'MM');
break;
case 'fit':
if ($width > $sourceWidth || $height > $sourceHeight) {
if ($width >= $height) {
// Landscape
$layer->resizeInPixel($width, null, true, 0, 0, 'MM');
} else {
// Portrait
$layer->resizeInPixel(null, $height, true, 0, 0, 'MM');
}
} else {
$layer->resizeByNarrowSideInPixel($width, $height);
}
$layer->cropInPixel($width, $height, 0, 0, 'MM');
break;
}
$image = $layer->getResult();
switch (strtolower($ext)) {
case 'png':
header('Content-type: image/png');
header('Content-Disposition: filename="' . $file . '"');
imagepng($image, null, 8);
break;
case 'jpg':
case 'jpeg':
header('Content-type: image/jpeg');
header('Content-Disposition: filename="' . $file . '"');
return imagejpeg($image, null, 100);
break;
case 'gif':
header('Content-type: image/gif');
header('Content-Disposition: filename="' . $file . '"');
return imagegif($image);
break;
}