Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added missing compression quality for webp #28

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -22,9 +22,7 @@ before_install:
- sudo service docker start
- docker --version
- docker buildx create --use

install:
- docker-compose build
- docker-compose up -d

script:
- docker-compose up
- docker compose exec php8 /code/vendor/bin/phpunit --configuration /code/phpunit.xml $@
2 changes: 1 addition & 1 deletion Dockerfile-php8
Original file line number Diff line number Diff line change
@@ -64,4 +64,4 @@ COPY ./psalm.xml /code/psalm.xml

RUN echo extension=imagick.so >> /usr/local/etc/php/conf.d/imagick.ini

CMD [ "sh", "-c", "/code/vendor/bin/phpunit --verbose --configuration /code/phpunit.xml && /code/vendor/bin/psalm --show-info=true" ]
CMD [ "tail", "-f", "/dev/null" ]
4 changes: 1 addition & 3 deletions src/Image/Image.php
Original file line number Diff line number Diff line change
@@ -360,7 +360,6 @@ public function save(string $path = null, string $type = '', int $quality = 75)
case 'jpg':
case 'jpeg':
$this->image->setImageCompressionQuality($quality);

$this->image->setImageFormat('jpg');
break;

@@ -370,6 +369,7 @@ public function save(string $path = null, string $type = '', int $quality = 75)

case 'webp':
try {
$this->image->setImageCompressionQuality($quality);
$this->image->setImageFormat('webp');
} catch (\Throwable$th) {
$signature = $this->image->getImageSignature();
@@ -406,12 +406,10 @@ public function save(string $path = null, string $type = '', int $quality = 75)
case 'png':
/* Scale quality from 0-100 to 0-9 */
$scaleQuality = \round(($quality / 100) * 9);

/* Invert quality setting as 0 is best, not 9 */
$invertScaleQuality = intval(9 - $scaleQuality);

$this->image->setImageCompressionQuality($invertScaleQuality);

$this->image->setImageFormat('png');
break;

Binary file added tests/Image/100x100-q30.webp
Binary file not shown.
28 changes: 28 additions & 0 deletions tests/Image/ImageTest.php
Original file line number Diff line number Diff line change
@@ -286,6 +286,34 @@ public function testCrop100x100WEBP(): void
\unlink($target);
}

public function testCrop100x100WEBPQuality30(): void
{
$image = new Image(\file_get_contents(__DIR__.'/../resources/disk-a/kitten-1.jpg') ?: '');
$target = __DIR__.'/100x100-q30.webp';
$original = __DIR__.'/../resources/resize/100x100-q30.webp';

$image->crop(100, 100);

$image->save($target, 'webp', 30);

$this->assertEquals(\is_readable($target), true);
$this->assertGreaterThan(500, \filesize($target));
$this->assertLessThan(2000, \filesize($target));
$this->assertEquals(\mime_content_type($target), \mime_content_type($original));
$this->assertNotEmpty(\md5(\file_get_contents($target) ?: ''));

$this->assertEquals(\is_readable($target), true);
$this->assertNotEmpty(\md5(\file_get_contents($target) ?: ''));

$image = new \Imagick($target);

$this->assertEquals(100, $image->getImageWidth());
$this->assertEquals(100, $image->getImageHeight());
$this->assertTrue(in_array($image->getImageFormat(), ['PAM', 'WEBP']));

//\unlink($target);
}

public function testCrop100x100PNG(): void
{
$image = new Image(\file_get_contents(__DIR__.'/../resources/disk-a/kitten-1.jpg') ?: '');
Binary file added tests/resources/resize/100x100-q30.webp
Binary file not shown.
Binary file modified tests/resources/resize/C.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/resources/resize/N.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/resources/resize/S.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/resources/resize/W.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.