This repository was archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.php
More file actions
241 lines (208 loc) · 8.5 KB
/
upload.php
File metadata and controls
241 lines (208 loc) · 8.5 KB
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
<!--Created by Craig Huff
11/28/18
CS 174 Final Project - Imagine Images
-->
<?php
include "../inc/dbinfo.inc";
session_start();
$watermark = imagecreatefrompng("Watermark.png");
if($_SESSION["homeURL"] !== "/dashboard.php"){
echo '<meta http-equiv="refresh" content="0; url=/">';
exit(0);
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="myicon.png" />
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0">
<meta name="google-signin-client_id" content="773465469592-70tepenvk2lc7sbhs1d1k5i98k0gdp09.apps.googleusercontent.com">
<title>Imagine Images</title>
<script src="https://apis.google.com/js/platform.js"></script>
<!-- CSS -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="css/style.css" type="text/css" rel="stylesheet" media="screen,projection">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="js/init.js"></script>
<script src="js/materialize.js"></script>
<script src="js/materialize.min.js"></script>
</head>
<body>
<nav class="light-blue" style="line-height: 0px"role="navigation">
<div class="nav-wrapper" >
<a id="logo-container" href='<?php echo ($_SESSION["homeURL"]);?>' class="brand-logo amber-text text-accent-2 center hide-on-small-and-down" style="padding-top:30px;">Imagine Images</a>
<ul id="nav-mobile" class="right">
<li><div class="right g-signin2 hide-on-small-and-down" style ="display:none;" data-onsuccess="onSignIn"></div></li>
<li><div class="right btn amber hide-on-small-and-down" style="margin-top:14px; margin-right:30px;" onclick="signOut()"><span class="black-text">Sign Out</span></div></li>
<li><div class="right btn amber hide-on-med-and-up" style ="margin-top:10px; margin-right:10px" onclick="signOut()"><span class="black-text">Sign Out</span></div></li>
</ul>
<ul>
<li><i class="large material-icons left hide-on-small-and-down" style="padding-left:30px;">camera_roll</i></li>
<li><i class="large material-icons left hide-on-med-and-up" style="padding-left:13px;">camera_roll</i></li>
</ul>
</div>
</nav>
<div class="container">
<div class="section">
<?php
$target_dir = "uploads/";
$uploadOk = 1;
$target_name = preg_replace('/\s+/', '', basename($_FILES["fileToUpload"]["name"]));
$target_file = $target_dir . $target_name;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if ($_POST["newFileName"] != ""){
$target_name = $_POST["newFileName"] . "." .$imageFileType;
$target_file = $target_dir . $target_name;
}
//Taken from W3Schools
//https://www.w3schools.com/PHP/php_file_upload.asp
if ($_FILES["fileToUpload"]["error"] > 0)
{
echo "Sorry about that.<br>";
echo "This image can't be uploaded, we'll take a look at the problem.";
}
// Check if image file is a actual image or fake image
if($target_dir != $target_file){
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
// echo "<a>" . "File is an image - " . $check["mime"] . ".</a><br>";
$uploadOk = 1;
} else {
echo "<a>" . "File is not an image.<br>";
$uploadOk = 0;
}
}
} else {
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file) && $uploadOk == 1) {
echo "<a>" . "Sorry, file already exists.</a><br>";
$uploadOk = 0;
}
// Check file size: Must be less than 5.3MB (This is set by AWS)
if ($_FILES["fileToUpload"]["size"] > 5388608 && $uploadOk == 1) {
echo "<a>" . "Sorry, the file must be less than 5MB.</a><br>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $uploadOk == 1) {
echo "<a>" . "Sorry, only JPG, JPEG & PNG files are allowed.</a><br>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<a>" . "Sorry, your file was not uploaded.</a><br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<a style='text-align:center;'>" . "The file '". $target_name . "' has been uploaded.</a><br>";
list($width, $height) = getimagesize($target_file) ;
echo "<a style='text-align:center;'>" . "Image size is $width x $height</a><br>";
shell_exec('aws s3 sync /var/www/html/uploads/ s3://rekognitiontest174/ ');
} else {
echo "<a>" . "Sorry, there was an error uploading your file.</a><br>";
}
$script = 'aws rekognition detect-labels --image \'{"S3Object":{"Bucket":"rekognitiontest174","Name":"' . $target_name . '"}}\' --region us-west-2 ';
$rekognize = shell_exec($script);
$labels_found = json_decode($rekognize,true);
$_SESSION["categories"] = $labels_found['Labels'];
echo "<img style='display: block; margin-left: auto; margin-right: auto;' width='75%' src='$target_file'>";
if (count($labels_found['Labels'])) {
echo "<a> Check the boxes of categories that apply to this image </a>";
echo (' <form id="test" action="#"> <p> ');
for($i = 0; $i < 5; $i++){
$category = $labels_found['Labels'][$i]['Name'];
//$_SESSION["cat".$i] = $category;
echo("<label> <input id='cat_$i' type='checkbox' display='inline-block' class='filled-in'/><span> $category </span> </label> ");
}
echo ('</p> </form>');
}
// ######################################
// # Image Copy and Merge
// #####################################
$target_watermark = "watermarked/" . $target_name;
if($imageFileType === "png") {
$im = imagecreatefrompng($target_file);
$png = true;
} else if ($imageFileType === "jpg" || $imageFileType ==="jpeg"){
$im = imagecreatefromjpeg($target_file);
$jpeg = true;
}
$foreground = $watermark;
$overlay_width = imagesx($foreground);
$overlay_height = imagesy($foreground);
//center width and height of the background image
$width = imagesx($im) / 2 - $overlay_width / 2;
$height = imagesy($im) / 2 - $overlay_height / 2;
//From the GD library, merges the two pictures together
//starting at a point specified by the method
//The overall opaqueness of the image can also be changed
imagecopymerge($im, $foreground, $width, $height, 0, 0, $overlay_width, $overlay_height, 30);
if(isset($png)) {
imagepng($im, $target_watermark);
} else if(isset($jpeg)) {
imagejpeg($im, $target_watermark);
}
imagedestroy($im);
imagedestroy($watermark);
}
?>
<br>
<a class="waves-effect waves-dark amber btn-large" style="display: block; margin-left: auto; margin-right: auto;" onclick="goHome()" >Home</a>
</div>
<div class="section no-pad-bot" id="index-banner">
<div class="container"></div>
</div>
<script>
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
var id_token = googleUser.getAuthResponse().id_token;
}
</script>
<script>
function signOut() {
gapi.auth2.init();
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
$( "#newUser" ).load( "logout.php", function() { });
window.location.replace('/');
}
</script>
<script>
function goHome(){
<?php if ($uploadOk == 1) {
echo('var str = "cat_";
var check = document.getElementById(str.concat("0")).checked;
var categories = new Array();
for(var i = 0; i < 5; i++) {
if(document.getElementById(str.concat(i)).checked == true) {
categories.push(i);
}
}');
echo("$.ajax(
{
type: 'post',
url: 'image_upload.php',
cache: false,
data: {cats: categories, name: '$target_name', path: '$target_file' },
success: function(){
window.location.replace('/dashboard.php');
}
})\n");
}?>
}
</script>
</body>
</html>