-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-handler.php
More file actions
232 lines (202 loc) · 8.26 KB
/
Copy pathupload-handler.php
File metadata and controls
232 lines (202 loc) · 8.26 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Enhanced File Upload Handler
* Features:
* - Auto-compress images to 300-400 KB
* - Organize files by date in /uploads/application/[YYYY-MM-DD]/
* - Support both regular file uploads and webcam captures
*/
if (!function_exists('compressImage')) {
/**
* Compress image to target size (300-400 KB)
* @param string $source - Source image path
* @param string $destination - Destination path
* @param int $targetSize - Target file size in KB (default 350)
* @return bool - Success status
*/
function compressImage($source, $destination, $targetSize = 350) {
// Get image info
$info = getimagesize($source);
if ($info === false) {
return false;
}
$mime = $info['mime'];
// Create image resource based on type
switch ($mime) {
case 'image/jpeg':
case 'image/jpg':
$image = imagecreatefromjpeg($source);
break;
case 'image/png':
$image = imagecreatefrompng($source);
break;
case 'image/gif':
$image = imagecreatefromgif($source);
break;
default:
return false;
}
if (!$image) {
return false;
}
// Get original dimensions
$width = imagesx($image);
$height = imagesy($image);
// Start with quality 85
$quality = 85;
$targetSizeBytes = $targetSize * 1024; // Convert KB to bytes
// Try to compress to target size
$attempts = 0;
$maxAttempts = 10;
while ($attempts < $maxAttempts) {
// Save with current quality
ob_start();
imagejpeg($image, null, $quality);
$imageData = ob_get_clean();
$currentSize = strlen($imageData);
// Check if we're in target range (300-400 KB)
if ($currentSize >= 300 * 1024 && $currentSize <= 400 * 1024) {
// Perfect size, save and exit
file_put_contents($destination, $imageData);
imagedestroy($image);
return true;
}
// If too large, reduce quality
if ($currentSize > $targetSizeBytes) {
$quality -= 5;
if ($quality < 20) {
// Quality too low, try resizing image
$scaleFactor = sqrt($targetSizeBytes / $currentSize);
$newWidth = (int)($width * $scaleFactor);
$newHeight = (int)($height * $scaleFactor);
$resized = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($resized, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagedestroy($image);
$image = $resized;
$width = $newWidth;
$height = $newHeight;
$quality = 75; // Reset quality for resized image
}
} else {
// Size is good enough, save it
file_put_contents($destination, $imageData);
imagedestroy($image);
return true;
}
$attempts++;
}
// If we couldn't hit exact target, save with best quality we have
file_put_contents($destination, $imageData);
imagedestroy($image);
return true;
}
}
if (!function_exists('uploadApplicationFile')) {
/**
* Upload and process application file
* @param array $file - $_FILES array element OR null for webcam
* @param string $webcamData - Base64 webcam image data OR null
* @param string $serialNo - Serial number for filename
* @return string|false - Relative path to uploaded file or false on failure
*/
function uploadApplicationFile($file = null, $webcamData = null, $serialNo = '') {
// Create base directory structure
$baseDir = "uploads/application/";
$dateFolder = date('Y-m-d'); // Format: 2026-02-08
$targetDir = $baseDir . $dateFolder . "/";
// Create directories if they don't exist
if (!is_dir($baseDir)) {
mkdir($baseDir, 0755, true);
}
if (!is_dir($targetDir)) {
mkdir($targetDir, 0755, true);
}
$uploadPath = "";
$tempFile = "";
try {
// Handle webcam image
if (!empty($webcamData)) {
// Parse base64 data
$parts = explode(";", $webcamData);
if (count($parts) > 1) {
$dataParts = explode(",", $parts[1]);
if (count($dataParts) > 1) {
$decoded = base64_decode($dataParts[1]);
// Create temporary file
$tempFile = sys_get_temp_dir() . '/' . uniqid() . '_temp.jpg';
file_put_contents($tempFile, $decoded);
// Final filename - sirf serial number
$filename = $serialNo . ".jpg";
$finalPath = $targetDir . $filename;
// Compress and save
if (compressImage($tempFile, $finalPath, 350)) {
$uploadPath = $finalPath;
}
// Clean up temp file
if (file_exists($tempFile)) {
unlink($tempFile);
}
}
}
}
// Handle regular file upload
elseif (isset($file) && $file['error'] == 0) {
$ext = strtolower(pathinfo($file["name"], PATHINFO_EXTENSION));
$allowed = array('jpg', 'jpeg', 'png', 'gif', 'pdf');
if (!in_array($ext, $allowed)) {
return false;
}
// Check file size (max 10MB before compression)
if ($file["size"] > 10000000) {
return false;
}
// Generate filename - sirf serial number
$filename = $serialNo . "." . $ext;
$finalPath = $targetDir . $filename;
// For images, compress them
if (in_array($ext, array('jpg', 'jpeg', 'png', 'gif'))) {
// Move to temp location first
$tempFile = sys_get_temp_dir() . '/' . uniqid() . '_temp.' . $ext;
if (move_uploaded_file($file["tmp_name"], $tempFile)) {
// Compress and save
if (compressImage($tempFile, $finalPath, 350)) {
$uploadPath = $finalPath;
}
// Clean up temp file
if (file_exists($tempFile)) {
unlink($tempFile);
}
}
} else {
// For PDFs, just move without compression
if (move_uploaded_file($file["tmp_name"], $finalPath)) {
$uploadPath = $finalPath;
}
}
}
return $uploadPath ? $uploadPath : false;
} catch (Exception $e) {
// Clean up temp file on error
if (!empty($tempFile) && file_exists($tempFile)) {
unlink($tempFile);
}
error_log("Upload error: " . $e->getMessage());
return false;
}
}
}
/**
* Get file size in human-readable format
*/
if (!function_exists('formatFileSize')) {
function formatFileSize($bytes) {
if ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} else {
return $bytes . ' bytes';
}
}
}
?>