-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.php
More file actions
95 lines (95 loc) · 3.19 KB
/
backend.php
File metadata and controls
95 lines (95 loc) · 3.19 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
<?php
session_start();
include_once "dbconnect.php";
$id = $_SESSION['id'];
if(isset($id)){
$sql2 = "SELECT * from gyms where gym_id = '$id'";
$result=$connect->query($sql2);
while($row=$result->fetch_assoc())
{
// old pic data
$old_file = $row['picture'];
}
$picture=time() . '.' . pathinfo($_FILES['picture']['name'], PATHINFO_EXTENSION);
$pic_data=upload($picture);
if($pic_data==1) {
$sql="UPDATE gyms set picture = '$picture' where gym_id='$id'";
if($connect->query($sql)===true) {
echo 1;
}
}
if($pic_data == 3 ){
//incomplatible data type
echo 3 ;
}
if ($pic_data == 2) {
//could not store image
echo 2 ;
}
} //isset condition set
if ($old_file == '') {
// deletion the file which already exists
//unlink('users_thumbs/' . $old_file);
}
else {
//do nothing
unlink('users_thumbs/' . $old_file);
}
function upload($file)
{
if (!empty($file)) {
$target_dir = 'users_images/';
$thumb_dir = 'users_thumbs/';
if (file_exists($target_dir)) {
$target_dir = 'users_images/';
$thumb_dir = 'users_thumbs/';
} else {
mkdir($target_dir, 0777, true);
mkdir($thumb_dir, 0777, true);
}
$target_file = $target_dir . $file;
$FileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if ($FileType == 'png' || $FileType == 'jpg' || $FileType == 'jpeg') {
$upload = 0;
function compress_image($source_url, $destination_url, $quality)
{
$info = getimagesize($source_url);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source_url);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source_url);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source_url);
else
$image = imagecreatefromfile($source_url);
imagejpeg($image, $destination_url, $quality);
$GLOBALS['upload'] = 1;
return $destination_url;
}
$url = $thumb_dir . $file;
$size = filesize($_FILES["picture"]["tmp_name"]) / 1024 / 1024;
if ($size > 2 && $size < 6)
$filename = compress_image($_FILES["picture"]["tmp_name"], $url, 7);
elseif ($size > 6)
$filename = compress_image($_FILES["picture"]["tmp_name"], $url, 5);
elseif ($size > 1 && $size < 2)
$filename = compress_image($_FILES["picture"]["tmp_name"], $url, 8);
elseif ($size > 0.5 && $size < 1)
$filename = compress_image($_FILES["picture"]["tmp_name"], $url, 10);
else
$filename = compress_image($_FILES["picture"]["tmp_name"], $url, 50);
if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file)) {
unlink($target_file);
//unlink('users_thumbs/' . $old_file);
return 1;
} else {
return 2;
}
}
else {
//Incompatible size error
return 3 ;
}
}
}
?>