Feature: Add trip publisher system - create, edit, delete, and publish trips
This commit is contained in:
@@ -2810,3 +2810,95 @@ function url($page) {
|
||||
return '/' . $page . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize image by resizing if it exceeds max dimensions
|
||||
*
|
||||
* @param string $filePath Path to the image file
|
||||
* @param int $maxWidth Maximum width in pixels
|
||||
* @param int $maxHeight Maximum height in pixels
|
||||
* @return bool Success status
|
||||
*/
|
||||
function optimizeImage($filePath, $maxWidth = 1920, $maxHeight = 1080)
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get image info
|
||||
$imageInfo = getimagesize($filePath);
|
||||
if (!$imageInfo) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$width = $imageInfo[0];
|
||||
$height = $imageInfo[1];
|
||||
$mime = $imageInfo['mime'];
|
||||
|
||||
// Only resize if image is larger than max dimensions
|
||||
if ($width <= $maxWidth && $height <= $maxHeight) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Calculate new dimensions maintaining aspect ratio
|
||||
$ratio = min($maxWidth / $width, $maxHeight / $height);
|
||||
$newWidth = (int)($width * $ratio);
|
||||
$newHeight = (int)($height * $ratio);
|
||||
|
||||
// Load image based on type
|
||||
switch ($mime) {
|
||||
case 'image/jpeg':
|
||||
$source = imagecreatefromjpeg($filePath);
|
||||
break;
|
||||
case 'image/png':
|
||||
$source = imagecreatefrompng($filePath);
|
||||
break;
|
||||
case 'image/gif':
|
||||
$source = imagecreatefromgif($filePath);
|
||||
break;
|
||||
case 'image/webp':
|
||||
$source = imagecreatefromwebp($filePath);
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$source) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create resized image
|
||||
$destination = imagecreatetruecolor($newWidth, $newHeight);
|
||||
|
||||
// Preserve transparency for PNG and GIF
|
||||
if ($mime === 'image/png' || $mime === 'image/gif') {
|
||||
$transparent = imagecolorallocatealpha($destination, 0, 0, 0, 127);
|
||||
imagefill($destination, 0, 0, $transparent);
|
||||
imagesavealpha($destination, true);
|
||||
}
|
||||
|
||||
// Resize
|
||||
imagecopyresampled($destination, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
||||
|
||||
// Save image
|
||||
$success = false;
|
||||
switch ($mime) {
|
||||
case 'image/jpeg':
|
||||
$success = imagejpeg($destination, $filePath, 85);
|
||||
break;
|
||||
case 'image/png':
|
||||
$success = imagepng($destination, $filePath, 6);
|
||||
break;
|
||||
case 'image/gif':
|
||||
$success = imagegif($destination, $filePath);
|
||||
break;
|
||||
case 'image/webp':
|
||||
$success = imagewebp($destination, $filePath, 85);
|
||||
break;
|
||||
}
|
||||
|
||||
// Free up memory
|
||||
imagedestroy($source);
|
||||
imagedestroy($destination);
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user