Compare commits
2 Commits
a1ac9edadc
...
6f23c71773
Author | SHA1 | Date | |
---|---|---|---|
|
6f23c71773 | ||
|
be8d37d4cb |
@ -6,7 +6,8 @@
|
|||||||
"vlucas/phpdotenv": "^5.5",
|
"vlucas/phpdotenv": "^5.5",
|
||||||
"ext-gd": "*",
|
"ext-gd": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"ext-pdo_sqlite": "*"
|
"ext-pdo_sqlite": "*",
|
||||||
|
"psr/log": "^3.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
52
composer.lock
generated
52
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "35cc1fc18887fe14e702beb8a8486527",
|
"content-hash": "2269f5deec22d5f78ed3c229cddb1359",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "graham-campbell/result-type",
|
"name": "graham-campbell/result-type",
|
||||||
@ -384,6 +384,56 @@
|
|||||||
},
|
},
|
||||||
"time": "2021-11-05T16:47:00+00:00"
|
"time": "2021-11-05T16:47:00+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "psr/log",
|
||||||
|
"version": "3.0.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/php-fig/log.git",
|
||||||
|
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001",
|
||||||
|
"reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.0.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"branch-alias": {
|
||||||
|
"dev-master": "3.x-dev"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Psr\\Log\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "PHP-FIG",
|
||||||
|
"homepage": "https://www.php-fig.org/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "Common interface for logging libraries",
|
||||||
|
"homepage": "https://github.com/php-fig/log",
|
||||||
|
"keywords": [
|
||||||
|
"log",
|
||||||
|
"psr",
|
||||||
|
"psr-3"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"source": "https://github.com/php-fig/log/tree/3.0.0"
|
||||||
|
},
|
||||||
|
"time": "2021-07-14T16:46:02+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "symfony/polyfill-ctype",
|
"name": "symfony/polyfill-ctype",
|
||||||
"version": "v1.27.0",
|
"version": "v1.27.0",
|
||||||
|
@ -3,10 +3,12 @@ error_reporting(E_ERROR | E_PARSE);
|
|||||||
|
|
||||||
require_once './vendor/autoload.php';
|
require_once './vendor/autoload.php';
|
||||||
|
|
||||||
|
use ImageResizer\Lib\FileLogger;
|
||||||
use ImageResizer\Lib\Resizer;
|
use ImageResizer\Lib\Resizer;
|
||||||
use ImageResizer\Lib\SqliteImageDb;
|
use ImageResizer\Lib\SqliteImageDb;
|
||||||
use ImageResizer\Models\ResizeParams;
|
use ImageResizer\Models\ResizeParams;
|
||||||
|
|
||||||
|
$logger = new FileLogger("resizer.log");
|
||||||
$dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]);
|
$dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]);
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|
||||||
@ -15,6 +17,12 @@ $imageDb = new SqliteImageDb(new PDO("sqlite:{$_ENV["db_name"]}"));
|
|||||||
$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);
|
||||||
|
|
||||||
$resizer = new Resizer($imageDb);
|
$resizer = new Resizer($imageDb, $logger);
|
||||||
|
|
||||||
echo $resizer->performResize($imgPath, $params);
|
try {
|
||||||
|
echo $resizer->performResize($imgPath, $params);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$logger->error($e->getMessage());
|
||||||
|
http_response_code(500);
|
||||||
|
echo "Internal Server Error";
|
||||||
|
}
|
21
src/Lib/FileLogger.php
Normal file
21
src/Lib/FileLogger.php
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ImageResizer\Lib;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use Psr\Log\AbstractLogger;
|
||||||
|
use Stringable;
|
||||||
|
|
||||||
|
class FileLogger extends AbstractLogger
|
||||||
|
{
|
||||||
|
function __construct(private readonly string $filename = 'php://stdout') {}
|
||||||
|
public function log($level, Stringable|string $message, array $context = []): void
|
||||||
|
{
|
||||||
|
$timestamp = new DateTime();
|
||||||
|
$callingClass = debug_backtrace()[2]['class'];
|
||||||
|
$callingFunction = debug_backtrace()[2]['function'];
|
||||||
|
$log = $timestamp->format(DATE_ATOM) . " [$level] $callingClass::$callingFunction $message " . json_encode($context);
|
||||||
|
fwrite(fopen($this->filename, 'w'), $log . PHP_EOL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,10 +6,13 @@ namespace ImageResizer\Lib;
|
|||||||
|
|
||||||
use ImageResizer\Models\ResizeParams;
|
use ImageResizer\Models\ResizeParams;
|
||||||
use ImageResizer\Models\Image;
|
use ImageResizer\Models\Image;
|
||||||
|
use Psr\Log\AbstractLogger;
|
||||||
|
|
||||||
class Resizer
|
class Resizer
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly ImageDb $imageDb
|
private readonly ImageDb $imageDb,
|
||||||
|
private readonly AbstractLogger $logger,
|
||||||
)
|
)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ class Resizer
|
|||||||
{
|
{
|
||||||
// Check if the image has already been processed
|
// Check if the image has already been processed
|
||||||
if ($image = $this->imageDb->findImage($path, $params)) {
|
if ($image = $this->imageDb->findImage($path, $params)) {
|
||||||
|
$this->logger->info("Found existing image", ['path'=>$path, 'params'=>$params]);
|
||||||
header("Content-type: {${$image->getMime()}}");
|
header("Content-type: {${$image->getMime()}}");
|
||||||
return base64_decode($image->encode(75));
|
return base64_decode($image->encode(75));
|
||||||
}
|
}
|
||||||
@ -28,6 +31,7 @@ class Resizer
|
|||||||
// Fetch the image at the given URL
|
// Fetch the image at the given URL
|
||||||
$original_img = Image::fromUrl($_ENV['ROOT_DOMAIN'] . $path);
|
$original_img = Image::fromUrl($_ENV['ROOT_DOMAIN'] . $path);
|
||||||
$converted = $this->createVariant($original_img, $params);
|
$converted = $this->createVariant($original_img, $params);
|
||||||
|
$this->logger->info("Created variant", ['path'=>$path, 'params'=>$params]);
|
||||||
|
|
||||||
// Store the image in the database
|
// Store the image in the database
|
||||||
$this->imageDb->saveImage(
|
$this->imageDb->saveImage(
|
||||||
@ -35,6 +39,7 @@ class Resizer
|
|||||||
$params,
|
$params,
|
||||||
$converted
|
$converted
|
||||||
);
|
);
|
||||||
|
$this->logger->info("Saved image for $path");
|
||||||
|
|
||||||
header("Content-type: {$original_img->getMime()}");
|
header("Content-type: {$original_img->getMime()}");
|
||||||
return base64_decode($converted);
|
return base64_decode($converted);
|
||||||
|
Loading…
Reference in New Issue
Block a user