Skip to content

Commit

Permalink
update ImageDispenser
Browse files Browse the repository at this point in the history
  • Loading branch information
reliq committed May 12, 2024
1 parent 86fbaf0 commit 75637bc
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 458 deletions.
25 changes: 2 additions & 23 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,9 @@ jobs:
strategy:
max-parallel: 10
matrix:
laravel-version: ['^6.0', '^7.0', '^8.0', '^9.0', '^10.0']
laravel-version: ['^11.0', '^11.1']
preference: ['stable']
php-version: ['7.4', '8.0', '8.1', '8.2']
exclude:
- laravel-version: ^6.0
php-version: 8.0
- laravel-version: ^6.0
php-version: 8.1
- laravel-version: ^6.0
php-version: 8.2
- laravel-version: ^7.0
php-version: 8.0
- laravel-version: ^7.0
php-version: 8.1
- laravel-version: ^7.0
php-version: 8.2
- laravel-version: ^9.0
php-version: 7.4
- laravel-version: ^9.0
php-version: 8.2
- laravel-version: ^10.0
php-version: 7.4
- laravel-version: ^10.0
php-version: 8.0
php-version: ['8.2', '8.3']
name: Laravel ${{ matrix.laravel-version }} (${{ matrix.preference }}) on PHP ${{ matrix.php-version }}
steps:
- name: Checkout
Expand Down
4 changes: 2 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

// image encoding @see: http://image.intervention.io/api/encode
'encoding' => [
'format' => env('GUIDED_IMAGE_ENCODING_FORMAT', 'png'),
'mime_type' => env('GUIDED_IMAGE_ENCODING_MIME_TYPE', 'image/png'),
'quality' => env('GUIDED_IMAGE_ENCODING_QUALITY', 90),
],

Expand Down Expand Up @@ -92,5 +92,5 @@
'dispenser' => [
// whether raw image should be served as fallback if NotReadableException occurs
'raw_image_fallback_enabled' => env('GUIDED_IMAGE_DISPENSER_RAW_IMAGE_FALLBACK_ENABLED', false),
]
],
];
18 changes: 10 additions & 8 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" beStrictAboutTestsThatDoNotTestAnything="true" bootstrap="tests/bootstrap.php" cacheResult="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" verbose="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix=".php">src/Concerns</directory>
</exclude>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="true" bootstrap="tests/bootstrap.php" cacheResult="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<report>
<clover outputFile="build/coverage.xml"/>
<text outputFile="build/coverage.txt"/>
Expand All @@ -32,4 +26,12 @@
<env name="GUIDED_IMAGE_MODEL" value="GuidedImage"/>
</php>
<logging/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix=".php">src/Concerns</directory>
</exclude>
</source>
</phpunit>
8 changes: 3 additions & 5 deletions src/Contract/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ public function getRoutePrefix(): string;
/**
* Get image model for guided image routes.
*
* @param bool $lowered whether model should be returned in lowercase form
*
* @param bool $lowered whether model should be returned in lowercase form
* @return string model name
*/
public function getGuidedModelName(bool $lowered = false): string;

/**
* Get image model namespace for guided image routes.
*
* @param bool $lowered whether model should be returned in lowercase form
*
* @param bool $lowered whether model should be returned in lowercase form
* @return string model namespace
*/
public function getGuidedModelNamespace(bool $lowered = false): string;
Expand Down Expand Up @@ -73,7 +71,7 @@ public function getAdditionalHeaders(): array;

public function getCacheDirectory(): string;

public function getImageEncodingFormat(): string;
public function getImageEncodingMimeType(): string;

public function getImageEncodingQuality(): int;

Expand Down
8 changes: 4 additions & 4 deletions src/Contract/ImageDispenser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ReliqArts\GuidedImage\Contract;

use Intervention\Image\Image;
use Intervention\Image\Interfaces\ImageInterface;
use ReliqArts\GuidedImage\Demand\Dummy;
use ReliqArts\GuidedImage\Demand\Resize;
use ReliqArts\GuidedImage\Demand\Thumbnail;
Expand All @@ -14,14 +14,14 @@
interface ImageDispenser
{
/**
* @return Image|Response
* @return ImageInterface|Response
*/
public function getImageThumbnail(Thumbnail $demand);

/**
* Get a resized Guided Image.
*
* @return Image|Response
* @return ImageInterface|Response
*/
public function getResizedImage(Resize $demand);

Expand All @@ -30,7 +30,7 @@ public function getResizedImage(Resize $demand);
*
* @throws RuntimeException
*/
public function getDummyImage(Dummy $demand): Image;
public function getDummyImage(Dummy $demand): ImageInterface;

public function emptyCache(): bool;
}
26 changes: 26 additions & 0 deletions src/Contract/ImageManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace ReliqArts\GuidedImage\Contract;

use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use RuntimeException;

interface ImageManager
{
/**
* @throws RuntimeException
*
* @see \Intervention\Image\ImageManager::create()
*/
public function create(int $width, int $height): ImageInterface;

/**
* @throws RuntimeException
*
* @see \Intervention\Image\ImageManager::read()
*/
public function read(mixed $input, string|array|DecoderInterface $decoders = []): ImageInterface;
}
11 changes: 0 additions & 11 deletions src/Contract/Logger.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Demand/Thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class Thumbnail extends ExistingImage

private const METHOD_FIT = 'fit';

private const METHODS = [self::METHOD_CROP, self::METHOD_FIT, self::METHOD_COVER];
private const METHODS = [self::METHOD_CROP, self::METHOD_COVER, self::METHOD_FIT];

public function __construct(
Request $request,
Expand Down
56 changes: 47 additions & 9 deletions src/Service/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,84 @@
final class ConfigProvider implements ConfigProviderContract
{
private const CONFIG_KEY_ALLOWED_EXTENSIONS = 'allowed_extensions';

private const CONFIG_KEY_ROUTES_CONTROLLERS = 'routes.controllers';

private const CONFIG_KEY_ROUTES_PREFIX = 'routes.prefix';

private const CONFIG_KEY_GUIDED_MODEL = 'model';

private const CONFIG_KEY_GUIDED_MODEL_NAMESPACE = 'model_namespace';

private const CONFIG_KEY_ROUTES_BINDINGS_WITH_GROUP = 'routes.bindings.%s';

private const CONFIG_KEY_IMAGE_RULES = 'rules';

private const CONFIG_KEY_IMAGES_TABLE = 'database.image_table';

private const CONFIG_KEY_IMAGEABLES_TABLE = 'database.imageables_table';

private const CONFIG_KEY_STORAGE_UPLOAD_DIRECTORY = 'storage.upload_dir';

private const CONFIG_KEY_STORAGE_CACHE_DISK = 'storage.cache_disk';

private const CONFIG_KEY_STORAGE_UPLOAD_DISK = 'storage.upload_disk';

private const CONFIG_KEY_STORAGE_CACHE_DIR = 'storage.cache_dir';

private const CONFIG_KEY_STORAGE_CACHE_SUB_DIR_RESIZED = 'storage.cache_sub_dir_resized';

private const CONFIG_KEY_STORAGE_CACHE_SUB_DIR_THUMBS = 'storage.cache_sub_dir_thumbs';

private const CONFIG_KEY_STORAGE_GENERATE_UPLOAD_DATE_SUB_DIRECTORIES
= 'storage.generate_upload_date_sub_directories';

private const CONFIG_KEY_HEADERS_CACHE_DAYS = 'headers.cache_days';

private const CONFIG_KEY_HEADERS_ADDITIONAL = 'headers.additional';
private const CONFIG_KEY_IMAGE_ENCODING_FORMAT = 'encoding.format';

private const CONFIG_KEY_IMAGE_ENCODING_MIME_TYPE = 'encoding.mime_type';

private const CONFIG_KEY_IMAGE_ENCODING_QUALITY = 'encoding.quality';

private const CONFIG_KEY_DISPENSER_RAW_IMAGE_FALLBACK_ENABLED = 'dispenser.raw_image_fallback_enabled';

private const DEFAULT_ALLOWED_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png'];

private const DEFAULT_ROUTES_PREFIX = 'image';

private const DEFAULT_GUIDED_MODEL = 'Image';

private const DEFAULT_GUIDED_MODEL_NAMESPACE = 'App\\';

private const DEFAULT_IMAGES_TABLE = 'images';

private const DEFAULT_IMAGEABLES_TABLE = 'imageables';

private const DEFAULT_IMAGE_RULES = 'required|mimes:png,gif,jpeg|max:2048';

private const DEFAULT_UPLOAD_DIRECTORY = 'uploads/images';

private const DEFAULT_STORAGE_CACHE_DIR = 'images';

private const DEFAULT_STORAGE_CACHE_DISK = 'local';

private const DEFAULT_STORAGE_UPLOAD_DISK = 'public';

private const DEFAULT_STORAGE_CACHE_SUB_DIR_THUMBS = '.thumbs';

private const DEFAULT_STORAGE_CACHE_SUB_DIR_RESIZED = '.resized';

private const DEFAULT_STORAGE_GENERATE_UPLOAD_DATE_SUB_DIRECTORIES = false;

private const DEFAULT_HEADER_CACHE_DAYS = 366;

private const DEFAULT_ADDITIONAL_HEADERS = [];
private const DEFAULT_IMAGE_ENCODING_FORMAT = 'png';

private const DEFAULT_IMAGE_ENCODING_MIME_TYPE = 'image/png';

private const DEFAULT_IMAGE_ENCODING_QUALITY = 90;

private const DEFAULT_DISPENSER_RAW_IMAGE_FALLBACK_ENABLED = false;

private const KEY_PREFIX = 'prefix';
Expand Down Expand Up @@ -121,7 +159,7 @@ public function getGuidedModelNamespace(bool $lowered = false): string
*/
public function getRouteGroupBindings(array $bindings = [], string $groupKey = self::ROUTE_GROUP_KEY_PUBLIC): array
{
$defaults = self::ROUTE_GROUP_KEY_PUBLIC === $groupKey ? [self::KEY_PREFIX => $this->getRoutePrefix()] : [];
$defaults = $groupKey === self::ROUTE_GROUP_KEY_PUBLIC ? [self::KEY_PREFIX => $this->getRoutePrefix()] : [];

$bindings = array_merge(
$this->configAccessor->get(sprintf(self::CONFIG_KEY_ROUTES_BINDINGS_WITH_GROUP, $groupKey), []),
Expand Down Expand Up @@ -159,7 +197,7 @@ public function getUploadDirectory(): string

public function generateUploadDateSubDirectories(): bool
{
return (bool)$this->configAccessor->get(
return (bool) $this->configAccessor->get(
self::CONFIG_KEY_STORAGE_GENERATE_UPLOAD_DATE_SUB_DIRECTORIES,
self::DEFAULT_STORAGE_GENERATE_UPLOAD_DATE_SUB_DIRECTORIES
);
Expand Down Expand Up @@ -189,7 +227,7 @@ public function getThumbsCachePath(): string

public function getCacheDaysHeader(): int
{
return (int)$this->configAccessor->get(self::CONFIG_KEY_HEADERS_CACHE_DAYS, self::DEFAULT_HEADER_CACHE_DAYS);
return (int) $this->configAccessor->get(self::CONFIG_KEY_HEADERS_CACHE_DAYS, self::DEFAULT_HEADER_CACHE_DAYS);
}

public function getAdditionalHeaders(): array
Expand All @@ -205,17 +243,17 @@ public function getCacheDirectory(): string
);
}

public function getImageEncodingFormat(): string
public function getImageEncodingMimeType(): string
{
return $this->configAccessor->get(
self::CONFIG_KEY_IMAGE_ENCODING_FORMAT,
self::DEFAULT_IMAGE_ENCODING_FORMAT
self::CONFIG_KEY_IMAGE_ENCODING_MIME_TYPE,
self::DEFAULT_IMAGE_ENCODING_MIME_TYPE
);
}

public function getImageEncodingQuality(): int
{
return (int)$this->configAccessor->get(
return (int) $this->configAccessor->get(
self::CONFIG_KEY_IMAGE_ENCODING_QUALITY,
self::DEFAULT_IMAGE_ENCODING_QUALITY
);
Expand Down
Loading

0 comments on commit 75637bc

Please sign in to comment.