-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathproject-photo-functions.php
310 lines (246 loc) · 9.99 KB
/
project-photo-functions.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
//FUNCTIONS
function handleFileUploadError($errorCode, $index) {
$isRequired = ($index == 1); // Assuming photo1_main is index 1
$errorType = "Photo " . $index;
if (!$isRequired) {
$errorType .= " (optional)"; // Marking the photo as optional in the error message
}
switch ($errorCode) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
return "{$errorType} exceeds the maximum file size allowed.<br>";
case UPLOAD_ERR_PARTIAL:
return "{$errorType} was only partially uploaded.<br>";
case UPLOAD_ERR_NO_FILE:
// Only report missing file for required photo
if ($isRequired) {
return "{$errorType} was not uploaded but is required.<br>";
}
break; // Optionally, you could ignore this error for optional photos
case UPLOAD_ERR_NO_TMP_DIR:
return "Missing a temporary folder on server.<br>";
case UPLOAD_ERR_CANT_WRITE:
return "{$errorType} could not be written to disk.<br>";
case UPLOAD_ERR_EXTENSION:
return "A PHP extension stopped the upload of {$errorType}.<br>";
default:
return "An unknown error occurred with {$errorType}.<br>";
}
}
// Function to create a thumbnail using GD library
function createThumbnail($source_path, $destination_path, $width, $height, $quality) {
list($source_width, $source_height, $source_type) = getimagesize($source_path);
switch ($source_type) {
case IMAGETYPE_JPEG:
$source_image = imagecreatefromjpeg($source_path);
break;
case IMAGETYPE_PNG:
$source_image = imagecreatefrompng($source_path);
break;
case IMAGETYPE_WEBP:
$source_image = imagecreatefromwebp($source_path);
break;
default:
return false;
}
$thumbnail = imagecreatetruecolor($width, $height);
imagecopyresampled($thumbnail, $source_image, 0, 0, 0, 0, $width, $height, $source_width, $source_height);
imagedestroy($source_image);
imagejpeg($thumbnail, $destination_path, $quality);
imagedestroy($thumbnail);
return true;
}
// Function to create a thumbnail with height 200px while maintaining aspect ratio
function createTrainingThumbnail($source_path, $destination_path, $target_height, $quality) {
list($source_width, $source_height, $source_type) = getimagesize($source_path);
switch ($source_type) {
case IMAGETYPE_JPEG:
$source_image = imagecreatefromjpeg($source_path);
break;
case IMAGETYPE_PNG:
$source_image = imagecreatefrompng($source_path);
break;
case IMAGETYPE_WEBP:
$source_image = imagecreatefromwebp($source_path);
break;
default:
echo "<script>console.log('Unsupported image type for $source_path');</script>";
return false;
}
$aspect_ratio = $source_width / $source_height;
$target_width = intval($target_height * $aspect_ratio); // Cast to integer
$thumbnail = imagecreatetruecolor($target_width, intval($target_height)); // Cast to integer
if (imagecopyresampled($thumbnail, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height)) {
if (imagewebp($thumbnail, $destination_path, $quality)) {
imagedestroy($source_image);
imagedestroy($thumbnail);
return true;
} else {
echo "<script>console.log('Failed to save thumbnail to $destination_path');</script>";
}
} else {
echo "<script>console.log('Failed to create thumbnail for $source_path');</script>";
}
imagedestroy($source_image);
imagedestroy($thumbnail);
return false;
}
// Function to resize original image if any of its dimensions are larger than 1500px.
function resizeAndConvertToWebP($sourcePath, $targetPath, $maxDim, $compressionQuality) {
// First check if the source is already a webp and simply copy if it is
$fileType = strtolower(pathinfo($sourcePath, PATHINFO_EXTENSION));
if ($fileType === 'webp') {
// If already webp and no resizing is needed, just copy the file over.
if (!file_exists($targetPath)) {
copy($sourcePath, $targetPath);
}
return true;
}
// Correct orientation based on EXIF data for JPEGs only
if ($fileType === 'jpeg' || $fileType === 'jpg') {
correctImageOrientation($sourcePath);
}
list($width, $height, $type, $attr) = getimagesize($sourcePath);
$scale = min($maxDim / $width, $maxDim / $height);
$newWidth = $width > $maxDim ? ceil($scale * $width) : $width;
$newHeight = $height > $maxDim ? ceil($scale * $height) : $height;
$src = null;
switch ($type) {
case IMAGETYPE_JPEG:
$src = imagecreatefromjpeg($sourcePath);
break;
case IMAGETYPE_PNG:
$src = imagecreatefrompng($sourcePath);
break;
default:
// Optionally handle other image types or skip unsupported ones
return false;
}
if ($src) {
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagewebp($dst, $targetPath, $compressionQuality); // Save the image as WebP
imagedestroy($src);
imagedestroy($dst);
}
return true;
}
// Function to resize original image to 1020px width and convert to WebP format
function resizeAndConvertTrainingToWebP($sourcePath, $targetPath, $maxWidth, $compressionQuality) {
$fileType = strtolower(pathinfo($sourcePath, PATHINFO_EXTENSION));
// Correct orientation based on EXIF data for JPEGs only
if ($fileType === 'jpeg' || $fileType === 'jpg') {
correctImageOrientation($sourcePath);
}
list($width, $height, $type) = getimagesize($sourcePath);
// Calculate new dimensions while maintaining aspect ratio
if ($width > $maxWidth) {
$newWidth = $maxWidth;
$newHeight = intval(($height / $width) * $newWidth);
} else {
$newWidth = $width;
$newHeight = $height;
}
// Initialize image source
$src = null;
switch ($type) {
case IMAGETYPE_JPEG:
$src = imagecreatefromjpeg($sourcePath);
break;
case IMAGETYPE_PNG:
$src = imagecreatefrompng($sourcePath);
break;
default:
echo "<script>console.log('Unsupported image type');</script>";
return false;
}
if ($src) {
// Create a true color image for the destination
$dst = imagecreatetruecolor($newWidth, $newHeight);
imagealphablending($dst, false);
imagesavealpha($dst, true);
// Copy and resize the image
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Save the resized image as WebP
if (imagewebp($dst, $targetPath, $compressionQuality)) {
echo "<script>console.log('Image resized to: {$newWidth}px x {$newHeight}px and saved to {$targetPath}');</script>";
} else {
echo "<script>console.log('Failed to save image as WebP');</script>";
imagedestroy($src);
imagedestroy($dst);
return false;
}
// Free up memory
imagedestroy($src);
imagedestroy($dst);
// Verify the resized image
list($newWidthVerified, $newHeightVerified) = getimagesize($targetPath);
echo "<script>console.log('Verified resized dimensions: {$newWidthVerified}px x {$newHeightVerified}px');</script>";
} else {
echo "<script>console.log('Failed to create image resource');</script>";
return false;
}
return true;
}
function correctImageOrientation($filepath) {
$fileType = strtolower(pathinfo($filepath, PATHINFO_EXTENSION));
if ($fileType !== 'jpeg' && $fileType !== 'jpg') {
// Skip EXIF processing for non-JPEG files
return;
}
$exif = @exif_read_data($filepath); // Using error control operator to suppress errors
if (!empty($exif['Orientation'])) {
$image = imagecreatefromjpeg($filepath);
if (!$image) return; // Ensure the image was successfully created
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
imagejpeg($image, $filepath, 90); // Save corrected image
imagedestroy($image);
}
}
function deleteProject($projectId, $conn) {
if ($conn->connect_error) {
return "Connection failed: " . $conn->connect_error;
}
$deleteStmt = $conn->prepare("DELETE FROM tb_projects WHERE project_id = ?");
$deleteStmt->bind_param("i", $projectId);
if (!$deleteStmt->execute()) {
return "Error deleting project: " . $deleteStmt->error;
}
$deleteStmt->close();
return true;
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST['action'] == 'delete_training') {
$training_id = $_POST['training_id'];
$deleteResult = deleteTraining($training_id, $conn);
if ($deleteResult === true) {
echo "<script>alert('Training has been successfully deleted.'); window.location.href='add-training.php';</script>";
exit;
} else {
echo "<script>alert('" . $deleteResult . "');</script>";
exit;
}
}
function deleteTraining($trainingId, $conn) {
if ($conn->connect_error) {
return "Connection failed: " . $conn->connect_error;
}
$deleteStmt = $conn->prepare("DELETE FROM tb_trainings WHERE training_id = ?");
$deleteStmt->bind_param("i", $trainingId);
if (!$deleteStmt->execute()) {
return "Error deleting training: " . $deleteStmt->error;
}
$deleteStmt->close();
return true;
}
?>