Skip to content

Commit

Permalink
Merge pull request #28 from utopia-php/feat-webp-quality
Browse files Browse the repository at this point in the history
Added missing compression quality for webp
  • Loading branch information
christyjacob4 authored Feb 5, 2024
2 parents 88f7209 + 9044558 commit 2d74c27
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 8 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
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
Expand Up @@ -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') ?: '');
Expand Down
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.

0 comments on commit 2d74c27

Please sign in to comment.