-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_image.php
126 lines (117 loc) · 3.2 KB
/
rotate_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
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
<?php
if (isset($_GET['dev'])) $_POST = $_POST + $_GET; //REMOVE AFTER DEVELOPMENT
function dieJSON($obj) {
header("Content-type: text/javascript");
die(json_encode($obj));
}
if ($src = $_POST['src']) {
$mime_type = getimagesize($src)['mime'];
switch ($mime_type) {
case 'image/jpeg':
$image = imagecreatefromjpeg($src);
break;
case 'image/png':
$image = imagecreatefrompng($src);
break;
default:
dieJSON(array(
'error' => 'Unsupported image type for rotation. Contact [email protected] for more help.',
));
break;
}
$rotated = imagerotate($image, -90, 0);
switch ($mime_type) {
case 'image/jpeg':
imagejpeg($rotated, $src);
break;
case 'image/png':
imagepng($rotated, $src);
break;
default:
dieJSON(array(
'error' => 'Unsupported image type for rotation. Contact [email protected] for more help.',
));
break;
}
imagedestroy($image);
imagedestroy($rotated);
dieJSON(array(
'src' => $src,
));
}
$error_msg_explanation = '(Should be formatted like `by_year/'.date('Y').'/hack_name/photo.jpg`)';
$valid_url = false;
if ($url = $_GET['url']) {
$valid_url = true;
if (!preg_match('/^by_year\/\d{4}\/[\w\-]+\//', $url) || strpos($url, '/..') !== false) {
$valid_url = false;
$error_msg = 'Bad image path! '.$error_msg_explanation;
} else {
$image_data = @getimagesize('./'.$url); // ['mime'];
if (!$image_data) {
$valid_url = false;
$error_msg = 'Not an image! '.$error_msg_explanation;
}
}
}
header('Content-type: text/html; charset=utf-8');
header('X-UA-Compatible: IE=edge,chrome=1');
?>
<!doctype html>
<html lang="en-us">
<head>
<title>Hack Submission Generator</title>
<style>
body{font-size:14px;font-family:sans-serif;}
label{font-weight:bold;}
label.required{color:red;}
em{font-size:12px;}
li{margin-bottom:10px;}
pre{margin:5px 10px 0px;}
</style>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<h3>Photo rotator!</h3>
<?php if ($valid_url) { ?>
<p>
Here is how this photo currently renders:<br>
<img id="uploaded-photo" src="<?= $url ?>" style="max-height:600px;" /><br>
Want to rotate it?
<input type="button" id="rotate-right" value="Rotate Right" />
</p>
<script type="text/javascript">
var src = '<?= $url ?>';
$(function() {
$('#rotate-right').on('click', function() {
$('#rotate-right').attr('disabled', true);
$.post('rotate_image.php', {src: src}, function(data) {
$('#rotate-right').attr('disabled', false);
if (data.error) {
alert(data.error);
return;
}
$("#uploaded-photo").attr('src', data.src+'?'+(new Date()).getTime());
}, 'json');
});
});
</script>
<?php } else { ?>
<?= $error_msg ? "<b>$error_msg</b>" : '' ?>
<p>
Enter the path for the image you want to rotate.<br>
The path you enter should be the part of the URL which starts at `by_year`:</br>
<form method="get" action="rotate_image.php">
<input
type="text"
name="url"
size="60"
placeholder="by_year/<?= date('Y') ?>/hack_name/photo.jpg"
value="<?= $url ?>"
/><br>
<input type="submit" />
</form>
</p>
<?php } ?>
</body>
</html>