automagic image optimization

This commit is contained in:
2023-03-13 21:15:54 +10:00
parent a7219861f4
commit 0bbb1f0eba
4 changed files with 144 additions and 41 deletions

View File

@@ -8,51 +8,72 @@ if (isset($_GET['url'])) {
}
if ($filepath !== false && strlen($filepath) > 0 && strpos($_GET['url'], 'uploads/') === 0 && is_file($filepath)) {
$image = imagecreatefromstring(file_get_contents($filepath));
$newWidth = (isset($_GET['w']) ? intval($_GET['w']) : -1);
$newHeight = (isset($_GET['h']) ? intval($_GET['h']) : -1);
if($newWidth != -1 || $newHeight != -1) {
$width = imagesx($image);
$height = imagesy($image);
$aspectRatio = $width / $height;
if($newWidth == -1) {
$newWidth = intval($newHeight * $aspectRatio);
if(isset($_GET['size'])) {
$availableSizes = ['thumb', 'medium', 'large']; // we ignore full as its the original file
$requestedSize = strtolower($_GET['size']);
$requestedSizeIndex = array_search($requestedSize, $availableSizes);
// Loop through the array from the requested size index
if($requestedSizeIndex !== false) {
for ($i = $requestedSizeIndex; $i < count($availableSizes); $i++) {
$sizePath = pathinfo($filepath, PATHINFO_DIRNAME) . '/' . pathinfo($filepath, PATHINFO_FILENAME) . "-$availableSizes[$i]." . pathinfo($filepath, PATHINFO_EXTENSION);
if (file_exists($sizePath)) {
$filepath = $sizePath;
break;
}
}
}
if($newHeight == -1) {
$newHeight = intval($newWidth / $aspectRatio);
}
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Output the resized image to the browser
$mime_type = mime_content_type($_GET['url']);
header('Content-Type: ' . $mime_type);
switch($mime_type) {
case "image/jpeg":
imagejpeg($newImage);
break;
case "image/gif":
imagegif($newImage);
break;
case "image/png":
imagepng($newImage);
break;
}
imagedestroy($newImage);
} else {
// Output the original image to the browser
header('Content-Type: '. mime_content_type($filepath));
readfile($filepath);
}
} else {
$newWidth = (isset($_GET['w']) ? intval($_GET['w']) : -1);
$newHeight = (isset($_GET['h']) ? intval($_GET['h']) : -1);
// Clean up the image resources
imagedestroy($image);
if($newWidth != -1 || $newHeight != -1) {
$image = imagecreatefromstring(file_get_contents($filepath));
$width = imagesx($image);
$height = imagesy($image);
$aspectRatio = $width / $height;
if($newWidth == -1) {
$newWidth = intval($newHeight * $aspectRatio);
}
if($newHeight == -1) {
$newHeight = intval($newWidth / $aspectRatio);
}
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Output the resized image to the browser
$mime_type = mime_content_type($_GET['url']);
header('Content-Type: ' . $mime_type);
switch($mime_type) {
case "image/jpeg":
imagejpeg($newImage);
break;
case "image/gif":
imagegif($newImage);
break;
case "image/png":
imagepng($newImage);
break;
}
imagedestroy($newImage);
// Clean up the image resources
imagedestroy($image);
} else {
// Output the original image to the browser
header('Content-Type: '. mime_content_type($filepath));
readfile($filepath);
}
}
} else {
// Return a 404 error
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");