Repository navigation
Expand file tree
/
Copy pathupload.php
More file actions
executable file
·77 lines (64 loc) · 2.23 KB
/
Copy pathupload.php
File metadata and controls
executable file
·77 lines (64 loc) · 2.23 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
<?php
/*
* Custom function to compress image size and
* upload to the server using PHP
*/
function compressImage($source, $destination, $quality) {
// Get image info
$imgInfo = getimagesize($source);
$mime = $imgInfo['mime'];
// Create a new image from file
switch($mime){
case 'image/jpeg':
$image = imagecreatefromjpeg($source);
imagejpeg($image, $destination, $quality);
break;
case 'image/png':
$image = imagecreatefrompng($source);
imagepng($image, $destination, $quality);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
imagegif($image, $destination, $quality);
break;
default:
$image = imagecreatefromjpeg($source);
imagejpeg($image, $destination, $quality);
}
// Return compressed image
return $destination;
}
// File upload path
$uploadPath = "uploads/";
// If file upload form is submitted
$status = $statusMsg = '';
if(isset($_POST["submit"])){
$status = 'error';
if(!empty($_FILES["image"]["name"])) {
// File info
$fileName = basename($_FILES["image"]["name"]);
$imageUploadPath = $uploadPath . $fileName;
$fileType = pathinfo($imageUploadPath, PATHINFO_EXTENSION);
// Allow certain file formats
$allowTypes = array('jpg','png','jpeg','gif');
if(in_array($fileType, $allowTypes)){
// Image temp source
$imageTemp = $_FILES["image"]["tmp_name"];
// Compress size and upload image
$compressedImage = compressImage($imageTemp, $imageUploadPath, 39);
if($compressedImage){
$status = 'success';
$statusMsg = "Image compressed successfully.";
}else{
$statusMsg = "Image compress failed!";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select an image file to upload.';
}
}
// Display status message
echo $statusMsg;
?>