-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.php
136 lines (102 loc) · 3.5 KB
/
upload.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
<?php
//importing dbDetails file
require_once 'dbconfig.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
//checking the required parameters from the request
if(isset($_FILES['image']['name'])){
//getting file info from the request
$fileinfo = pathinfo($_FILES['image']['name']);
//getting the file extension
$extension = $fileinfo['extension'];
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo 'Unknown image format.';
}
//jpg-jpeg
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
//set new width
$newwidth1=350;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
//new random name
$temp = explode(".", $_FILES["image"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$filename1 = "img/". $newfilename;
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp1);
//insert in database
$insert=mysqli_query($con, "INSERT INTO images (img) VALUES ('$filename1');");
echo "<html>
<head>
</head>
<body>
<meta http-equiv='REFRESH' content='0 ; url=index.php'>
<script>
alert('The image has been uploaded .');
</script>
</body>
</html>";
}
//png
else if($extension=="png")
{
$uploadedfile = $_FILES['image']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
//set new width
$newwidth1=350;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
//new random name
$temp = explode(".", $_FILES["image"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$filename1 = "img/". $newfilename;
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp1);
//insert in database
$insert=mysqli_query($con, "INSERT INTO images (img) VALUES ('$filename1');");
echo "<html>
<head>
</head>
<body>
<meta http-equiv='REFRESH' content='0 ; url=index.php'>
<script>
alert('The image has been uploaded .');
</script>
</body>
</html>";
}
else if($extension=="gif") {
$uploadedfile = $_FILES['image']['tmp_name'];
//new random name
$temp = explode(".", $_FILES["image"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$filename1 = "img/". $newfilename;
move_uploaded_file($uploadedfile,$filename1);
//insert in database
$insert=mysqli_query($con, "INSERT INTO images (img) VALUES ('$filename1');");
echo "<html>
<head>
</head>
<body>
<meta http-equiv='REFRESH' content='0 ; url=index.php'>
<script>
alert('The image has been uploaded .');
</script>
</body>
</html>";
}
else
{
echo 'error';
}
}
}