|
65 | 65 | // Use the file reference (without extension) as the file hash |
66 | 66 | $file_hash = strstr($file_reference_name, '.', true) ?: $file_reference_name; |
67 | 67 |
|
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 |
83 | 69 | if (in_array($file_extension, ['jpg', 'jpeg', 'png', 'gif', 'webp'])) { |
84 | 70 |
|
85 | 71 | // Create image resource based on file extension |
|
121 | 107 | } |
122 | 108 | } |
123 | 109 |
|
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; |
153 | 114 |
|
154 | | - /* --- CREATE OPTIMIZED PREVIEW IMAGE --- */ |
155 | 115 | $preview_max_width = 1200; |
156 | 116 | $preview_max_height = 1200; |
157 | | - $aspect_ratio = $orig_width / $orig_height; |
158 | 117 |
|
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); |
166 | 122 | } 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); |
170 | 125 | } |
171 | 126 |
|
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); |
192 | 136 | } |
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); |
194 | 151 | imagedestroy($src_img); |
195 | 152 |
|
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"; |
197 | 164 | } |
198 | 165 | } |
199 | 166 |
|
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 |
201 | 182 | logAction("File", "Upload", "$session_name uploaded file $file_name", $client_id, $file_id); |
202 | 183 | $_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.'; |
206 | 184 | } |
207 | 185 | } |
208 | | - // Redirect back to the previous page after processing |
| 186 | + |
| 187 | + // Redirect after processing |
209 | 188 | header("Location: " . $_SERVER["HTTP_REFERER"]); |
210 | 189 | exit; |
211 | 190 | } |
212 | 191 |
|
| 192 | + |
213 | 193 | if (isset($_POST['rename_file'])) { |
214 | 194 |
|
215 | 195 | enforceUserPermission('module_support', 2); |
|
0 commit comments