-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.php
235 lines (192 loc) · 6.09 KB
/
function.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
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
<?php
function ProcessImage($imgFile){
global $db_server,$COEFFNUM;
// Parse image
list ($y_values, $i_values, $q_values) = ParseImage("img/".$imgFile);
// Dempose and trunctate
DecomposeImage($y_values);
$y_trunc = TruncateCoeffs($y_values, $COEFFNUM);
DecomposeImage($i_values);
$i_trunc = TruncateCoeffs($i_values, $COEFFNUM);
DecomposeImage($q_values);
$q_trunc = TruncateCoeffs($q_values, $COEFFNUM);
require 'config.php';
// save image file
$imageid = InsertFileToDB($imgFile,$y_values[0][0],$i_values[0][0],$q_values[0][0],$connection);
// save y-coeffs
InsertCoeffsToDB("coeffs_y",$y_trunc,$imageid);
// save i-coeffs
InsertCoeffsToDB("coeffs_i",$i_trunc,$imageid);
// save q-coeffs
InsertCoeffsToDB("coeffs_q",$q_trunc,$imageid);
mysqli_close($connection);
}
function UploadImage($location){
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// check for errors
if ($_FILES["file"]["error"] > 0){
exit("Return Code: " . $_FILES["file"]["error"] . "<br>");
}
// get uploaded image
$filepart = implode(explode(".",$_FILES["file"]["name"],-1));
$explosion = explode(".", $_FILES["file"]["name"]);
foreach ($explosion as $i){
$extension = $i;
}
$extension = strtolower($extension);
// size checks
$image = imagecreatefromjpeg($_FILES["file"]["tmp_name"]);
$image_width = imagesx($image);
$image_height = imagesy($image);
if ($image_height != 300 || $image_width != 300){
exit("Size must be 300x 300");
}
// check type and extension
$allowedExts = array("jpg", "jpeg", "gif", "png");
if ((($_FILES["file"]["type"] != "image/gif")
&& ($_FILES["file"]["type"] != "image/jpeg")
&& ($_FILES["file"]["type"] != "image/png")
&& ($_FILES["file"]["type"] != "image/pjpeg"))
|| ($_FILES["file"]["size"] > 100000)
|| !in_array($extension, $allowedExts)){
exit("Invalid file");
}
// rename if necessary
$filename = $filepart.".".$extension;
while (file_exists($location."/" . $filename)){
$filename = $filepart."_".mt_rand(11,99).".".$extension;
}
// all OK, copy image
move_uploaded_file($_FILES["file"]["tmp_name"], $location . "/" . $filename);
return $filename;
}
// Parse image
function ParseImage($imgFile){
// read image
$image = imagecreatefromjpeg($imgFile);
$image_width = imagesx($image);
$image_height = imagesy($image);
// iterate through x axis
for ($x = 0; $x < $image_width; $x++) {
// iterate through y axis
for ($y = 0; $y < $image_height; $y++) {
// look at current pixel
$rgb = imagecolorat($image, $x, $y);
$r = (($rgb >> 16) & 0xFF) / 255;
$g = (($rgb >> 8) & 0xFF) / 255;
$b = ($rgb & 0xFF) / 255;
// get YIQ values
$y_values[$x][$y] = 0.299*$r + 0.587*$g + 0.114*$b;
$i_values[$x][$y] = 0.596*$r - 0.275*$g - 0.321*$b;
$q_values[$x][$y] = 0.212*$r - 0.523*$g + 0.311*$b;
}
}
return array($y_values, $i_values, $q_values);
}
// insert file to database
function InsertFileToDB($imgFile,$y_average,$i_average,$q_average,$connection){
require 'config.php';
$query = "INSERT INTO images VALUES(NULL,'".$imgFile."',".$y_average.",".$i_average.",".$q_average.")";
$result = mysqli_query($connection,$query);
if (!$result) die('Invalid query: ' . mysql_error());
$result = mysqli_query($connection,"SELECT LAST_INSERT_ID()");
while($row = mysqli_fetch_array($result))
$last_id = $row[0];
return $last_id;
}
// save coefficients
function InsertCoeffsToDB($dbtable,$coefftable,$imageid){
require 'config.php';
global $COEFFNUM;
$query = "INSERT INTO ".$dbtable." VALUES";
for ($i = 0; $i < $COEFFNUM; $i++) {
if ($i>0) $query.=",";
$query.="(".$coefftable['x'][$i].",".$coefftable['y'][$i].",'".$coefftable['sign'][$i]."',".$imageid.")";
}
$result = mysqli_query($connection,$query);
if (!$result) die('Invalid query: ' . mysql_error());
}
// Transpose table
function transpose($array){
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
// Decompose Array
function DecomposeArray(&$array){
//get length
$h = count($array);
// initialize array
for ($x = 0; $x < $h; $x++) {
$array[$x] = $array[$x] / sqrt($h);
}
// do the transformation
while ($h > 1) {
$h = $h /2;
for ($i = 0; $i < $h; $i++) {
$arraynew[$i] = ($array[2*$i] + $array[2*$i+1])/sqrt(2);
$arraynew[$h+$i] = ($array[2*$i] - $array[2*$i+1])/sqrt(2);
}
// copy arraynew to array
for ($i = 0; $i < 2*$h; $i++) {
$array[$i] = $arraynew[$i];
}
}
}
// Decompose Image
function DecomposeImage(&$array){
//get length
$rows = count($array);
// decompose rows
for ($x = 0; $x < $rows; $x++) {
DecomposeArray($array[$x]);
}
// transpose matrix
$array = transpose($array);
// decompose rows again
for ($x = 0; $x < $rows; $x++) {
DecomposeArray($array[$x]);
}
// transpose matrix back
$array = transpose($array);
}
// Truncate Coefficients - return array: x,y,sign
function TruncateCoeffs($multi_array,$m){
list ($abs_array, $sign_array) = TableToArrays($multi_array);
arsort($abs_array);
$i = 0;
foreach ($abs_array as $key => $value){
if ($i==$m)
break;
$coord = explode(",", $key);
$trunc['x'][] = $coord[0];
$trunc['y'][] = $coord[1];
$trunc['sign'][] = $sign_array[$key];
$i++;
}
return $trunc;
}
// Convert multi-dimensional array to array
function TableToArrays($multi){
foreach ($multi as $x => $array){
foreach ($array as $y => $value){
$key = $x.",".$y;
$abs[$key] = abs($value);
$sign[$key] = ($value > 0 ? "+" : "-");
}
}
return array ($abs, $sign);
}
// Bin
function bin($i,$j){
return min(max($i,$j),5);
}
// Database Variables
// Constant Variables
$COEFFNUM = 40;
$w['Y'] = array( 5.00, 0.83, 1.01, 0.52, 0.47, 0.30);
$w['I'] = array(19.21, 1.26, 0.44, 0.53, 0.28, 0.14);
$w['Q'] = array(34.37, 0.36, 0.45, 0.14, 0.18, 0.27);
?>