-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_photo_enne_classi.php
164 lines (148 loc) · 5.47 KB
/
upload_photo_enne_classi.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
<?php
require_once "usesession.php";
require_once "../../../conf.php";
require_once "fnc_general.php";
$photo_upload_error = null;
$image_file_type = null;
$image_file_name = null;
$file_name_prefix = "vr_";
$file_size_limit = 1.5 * 1024 * 1024;
$image_max_w = 600;
$image_max_h = 400;
$resize_image = null;
if(isset($_POST["photo_submit"])){
//var_dump($_POST);
//var_dump($_FILES);
//kas üldse on pilt
$check = getimagesize($_FILES["file_input"]["tmp_name"]);
if($check !== false){
//kontrollime, kas aktepteeritud failivorming ja fikseerime laiendi
if($check["mime"] == "image/jpeg"){
$image_file_type = "jpg";
} elseif ($check["mime"] == "image/png"){
$image_file_type = "png";
} else {
$photo_upload_error = "Pole sobiv formaat! Ainult jpg ja png on lubatud!";
}
} else {
$photo_upload_error = "Tegemist pole pildifailiga!";
}
if(empty($photo_upload_error)){
//ega pole liiga suur fail
if($_FILES["file_input"]["size"] > $file_size_limit){
$photo_upload_error = "Valitud fail on liiga suur! Lubatud kuni 2MiB!";
}
if(empty($photo_upload_error)){
//loome oma failinime
$timestamp = microtime(1) * 10000;
$image_file_name = $file_name_prefix .$timestamp ."." .$image_file_type;
// suuruse muutmine
$temp_image = null;
if($image_file_type == "jpg"){
$temp_image = imagecreatefromjpeg($_FILES["file_input"]["tmp_name"]);
}
if($image_file_type == "png"){
$temp_image = imagecreatefrompng($_FILES["file_input"]["tmp_name"]);
}
$image_w = imagesx($temp_image);
$image_h = imagesy($temp_image);
//kuvasuhte säilitamiseks arvutame suuruse muutuja kordaja lähtudes kõrgusest või laiusest
if($image_w / $image_max_w > $image_h / $image_max_h){
$image_size_ratio = $image_w / $image_max_w;
}else {
$image_size_ratio = $image_h / $image_max_h;
}
$image_new_w = round($image_w / $image_size_ratio);
$image_new_h = round($image_h / $image_size_ratio);
// vähendamiseks loome uue image objekti, kuhu kopeerime vähendatud kujutise
$new_temp_image = imagecreatetruecolor($image_new_w, $image_new_h);
imagecopyresampled($new_temp_image, $temp_image, 0, 0, 0, 0, $image_new_w, $image_new_h, $image_w, $image_h);
// salvestame faili
$target_file = "../upload_photos_normal/" .$image_file_name;
if($image_file_type == "jpg"){
if(imagejpeg($new_temp_image, $target_file, 90)){
$photo_upload_error = "Vähendatud pilt on salvestatud!";
} else{
$photo_upload_error = "Vähendatud pilti ei salvestatud!";
}
}
if($image_file_type == "png"){
if(imagepng($new_temp_image, $target_file, 6)){
$photo_upload_error = "Vähendatud pilt on salvestatud!";
} else{
$photo_upload_error = "Vähendatud pilti ei salvestatud!";
}
$target_file = "../upload_photos_orig/" .$image_file_name;
if(move_uploaded_file($_FILES["file_input"]["tmp_name"], $target_file)){
$photo_upload_error .= "Foto üleslaadimine õnnestus!";
} else {
$photo_upload_error .= " Foto üleslaadimine ebaõnnestus!";
}
}
$new_temp_image = resize_image($temp_image, 100, 100, false );
$target_file = "../upload_photos_thumbnail/" .$image_file_name;
if($image_file_type == "jpg"){
if(imagejpeg($new_temp_image, $target_file, 90)){
$photo_upload_error = "Vähendatud pilt on salvestatud!";
} else {
$photo_upload_error = "Vähendatud pilti ei salvestatud!";
}
}
if($image_file_type == "png"){
if(imagepng($new_temp_image, $target_file, 6)){
$photo_upload_error = "Thumbnail on salvestatud!";
} else {
$photo_upload_error = "Thumbnail ei salvestu!";
}
}
$target_file = "../upload_photos_orig/" .$image_file_name;
if(move_uploaded_file($_FILES["file_input"]["tmp_name"], $target_file)){
if (upload_to_database($_FILES["file_input"]["name"],$image_file_name, $_POST['alt_text'], $_POST['privacy_input']) == 1){
$photo_upload_error .= " Pildi üleslaadimine õnnestus!";
} else {
$photo_upload_error .= " Pildi lisamine ebaõnnestus";
}
} else {
$photo_upload_error .= " Pildi üleslaadimine ebaõnnestus!";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="et">
<head>
<meta charset="utf-8">
<title>Veebirakendused ja nende loomine 2021</title>
</head>
<body>
<h1>Fotode üleslaadimine</h1>
<p>See leht on valminud õppetöö raames!</p>
<hr>
<p><a href="home.php">Avalehele</a></p>
<hr>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">
<label for="file_input">Vali foto fail! </label>
<input id="file_input" name="file_input" type="file">
<br>
<label for= "alt_input">Alternatiivtekst ehk pildi selgitus</label>
<input id="alt_text" name = "alt_text" type = "text" placeholder="Pildil on ... ">
<br>
<label> Privaatsustase: </label>
<br>
<label for="privacy_input_1">Privaatne</label>
<input id="privacy_input_1" name ="privacy_input" type="radio" value="3" checked>
<br>
<label for="privacy_input_2">Registreeritud kasutajatele</label>
<input id="privacy_input_2" name ="privacy_input" type="radio" value="2" >
<br>
<label for="privacy_input_3">Avalik</label>
<input id="privacy_input_3" name ="privacy_input" type="radio" value="1" >
<br>
<input type="submit" name="photo_submit" value="Lae pilt üles!">
</form>
<p><?php echo $photo_upload_error; ?></p>
<hr>
<p><a href="?logout=1">Logi välja</a></p>
</body>
</html>