Skip to content

Commit

Permalink
quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Gappa committed Jun 26, 2023
1 parent f2c65ec commit fb29661
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 24 deletions.
18 changes: 10 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ AVIF is preferred over WEBP in case both upgrades are enabled.

# Usage

Order of parameters:
Order of parameters is dependant on the router, the default is:

1. Image file. `string`
2. Dimensions. `string`
1. Dimensions & modifiers. `string`
2. Image file. `string`
3. Format. `string`

## Dimensions
## Dimensions & modifiers

Allowed variants:
### Dimensions:

- `100x100` - width and height must be equal or less, resized according to AR.
- `x100` - height must be equal or less.
- `100x` - width must be equal or less.

Modificators:
### Modifiers:

- Cropping:
- width: `l` - left, `c` - center, `r` - right.
Expand All @@ -61,12 +61,14 @@ Modificators:
- `ifresize-100x200` - do not resize if the source is smaller.
- Force dimensions:
- `100x200!` - resize to these dimensions, regardless of AR.
- Quality:
- `-q50` - set custom quality

Formats:
## Format:

- The format parameter can be used to switch between image file formats, e.g. `<source srcset="">` in `<picture>` tag for converting jpegs to WEBP/AVIF.

## Types
# Types

Insert the src manually:

Expand Down
2 changes: 1 addition & 1 deletion src/Presenters/ResizePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function startup(): void
public function actionDefault(
string $file,
?string $params = null,
?string $format = null
?string $format = null,
): void {
try {
$image = $this->resizer->process(
Expand Down
10 changes: 5 additions & 5 deletions src/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public function __construct(
private readonly OutputFormat $outputFormat,
)
{
$this->cacheDir = $config->getTempDir() . $config->getCache();
$this->cacheDir = $this->config->getTempDir() . $this->config->getCache();
FileSystem::createDir($this->cacheDir);

/** @var AbstractImagine $library */
$library = implode('\\', ['Imagine', $config->getLibrary(), 'Imagine']);
$library = implode('\\', ['Imagine', $this->config->getLibrary(), 'Imagine']);
$this->imagine = new $library;
}


public function process(
string $path,
?string $params,
?string $format = null
?string $format = null,
): string {
$sourceImagePath = $this->getSourceImagePath($path);

Expand All @@ -63,7 +63,7 @@ public function process(
if (!$this->thumbnailExists($thumbnailPath)) {
try {
$thumbnail = $this->processImage($sourceImagePath, $geometry);
} catch (RuntimeException $e) {
} catch (RuntimeException) {
throw new ImageNotFoundOrReadableException('Unable to open image - wrong permissions, empty or corrupted.');
}

Expand All @@ -77,7 +77,7 @@ public function process(
$thumbnail->interlace(ImageInterface::INTERLACE_LINE);
}

$thumbnail->save($thumbnailPath, $this->config->getOptions());
$thumbnail->save($thumbnailPath, $this->config->getOptions($geometry->getResizerParams()->getQuality()));
}

return $thumbnailPath;
Expand Down
20 changes: 15 additions & 5 deletions src/ResizerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,30 @@ public function getCompressionPng(): int


/**
* @param int<0,100>|null $quality
* @return array{
* avif_quality: int<0, 100>,
* webp_quality: int<0, 100>,
* jpeg_quality: int<0, 100>,
* png_compression_level: int<0, 9>
* }
*/
public function getOptions(): array
public function getOptions(?int $quality = null): array
{

if ($quality !== null) {
$qualityPng = (int) round($quality / 10);

if ($qualityPng > 9) {
$qualityPng = 9;
}
}

return [
'avif_quality' => $this->getQualityAvif(),
'webp_quality' => $this->getQualityWebp(),
'jpeg_quality' => $this->getQualityJpeg(),
'png_compression_level' => $this->getCompressionPng(),
'avif_quality' => $quality ?? $this->getQualityAvif() ,
'webp_quality' => $quality ?? $this->getQualityWebp(),
'jpeg_quality' => $quality ?? $this->getQualityJpeg(),
'png_compression_level' => $qualityPng ?? $this->getCompressionPng(),
];
}

Expand Down
4 changes: 2 additions & 2 deletions src/ResizerParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ResizerParams
/**
* @param positive-int|null $width
* @param positive-int|null $height
* @param positive-int|null $quality
* @param int<0,100>|null $quality
*/
public function __construct(
private readonly bool $ifresize,
Expand Down Expand Up @@ -74,7 +74,7 @@ public function hasWidth(): bool
}


/** @return positive-int|null */
/** @return int<0,100>|null */
public function getHeight(): ?int
{
return $this->height;
Expand Down
7 changes: 4 additions & 3 deletions src/ResizerParamsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ResizerParamsParser
(!?) # force dimensions, disregard aspect ratio
([+-]?[0-9]*) # horizontal margin, unused
([+-]?[0-9]*) # vertical margin, unused
(?:-q([1-9][0-9]?|100))? # quality, 1-100
(?:-q([0-9][0-9]?|100))? # quality, 0-100
)$
~x';

Expand Down Expand Up @@ -72,7 +72,7 @@ public function getParams(): ResizerParams
}


/** @return positive-int|null */
/** @return int|null */
private function parseNumericValueToIntOrNull(string $value): ?int
{
if (strlen($value) === 0) {
Expand All @@ -82,11 +82,12 @@ private function parseNumericValueToIntOrNull(string $value): ?int
if (is_numeric($value)) {
$int = (int) $value;

if ($int > 0) {
if ($int >= 0) {
return $int;
}
}

return null;
}

}
38 changes: 38 additions & 0 deletions tests/ResizerParamsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,44 @@ public function testQuality2(): void
}


public function testQuality3(): void
{
$expected = new ResizerParams(
false,
null,
null,
false,
null,
null,
100,
200,
0,
);

$actual = $this->parse('100x200-q0');
$this->assertEquals($expected, $actual);
}


public function testQuality4(): void
{
$expected = new ResizerParams(
false,
null,
null,
false,
null,
null,
100,
200,
100,
);

$actual = $this->parse('100x200-q100');
$this->assertEquals($expected, $actual);
}


public function testWrongKeyword1(): void
{
$this->expectException(CouldNotParseResizerParamsException::class);
Expand Down

0 comments on commit fb29661

Please sign in to comment.