added media parser

This commit is contained in:
2023-02-20 10:19:41 +10:00
parent 584e146af0
commit 5414c0b232
2 changed files with 57 additions and 0 deletions

View File

@@ -18,6 +18,11 @@
RewriteCond %{REQUEST_URI} (.+)/$ RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301] RewriteRule ^ %1 [L,R=301]
# Pass to media handler if the media request has query
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{QUERY_STRING} .
RewriteRule ^uploads/(.+)\.(jpg|png)$ media.php?url=uploads/$1.$2 [QSA,L]
# Send Requests To Front Controller... # Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-f

52
public/media.php Normal file
View File

@@ -0,0 +1,52 @@
<?php
if (isset($_GET['url']) && strpos($_GET['url'], 'uploads/') === 0 && is_file($_GET['url'])) {
$image = imagecreatefromstring(file_get_contents($_GET['url']));
$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($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($_GET['url']));
readfile($_GET['url']);
}
// Clean up the image resources
imagedestroy($image);
} else {
// Return a 404 error
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
exit;
}