Compare commits
10
Commits
6f23c71773
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
519611d06b | ||
|
|
a0580ad085 | ||
|
|
e2cf30a307 | ||
|
|
a9e8b61e0b | ||
|
|
782c5b4b38 | ||
|
|
3949e611ea | ||
|
|
9164929969 | ||
|
|
16e0a8f381 | ||
|
|
69d8ec33a5 | ||
|
|
967beadeb3 |
No files matched your search
@@ -2,3 +2,4 @@
|
|||||||
/vendor/
|
/vendor/
|
||||||
/*.db
|
/*.db
|
||||||
/.idea/
|
/.idea/
|
||||||
|
/*.log
|
||||||
@@ -12,7 +12,8 @@ $logger = new FileLogger("resizer.log");
|
|||||||
$dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]);
|
$dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]);
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|
||||||
$imageDb = new SqliteImageDb(new PDO("sqlite:{$_ENV["db_name"]}"));
|
$pdo = new PDO("sqlite:".$_ENV["DB_NAME"]);
|
||||||
|
$imageDb = new SqliteImageDb($pdo, $logger);
|
||||||
|
|
||||||
$imgPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
$imgPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||||
$params = new ResizeParams($_GET);
|
$params = new ResizeParams($_GET);
|
||||||
@@ -20,9 +21,11 @@ $params = new ResizeParams($_GET);
|
|||||||
$resizer = new Resizer($imageDb, $logger);
|
$resizer = new Resizer($imageDb, $logger);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
echo $resizer->performResize($imgPath, $params);
|
$image = $resizer->performResize($imgPath, $params);
|
||||||
|
header("Cache-Control: max-age=86400, public");
|
||||||
|
echo $image;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$logger->error($e->getMessage());
|
$logger->error($e->getMessage(), ['exception' => $e, 'path' => $imgPath, 'params' => $params]);
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo "Internal Server Error";
|
echo "Internal Server Error";
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ class FileLogger extends AbstractLogger
|
|||||||
$callingClass = debug_backtrace()[2]['class'];
|
$callingClass = debug_backtrace()[2]['class'];
|
||||||
$callingFunction = debug_backtrace()[2]['function'];
|
$callingFunction = debug_backtrace()[2]['function'];
|
||||||
$log = $timestamp->format(DATE_ATOM) . " [$level] $callingClass::$callingFunction $message " . json_encode($context);
|
$log = $timestamp->format(DATE_ATOM) . " [$level] $callingClass::$callingFunction $message " . json_encode($context);
|
||||||
fwrite(fopen($this->filename, 'w'), $log . PHP_EOL);
|
fwrite(fopen($this->filename, 'a'), $log . PHP_EOL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
+2
-1
@@ -39,7 +39,7 @@ class Resizer
|
|||||||
$params,
|
$params,
|
||||||
$converted
|
$converted
|
||||||
);
|
);
|
||||||
$this->logger->info("Saved image for $path");
|
$this->logger->info("Saved image", ['path'=>$path, 'params'=>$params]);
|
||||||
|
|
||||||
header("Content-type: {$original_img->getMime()}");
|
header("Content-type: {$original_img->getMime()}");
|
||||||
return base64_decode($converted);
|
return base64_decode($converted);
|
||||||
@@ -48,6 +48,7 @@ class Resizer
|
|||||||
private function createVariant(Image $original, ResizeParams $params): string
|
private function createVariant(Image $original, ResizeParams $params): string
|
||||||
{
|
{
|
||||||
$quality = $params->quality ?: -1;
|
$quality = $params->quality ?: -1;
|
||||||
|
$this->logger->info("Resizing image", ['params'=>$params]);
|
||||||
|
|
||||||
$original->toWidth($params->width ?? intval($_ENV["DEFAULT_WIDTH"]));
|
$original->toWidth($params->width ?? intval($_ENV["DEFAULT_WIDTH"]));
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ namespace ImageResizer\Lib;
|
|||||||
|
|
||||||
use ImageResizer\Models\Image;
|
use ImageResizer\Models\Image;
|
||||||
use ImageResizer\Models\ResizeParams;
|
use ImageResizer\Models\ResizeParams;
|
||||||
|
use Psr\Log\AbstractLogger;
|
||||||
|
|
||||||
class SqliteImageDb implements ImageDb {
|
class SqliteImageDb implements ImageDb {
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly \PDO $db
|
private readonly \PDO $db,
|
||||||
|
private readonly AbstractLogger $logger,
|
||||||
) {
|
) {
|
||||||
$this->up();
|
$this->up();
|
||||||
}
|
}
|
||||||
@@ -22,16 +24,31 @@ class SqliteImageDb implements ImageDb {
|
|||||||
quality INTEGER NULLABLE,
|
quality INTEGER NULLABLE,
|
||||||
content TEXT NOT NULL
|
content TEXT NOT NULL
|
||||||
)');
|
)');
|
||||||
|
|
||||||
|
// Create a unique index on the path and params
|
||||||
|
$this->db->exec('CREATE UNIQUE INDEX IF NOT EXISTS images_path_width_height_quality ON images (path, width, height, quality)');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findImage(string $path, ResizeParams $params): Image | null {
|
public function findImage(string $path, ResizeParams $params): Image | null {
|
||||||
$stmt = $this->db->prepare('SELECT * FROM images WHERE path = :path AND width = :width AND height = :height AND quality = :quality');
|
$query = 'SELECT * FROM images WHERE path = :path';
|
||||||
$stmt->execute([
|
$query .= $params->width ? ' AND width = :width' : ' AND width IS NULL';
|
||||||
|
$query .= $params->height ? ' AND height = :height' : ' AND height IS NULL';
|
||||||
|
$query .= $params->quality ? ' AND quality = :quality' : ' AND quality IS NULL';
|
||||||
|
|
||||||
|
$stmt = $this->db->prepare($query);
|
||||||
|
|
||||||
|
$queryParams = [
|
||||||
':path' => $path,
|
':path' => $path,
|
||||||
':width' => $params->width,
|
];
|
||||||
':height' => $params->height,
|
if ($params->width !== null) $queryParams[':width'] = $params->width;
|
||||||
':quality' => $params->quality
|
if ($params->height !== null) $queryParams[':height'] = $params->height;
|
||||||
]);
|
if ($params->quality !== null) $queryParams[':quality'] = $params->quality;
|
||||||
|
|
||||||
|
$stmt->execute($queryParams);
|
||||||
|
|
||||||
|
$this->logger->info("Querying images", ['query' => $stmt->queryString]);
|
||||||
|
|
||||||
|
$stmt->execute();
|
||||||
|
|
||||||
$result = $stmt->fetchObject();
|
$result = $stmt->fetchObject();
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class ResizeParams
|
|||||||
|
|
||||||
public function __construct(array $params)
|
public function __construct(array $params)
|
||||||
{
|
{
|
||||||
$this->width = intval($params['width']) ?: intval($_ENV["DEFAULT_WIDTH"]);
|
$this->width = isset($params['width']) ? intval($params['width']) : intval($_ENV["DEFAULT_WIDTH"]);
|
||||||
$this->height = intval($params['height']) ?: null;
|
$this->height = intval($params['height']) ?: null;
|
||||||
$this->quality = isset($params['quality']) ? intval($params['quality']) : null;
|
$this->quality = isset($params['quality']) ? intval($params['quality']) : null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user