mirror of https://github.com/itflow-org/itflow
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
This commit is contained in:
parent
b803ba4c55
commit
ec8d7a36a8
|
|
@ -322,22 +322,12 @@ while ($folder_id > 0) {
|
|||
$file_size_KB = number_format($file_size / 1024);
|
||||
$file_mime_type = nullable_htmlentities($row['file_mime_type']);
|
||||
$file_uploaded_by = nullable_htmlentities($row['user_name']);
|
||||
$file_has_thumbnail = intval($row['file_has_thumbnail']);
|
||||
$file_has_preview = intval($row['file_has_preview']);
|
||||
$file_thumbnail_source = $file_reference_name;
|
||||
if ($file_has_thumbnail == 1) {
|
||||
$file_thumbnail_source = "thumbnail_$file_reference_name";
|
||||
}
|
||||
$file_preview_source = $file_reference_name;
|
||||
if ($file_has_preview == 1) {
|
||||
$file_preview_source = "preview_$file_reference_name";
|
||||
}
|
||||
|
||||
// Store file data into an array for JS
|
||||
$files[] = [
|
||||
'id' => $file_id,
|
||||
'name' => $file_name,
|
||||
'preview' => "uploads/clients/$client_id/$file_preview_source"
|
||||
'preview' => "uploads/clients/$client_id/$file_reference_name"
|
||||
];
|
||||
|
||||
?>
|
||||
|
|
@ -345,7 +335,7 @@ while ($folder_id > 0) {
|
|||
<div class="col-xl-2 col-lg-2 col-md-6 col-sm-6 mb-3 text-center">
|
||||
|
||||
<a href="#" onclick="openModal(<?php echo count($files)-1; ?>)"><!-- passing the index -->
|
||||
<img class="img-thumbnail" src="<?php echo "uploads/clients/$client_id/$file_thumbnail_source"; ?>" alt="<?php echo $file_reference_name ?>">
|
||||
<img class="img-thumbnail" src="<?php echo "uploads/clients/$client_id/$file_reference_name"; ?>" alt="<?php echo $file_reference_name ?>">
|
||||
</a>
|
||||
|
||||
<div>
|
||||
|
|
@ -356,18 +346,8 @@ while ($folder_id > 0) {
|
|||
</button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/$file_reference_name"; ?>" download="<?php echo $file_name; ?>">
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Original
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download
|
||||
</a>
|
||||
<?php if ($file_has_preview) { ?>
|
||||
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/preview_$file_reference_name"; ?>" download="preview_<?php echo $file_name; ?>">
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Optimized
|
||||
</a>
|
||||
<?php } ?>
|
||||
<?php if ($file_has_thumbnail) { ?>
|
||||
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/thumbnail_$file_reference_name"; ?>" download="thumbnail_<?php echo $file_name; ?>">
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Thumbnail
|
||||
</a>
|
||||
<?php } ?>
|
||||
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#shareModal" onclick="populateShareModal(<?php echo "$client_id, 'File', $file_id"; ?>)">
|
||||
<i class="fas fa-fw fa-share mr-2"></i>Share
|
||||
</a>
|
||||
|
|
@ -493,8 +473,6 @@ while ($folder_id > 0) {
|
|||
$file_mime_type = nullable_htmlentities($row['file_mime_type']);
|
||||
$file_size = intval($row['file_size']);
|
||||
$file_uploaded_by = nullable_htmlentities($row['user_name']);
|
||||
$file_has_thumbnail = intval($row['file_has_thumbnail']);
|
||||
$file_has_preview = intval($row['file_has_preview']);
|
||||
$file_created_at = nullable_htmlentities($row['file_created_at']);
|
||||
$file_folder_id = intval($row['file_folder_id']);
|
||||
|
||||
|
|
@ -573,18 +551,8 @@ while ($folder_id > 0) {
|
|||
</button>
|
||||
<div class="dropdown-menu">
|
||||
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/$file_reference_name"; ?>" download="<?php echo $file_name; ?>">
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Original
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download
|
||||
</a>
|
||||
<?php if ($file_has_preview) { ?>
|
||||
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/preview_$file_reference_name"; ?>" download="preview_<?php echo $file_name; ?>">
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Optimized
|
||||
</a>
|
||||
<?php } ?>
|
||||
<?php if ($file_has_thumbnail) { ?>
|
||||
<a class="dropdown-item" href="<?php echo "uploads/clients/$client_id/thumbnail_$file_reference_name"; ?>" download="thumbnail_<?php echo $file_name; ?>">
|
||||
<i class="fas fa-fw fa-cloud-download-alt mr-2"></i>Download Thumbnail
|
||||
</a>
|
||||
<?php } ?>
|
||||
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#shareModal" onclick="populateShareModal(<?php echo "$client_id, 'File', $file_id"; ?>)">
|
||||
<i class="fas fa-fw fa-share mr-2"></i>Share
|
||||
</a>
|
||||
|
|
|
|||
|
|
@ -3387,10 +3387,19 @@ if (LATEST_DATABASE_VERSION > CURRENT_DATABASE_VERSION) {
|
|||
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.0.8'");
|
||||
}
|
||||
|
||||
// if (CURRENT_DATABASE_VERSION == '2.0.8') {
|
||||
// // Insert queries here required to update to DB version 2.0.9
|
||||
if (CURRENT_DATABASE_VERSION == '2.0.8') {
|
||||
|
||||
mysqli_query($mysqli, "ALTER TABLE `files` DROP `file_has_thumbnail`");
|
||||
mysqli_query($mysqli, "ALTER TABLE `files` DROP `file_has_preview`");
|
||||
mysqli_query($mysqli, "ALTER TABLE `files` DROP `file_asset_id`");
|
||||
|
||||
mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.0.9'");
|
||||
}
|
||||
|
||||
// if (CURRENT_DATABASE_VERSION == '2.0.9') {
|
||||
// // Insert queries here required to update to DB version 2.1.0
|
||||
// // Then, update the database to the next sequential version
|
||||
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.0.9'");
|
||||
// mysqli_query($mysqli, "UPDATE `settings` SET `config_current_database_version` = '2.1.0'");
|
||||
// }
|
||||
|
||||
} else {
|
||||
|
|
|
|||
2
db.sql
2
db.sql
|
|
@ -2494,4 +2494,4 @@ CREATE TABLE `vendors` (
|
|||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- Dump completed on 2025-03-17 14:58:00
|
||||
-- Dump completed on 2025-03-18 16:46:18
|
||||
|
|
|
|||
|
|
@ -5,4 +5,4 @@
|
|||
* It is used in conjunction with database_updates.php
|
||||
*/
|
||||
|
||||
DEFINE("LATEST_DATABASE_VERSION", "2.0.8");
|
||||
DEFINE("LATEST_DATABASE_VERSION", "2.0.9");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
<div class="modal" id="viewFileModal" tabindex="-1">
|
||||
<div class="modal-dialog modal-xl">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content bg-dark text-white">
|
||||
<div class="modal-header">
|
||||
<h6 class="modal-title" id="modalTitle"></h6>
|
||||
|
|
|
|||
|
|
@ -65,21 +65,7 @@ if (isset($_POST['upload_files'])) {
|
|||
// Use the file reference (without extension) as the file hash
|
||||
$file_hash = strstr($file_reference_name, '.', true) ?: $file_reference_name;
|
||||
|
||||
// Insert file metadata into the database
|
||||
$query = "INSERT INTO files SET
|
||||
file_reference_name = '$file_reference_name',
|
||||
file_name = '$file_name',
|
||||
file_description = '$description',
|
||||
file_ext = '$file_extension',
|
||||
file_mime_type = '$file_mime_type',
|
||||
file_size = $file_size,
|
||||
file_created_by = $session_user_id,
|
||||
file_folder_id = $folder_id,
|
||||
file_client_id = $client_id";
|
||||
mysqli_query($mysqli, $query);
|
||||
$file_id = mysqli_insert_id($mysqli);
|
||||
|
||||
// If the file is an image, create a thumbnail and an optimized preview
|
||||
// If the file is an image, optimize it
|
||||
if (in_array($file_extension, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) {
|
||||
|
||||
// Create image resource based on file extension
|
||||
|
|
@ -121,95 +107,89 @@ if (isset($_POST['upload_files'])) {
|
|||
}
|
||||
}
|
||||
|
||||
// Get original image dimensions
|
||||
list($orig_width, $orig_height) = getimagesize($dest_path);
|
||||
|
||||
/* --- CREATE THUMBNAIL --- */
|
||||
$thumbnail_width = 200;
|
||||
$thumbnail_height = 200;
|
||||
$thumb_img = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
|
||||
imagecopyresampled($thumb_img, $src_img, 0, 0, 0, 0,
|
||||
$thumbnail_width, $thumbnail_height, $orig_width, $orig_height);
|
||||
$thumbnail_file_name = 'thumbnail_' . $file_reference_name;
|
||||
$thumb_path = $upload_file_dir . $thumbnail_file_name;
|
||||
|
||||
switch ($file_extension) {
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
imagejpeg($thumb_img, $thumb_path, 80);
|
||||
break;
|
||||
case 'png':
|
||||
imagepng($thumb_img, $thumb_path);
|
||||
break;
|
||||
case 'gif':
|
||||
imagegif($thumb_img, $thumb_path);
|
||||
break;
|
||||
case 'webp':
|
||||
imagewebp($thumb_img, $thumb_path);
|
||||
break;
|
||||
}
|
||||
imagedestroy($thumb_img);
|
||||
mysqli_query($mysqli, "UPDATE files SET file_has_thumbnail = 1 WHERE file_id = $file_id");
|
||||
|
||||
/* --- CREATE OPTIMIZED PREVIEW IMAGE --- */
|
||||
$preview_max_width = 1200;
|
||||
$preview_max_height = 1200;
|
||||
// Get image dimensions
|
||||
$orig_width = imagesx($src_img);
|
||||
$orig_height = imagesy($src_img);
|
||||
$aspect_ratio = $orig_width / $orig_height;
|
||||
|
||||
if ($orig_width <= $preview_max_width && $orig_height <= $preview_max_height) {
|
||||
$preview_new_width = $orig_width;
|
||||
$preview_new_height = $orig_height;
|
||||
} elseif ($aspect_ratio > 1) {
|
||||
// Wider than tall
|
||||
$preview_new_width = $preview_max_width;
|
||||
$preview_new_height = (int)($preview_max_width / $aspect_ratio);
|
||||
$preview_max_width = 1200;
|
||||
$preview_max_height = 1200;
|
||||
|
||||
// Maintain aspect ratio
|
||||
if ($orig_width > $orig_height) {
|
||||
$preview_new_width = min($preview_max_width, $orig_width);
|
||||
$preview_new_height = round($preview_new_width / $aspect_ratio);
|
||||
} else {
|
||||
// Taller or square
|
||||
$preview_new_height = $preview_max_height;
|
||||
$preview_new_width = (int)($preview_max_height * $aspect_ratio);
|
||||
$preview_new_height = min($preview_max_height, $orig_height);
|
||||
$preview_new_width = round($preview_new_height * $aspect_ratio);
|
||||
}
|
||||
|
||||
$preview_img = imagecreatetruecolor($preview_new_width, $preview_new_height);
|
||||
imagecopyresampled($preview_img, $src_img, 0, 0, 0, 0,
|
||||
// Create optimized image
|
||||
$optimized_img = imagecreatetruecolor($preview_new_width, $preview_new_height);
|
||||
|
||||
// Handle transparency for PNG & GIF
|
||||
if (in_array($file_extension, ['png', 'gif'])) {
|
||||
imagealphablending($optimized_img, false);
|
||||
imagesavealpha($optimized_img, true);
|
||||
$transparent = imagecolorallocatealpha($optimized_img, 0, 0, 0, 127);
|
||||
imagefilledrectangle($optimized_img, 0, 0, $preview_new_width, $preview_new_height, $transparent);
|
||||
}
|
||||
|
||||
// Resize image
|
||||
imagecopyresampled($optimized_img, $src_img, 0, 0, 0, 0,
|
||||
$preview_new_width, $preview_new_height, $orig_width, $orig_height);
|
||||
$preview_file_name = 'preview_' . $file_reference_name;
|
||||
$preview_path = $upload_file_dir . $preview_file_name;
|
||||
|
||||
switch ($file_extension) {
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
imagejpeg($preview_img, $preview_path, 70);
|
||||
break;
|
||||
case 'png':
|
||||
imagepng($preview_img, $preview_path, 7);
|
||||
break;
|
||||
case 'gif':
|
||||
imagegif($preview_img, $preview_path);
|
||||
break;
|
||||
case 'webp':
|
||||
imagewebp($preview_img, $preview_path, 70);
|
||||
break;
|
||||
}
|
||||
imagedestroy($preview_img);
|
||||
// Define WebP file path
|
||||
$optimized_file_name = $file_hash . ".webp";
|
||||
$optimized_path = $upload_file_dir . $optimized_file_name;
|
||||
|
||||
// Save as WebP
|
||||
imagewebp($optimized_img, $optimized_path, 80);
|
||||
|
||||
// Free memory
|
||||
imagedestroy($optimized_img);
|
||||
imagedestroy($src_img);
|
||||
|
||||
mysqli_query($mysqli, "UPDATE files SET file_has_preview = 1 WHERE file_id = $file_id");
|
||||
// Delete original uploaded image
|
||||
unlink($dest_path);
|
||||
|
||||
// Get new file size
|
||||
$file_size = filesize($optimized_path);
|
||||
|
||||
// Update details for WebP
|
||||
$file_reference_name = $optimized_file_name;
|
||||
$file_extension = "webp";
|
||||
$file_mime_type = "image/webp";
|
||||
$file_name = pathinfo($originalName, PATHINFO_FILENAME) . ".webp";
|
||||
}
|
||||
}
|
||||
|
||||
// Log the file upload action
|
||||
// Insert file metadata into the database
|
||||
$query = "INSERT INTO files SET
|
||||
file_reference_name = '$file_reference_name',
|
||||
file_name = '$file_name',
|
||||
file_description = '$description',
|
||||
file_ext = '$file_extension',
|
||||
file_mime_type = '$file_mime_type',
|
||||
file_size = $file_size,
|
||||
file_created_by = $session_user_id,
|
||||
file_folder_id = $folder_id,
|
||||
file_client_id = $client_id";
|
||||
mysqli_query($mysqli, $query);
|
||||
$file_id = mysqli_insert_id($mysqli);
|
||||
|
||||
// Log upload action
|
||||
logAction("File", "Upload", "$session_name uploaded file $file_name", $client_id, $file_id);
|
||||
$_SESSION['alert_message'] = "Uploaded file <strong>$file_name</strong>";
|
||||
} else {
|
||||
$_SESSION['alert_type'] = 'error';
|
||||
$_SESSION['alert_message'] = 'There was an error processing the file upload. Please ensure the upload directory is writable by the web server.';
|
||||
}
|
||||
}
|
||||
// Redirect back to the previous page after processing
|
||||
|
||||
// Redirect after processing
|
||||
header("Location: " . $_SERVER["HTTP_REFERER"]);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
if (isset($_POST['rename_file'])) {
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
|
|
|||
Loading…
Reference in New Issue