-
Notifications
You must be signed in to change notification settings - Fork 5
/
image.php
80 lines (64 loc) · 1.48 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
73
74
75
76
77
78
79
80
<?
$path = $_GET['name'];
if (!preg_match('/^[A-Za-z0-9.-]*$/', $path) || !file_exists("/photos/{$path}"))
{
header('HTTP/1.1 404 Not Found');
echo "404 Not found";
exit;
}
header('Content-Type: image/jpeg');
if (file_exists("/photos/scaled/{$path}"))
{
readfile("/photos/scaled/{$path}");
exit;
}
$data = @file_get_contents("/photos/{$path}");
function get_rotation($filename) {
if (!function_exists('exif_read_data')) {
return;
}
$exif = exif_read_data($filename);
if(!$exif || !isset($exif['Orientation'])) {
return;
}
$orientation = $exif['Orientation'];
if ($orientation == 1) {
return;
}
$img = imagecreatefromjpeg($filename);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
return $deg;
}
$image = @imagecreatefromstring($data);
$rotation = get_rotation("/photos/{$path}");
if ($rotation) {
$image = imagerotate($image, $rotation, 0);
}
$old_width = imagesx($image);
$old_height = imagesy($image);
if (!$rotation && $old_width < 960)
{
echo "$data";
exit;
}
$new_width = 960;
$new_height = 960 * $old_height / $old_width;
$newimage = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($newimage, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
ob_start();
imagejpeg($newimage);
$data = ob_get_contents();
ob_end_clean();
echo "$data";
file_put_contents("/photos/scaled/{$path}", $data);