Skip to content

Commit ec8d7a3

Browse files
committed
Only store optimized WebP images, so only 1 image is stored and used for thumnails and viewing, removed fields has_preview and has_thumbnail as these are no longer needed, fixed issue when optimizing portrait images the bottom half would get cropped out
1 parent b803ba4 commit ec8d7a3

File tree

6 files changed

+82
-125
lines changed

6 files changed

+82
-125
lines changed

client_files.php

+4-36
Original file line numberDiff line numberDiff line change
@@ -322,30 +322,20 @@ function display_folders($parent_folder_id, $client_id, $indent = 0) {
322322
$file_size_KB = number_format($file_size / 1024);
323323
$file_mime_type = nullable_htmlentities($row['file_mime_type']);
324324
$file_uploaded_by = nullable_htmlentities($row['user_name']);
325-
$file_has_thumbnail = intval($row['file_has_thumbnail']);
326-
$file_has_preview = intval($row['file_has_preview']);
327-
$file_thumbnail_source = $file_reference_name;
328-
if ($file_has_thumbnail == 1) {
329-
$file_thumbnail_source = "thumbnail_$file_reference_name";
330-
}
331-
$file_preview_source = $file_reference_name;
332-
if ($file_has_preview == 1) {
333-
$file_preview_source = "preview_$file_reference_name";
334-
}
335325

336326
// Store file data into an array for JS
337327
$files[] = [
338328
'id' => $file_id,
339329
'name' => $file_name,
340-
'preview' => "uploads/clients/$client_id/$file_preview_source"
330+
'preview' => "uploads/clients/$client_id/$file_reference_name"
341331
];
342332

343333
?>
344334

345335
<div class="col-xl-2 col-lg-2 col-md-6 col-sm-6 mb-3 text-center">
346336

347337
<a href="#" onclick="openModal(<?php echo count($files)-1; ?>)"><!-- passing the index -->
348-
<img class="img-thumbnail" src="<?php echo "uploads/clients/$client_id/$file_thumbnail_source"; ?>" alt="<?php echo $file_reference_name ?>">
338+
<img class="img-thumbnail" src="<?php echo "uploads/clients/$client_id/$file_reference_name"; ?>" alt="<?php echo $file_reference_name ?>">
349339
</a>
350340

351341
<div>
@@ -356,18 +346,8 @@ function display_folders($parent_folder_id, $client_id, $indent = 0) {
356346
</button>
357347
<div class="dropdown-menu">
358348
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/$file_reference_name"; ?>" download="<?php echo $file_name; ?>">
359-
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Original
360-
</a>
361-
<?php if ($file_has_preview) { ?>
362-
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/preview_$file_reference_name"; ?>" download="preview_<?php echo $file_name; ?>">
363-
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Optimized
364-
</a>
365-
<?php } ?>
366-
<?php if ($file_has_thumbnail) { ?>
367-
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/thumbnail_$file_reference_name"; ?>" download="thumbnail_<?php echo $file_name; ?>">
368-
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Thumbnail
349+
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download
369350
</a>
370-
<?php } ?>
371351
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#shareModal" onclick="populateShareModal(<?php echo "$client_id, 'File', $file_id"; ?>)">
372352
<i class="fas fa-fw fa-share mr-2"></i>Share
373353
</a>
@@ -493,8 +473,6 @@ function display_folders($parent_folder_id, $client_id, $indent = 0) {
493473
$file_mime_type = nullable_htmlentities($row['file_mime_type']);
494474
$file_size = intval($row['file_size']);
495475
$file_uploaded_by = nullable_htmlentities($row['user_name']);
496-
$file_has_thumbnail = intval($row['file_has_thumbnail']);
497-
$file_has_preview = intval($row['file_has_preview']);
498476
$file_created_at = nullable_htmlentities($row['file_created_at']);
499477
$file_folder_id = intval($row['file_folder_id']);
500478

@@ -573,18 +551,8 @@ function display_folders($parent_folder_id, $client_id, $indent = 0) {
573551
</button>
574552
<div class="dropdown-menu">
575553
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/$file_reference_name"; ?>" download="<?php echo $file_name; ?>">
576-
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Original
577-
</a>
578-
<?php if ($file_has_preview) { ?>
579-
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/preview_$file_reference_name"; ?>" download="preview_<?php echo $file_name; ?>">
580-
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Optimized
554+
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download
581555
</a>
582-
<?php } ?>
583-
<?php if ($file_has_thumbnail) { ?>
584-
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/thumbnail_$file_reference_name"; ?>" download="thumbnail_<?php echo $file_name; ?>">
585-
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Thumbnail
586-
</a>
587-
<?php } ?>
588556
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#shareModal" onclick="populateShareModal(<?php echo "$client_id, 'File', $file_id"; ?>)">
589557
<i class="fas fa-fw fa-share mr-2"></i>Share
590558
</a>

database_updates.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -3387,10 +3387,19 @@ function processFile($file_path, $file_name, $mysqli) {
33873387
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.0.8'");
33883388
}
33893389

3390-
// if (CURRENT_DATABASE_VERSION == '2.0.8') {
3391-
// // Insert queries here required to update to DB version 2.0.9
3390+
if (CURRENT_DATABASE_VERSION == '2.0.8') {
3391+
3392+
mysqli_query($mysqli, "ALTER TABLE `files` DROP `file_has_thumbnail`");
3393+
mysqli_query($mysqli, "ALTER TABLE `files` DROP `file_has_preview`");
3394+
mysqli_query($mysqli, "ALTER TABLE `files` DROP `file_asset_id`");
3395+
3396+
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.0.9'");
3397+
}
3398+
3399+
// if (CURRENT_DATABASE_VERSION == '2.0.9') {
3400+
// // Insert queries here required to update to DB version 2.1.0
33923401
// // Then, update the database to the next sequential version
3393-
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.0.9'");
3402+
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.1.0'");
33943403
// }
33953404

33963405
} else {

db.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -2494,4 +2494,4 @@ CREATE TABLE `vendors` (
24942494
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
24952495
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
24962496

2497-
-- Dump completed on 2025-03-17 14:58:00
2497+
-- Dump completed on 2025-03-18 16:46:18

includes/database_version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* It is used in conjunction with database_updates.php
66
*/
77

8-
DEFINE("LATEST_DATABASE_VERSION", "2.0.8");
8+
DEFINE("LATEST_DATABASE_VERSION", "2.0.9");

modals/client_file_view_modal.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="modal" id="viewFileModal" tabindex="-1">
2-
<div class="modal-dialog modal-xl">
2+
<div class="modal-dialog modal-lg">
33
<div class="modal-content bg-dark text-white">
44
<div class="modal-header">
55
<h6 class="modal-title" id="modalTitle"></h6>

post/user/file.php

+63-83
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,7 @@
6565
// Use the file reference (without extension) as the file hash
6666
$file_hash = strstr($file_reference_name, '.', true) ?: $file_reference_name;
6767

68-
// Insert file metadata into the database
69-
$query = "INSERT INTO files SET
70-
file_reference_name = '$file_reference_name',
71-
file_name = '$file_name',
72-
file_description = '$description',
73-
file_ext = '$file_extension',
74-
file_mime_type = '$file_mime_type',
75-
file_size = $file_size,
76-
file_created_by = $session_user_id,
77-
file_folder_id = $folder_id,
78-
file_client_id = $client_id";
79-
mysqli_query($mysqli, $query);
80-
$file_id = mysqli_insert_id($mysqli);
81-
82-
// If the file is an image, create a thumbnail and an optimized preview
68+
// If the file is an image, optimize it
8369
if (in_array($file_extension, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
8470

8571
// Create image resource based on file extension
@@ -121,95 +107,89 @@
121107
}
122108
}
123109

124-
// Get original image dimensions
125-
list($orig_width, $orig_height) = getimagesize($dest_path);
126-
127-
/* --- CREATE THUMBNAIL --- */
128-
$thumbnail_width = 200;
129-
$thumbnail_height = 200;
130-
$thumb_img = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
131-
imagecopyresampled($thumb_img, $src_img, 0, 0, 0, 0,
132-
$thumbnail_width, $thumbnail_height, $orig_width, $orig_height);
133-
$thumbnail_file_name = 'thumbnail_' . $file_reference_name;
134-
$thumb_path = $upload_file_dir . $thumbnail_file_name;
135-
136-
switch ($file_extension) {
137-
case 'jpg':
138-
case 'jpeg':
139-
imagejpeg($thumb_img, $thumb_path, 80);
140-
break;
141-
case 'png':
142-
imagepng($thumb_img, $thumb_path);
143-
break;
144-
case 'gif':
145-
imagegif($thumb_img, $thumb_path);
146-
break;
147-
case 'webp':
148-
imagewebp($thumb_img, $thumb_path);
149-
break;
150-
}
151-
imagedestroy($thumb_img);
152-
mysqli_query($mysqli, "UPDATE files SET file_has_thumbnail = 1 WHERE file_id = $file_id");
110+
// Get image dimensions
111+
$orig_width = imagesx($src_img);
112+
$orig_height = imagesy($src_img);
113+
$aspect_ratio = $orig_width / $orig_height;
153114

154-
/* --- CREATE OPTIMIZED PREVIEW IMAGE --- */
155115
$preview_max_width = 1200;
156116
$preview_max_height = 1200;
157-
$aspect_ratio = $orig_width / $orig_height;
158117

159-
if ($orig_width <= $preview_max_width && $orig_height <= $preview_max_height) {
160-
$preview_new_width = $orig_width;
161-
$preview_new_height = $orig_height;
162-
} elseif ($aspect_ratio > 1) {
163-
// Wider than tall
164-
$preview_new_width = $preview_max_width;
165-
$preview_new_height = (int)($preview_max_width / $aspect_ratio);
118+
// Maintain aspect ratio
119+
if ($orig_width > $orig_height) {
120+
$preview_new_width = min($preview_max_width, $orig_width);
121+
$preview_new_height = round($preview_new_width / $aspect_ratio);
166122
} else {
167-
// Taller or square
168-
$preview_new_height = $preview_max_height;
169-
$preview_new_width = (int)($preview_max_height * $aspect_ratio);
123+
$preview_new_height = min($preview_max_height, $orig_height);
124+
$preview_new_width = round($preview_new_height * $aspect_ratio);
170125
}
171126

172-
$preview_img = imagecreatetruecolor($preview_new_width, $preview_new_height);
173-
imagecopyresampled($preview_img, $src_img, 0, 0, 0, 0,
174-
$preview_new_width, $preview_new_height, $orig_width, $orig_height);
175-
$preview_file_name = 'preview_' . $file_reference_name;
176-
$preview_path = $upload_file_dir . $preview_file_name;
177-
178-
switch ($file_extension) {
179-
case 'jpg':
180-
case 'jpeg':
181-
imagejpeg($preview_img, $preview_path, 70);
182-
break;
183-
case 'png':
184-
imagepng($preview_img, $preview_path, 7);
185-
break;
186-
case 'gif':
187-
imagegif($preview_img, $preview_path);
188-
break;
189-
case 'webp':
190-
imagewebp($preview_img, $preview_path, 70);
191-
break;
127+
// Create optimized image
128+
$optimized_img = imagecreatetruecolor($preview_new_width, $preview_new_height);
129+
130+
// Handle transparency for PNG & GIF
131+
if (in_array($file_extension, ['png', 'gif'])) {
132+
imagealphablending($optimized_img, false);
133+
imagesavealpha($optimized_img, true);
134+
$transparent = imagecolorallocatealpha($optimized_img, 0, 0, 0, 127);
135+
imagefilledrectangle($optimized_img, 0, 0, $preview_new_width, $preview_new_height, $transparent);
192136
}
193-
imagedestroy($preview_img);
137+
138+
// Resize image
139+
imagecopyresampled($optimized_img, $src_img, 0, 0, 0, 0,
140+
$preview_new_width, $preview_new_height, $orig_width, $orig_height);
141+
142+
// Define WebP file path
143+
$optimized_file_name = $file_hash . ".webp";
144+
$optimized_path = $upload_file_dir . $optimized_file_name;
145+
146+
// Save as WebP
147+
imagewebp($optimized_img, $optimized_path, 80);
148+
149+
// Free memory
150+
imagedestroy($optimized_img);
194151
imagedestroy($src_img);
195152

196-
mysqli_query($mysqli, "UPDATE files SET file_has_preview = 1 WHERE file_id = $file_id");
153+
// Delete original uploaded image
154+
unlink($dest_path);
155+
156+
// Get new file size
157+
$file_size = filesize($optimized_path);
158+
159+
// Update details for WebP
160+
$file_reference_name = $optimized_file_name;
161+
$file_extension = "webp";
162+
$file_mime_type = "image/webp";
163+
$file_name = pathinfo($originalName, PATHINFO_FILENAME) . ".webp";
197164
}
198165
}
199166

200-
// Log the file upload action
167+
// Insert file metadata into the database
168+
$query = "INSERT INTO files SET
169+
file_reference_name = '$file_reference_name',
170+
file_name = '$file_name',
171+
file_description = '$description',
172+
file_ext = '$file_extension',
173+
file_mime_type = '$file_mime_type',
174+
file_size = $file_size,
175+
file_created_by = $session_user_id,
176+
file_folder_id = $folder_id,
177+
file_client_id = $client_id";
178+
mysqli_query($mysqli, $query);
179+
$file_id = mysqli_insert_id($mysqli);
180+
181+
// Log upload action
201182
logAction("File", "Upload", "$session_name uploaded file $file_name", $client_id, $file_id);
202183
$_SESSION['alert_message'] = "Uploaded file <strong>$file_name</strong>";
203-
} else {
204-
$_SESSION['alert_type'] = 'error';
205-
$_SESSION['alert_message'] = 'There was an error processing the file upload. Please ensure the upload directory is writable by the web server.';
206184
}
207185
}
208-
// Redirect back to the previous page after processing
186+
187+
// Redirect after processing
209188
header("Location: " . $_SERVER["HTTP_REFERER"]);
210189
exit;
211190
}
212191

192+
213193
if (isset($_POST['rename_file'])) {
214194

215195
enforceUserPermission('module_support', 2);

0 commit comments

Comments
 (0)