-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
86 lines (82 loc) · 3.03 KB
/
index.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
<?php
function path_microtime($path, &$small){
list($usec, $sec) = explode(" ", microtime());
$ext = pathinfo($path, PATHINFO_EXTENSION);
$small = './thumbs/uploads/' . $sec . $usec . '.' . $ext;
return './uploads/' . $sec . $usec . '.' . $ext;
}
function make_thumbnail($path, $path_thumb) {
$thumbnail_width = 300;
$thumbnail_height = 200;
$arr_image_details = getimagesize($path);
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];
if ($original_width > $original_height) {
$new_width = $thumbnail_width;
$new_height = intval($original_height * $new_width / $original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width * $new_height / $original_height);
}
$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);
if ($arr_image_details[2] == 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
}
if ($arr_image_details[2] == 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
}
if ($arr_image_details[2] == 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
}
if ($imgt) {
$old_image = $imgcreatefrom($path);
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
$imgt($new_image, $path_thumb);
}
}
function store_file($target_file, $target_file_small) {
$success = move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file);
make_thumbnail($target_file, $target_file_small);
return $success;
}
if(isset($_POST['submit'])) {
if (empty($_FILES['fileToUpload']['name'])) {
echo file_get_contents("./views/error.php");
exit();
}
$target_dir = 'uploads/';
$target_file_small = '';
$target_file = path_microtime($_FILES['fileToUpload']['name'], $target_file_small);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST['submit'])) {
$check = getimagesize($_FILES['fileToUpload']['tmp_name']);
$uploadOk = ($check !== false);
}
// Check if file already exists
if (file_exists($target_file)) {
$uploadOk = 0;
}
// Check file size
if ($_FILES['fileToUpload']['size'] > 10000000) {
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != 'jpg' && $imageFileType != 'png' && $imageFileType != 'jpeg' ) {
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 1 && store_file($target_file, $target_file_small)) {
echo file_get_contents("./views/success.php");
} else {
echo file_get_contents("./views/error.php");
}
} else {
echo file_get_contents("./views/main.php");
}