Skip to content

Commit

Permalink
Fix interlacing.
Browse files Browse the repository at this point in the history
Interlacing for JPGs only seems to be working when using Imagick.
So need to look into that later or state that as a limitation in the docs.
  • Loading branch information
ADmad committed Jan 30, 2024
1 parent bcb3168 commit 10c8b36
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/Manipulators/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\ImageInterface;

class Encode extends BaseManipulator
Expand All @@ -21,30 +22,23 @@ public function run(ImageInterface $image): ImageInterface
$format = $this->getFormat($image);
$quality = $this->getQuality();
$driver = $image->driver();
$interlace = false;

if (in_array($format, ['jpg', 'pjpg'], true)) {
$image = (new ImageManager($driver))
->create($image->width(), $image->height())
->fill('ffffff')
->place($image, 'top-left', 0, 0);
}
if ('pjpg' === $format) {
$interlace = true;

if (in_array($format, ['png', 'pjpg'], true)) {
$i = $image->core()->native();
if ($driver instanceof ImagickDriver) {
$i->setInterlaceScheme(3); // 3 = Imagick::INTERLACE_PLANE constant
} elseif ($driver instanceof GdDriver) {
imageinterlace($i, true);
}

if ('pjpg' === $format) {
$format = 'jpg';
}
$format = 'jpg';
}

return (new ImageManager($driver))->read(
$image->encodeByExtension($format, $quality)->toFilePointer()
$image = (new ImageManager($driver))->read(
$image->encodeByExtension($format, $quality)->toString()
);

if ($interlace) {
$image = $this->interlace($image, $driver);
}

return $image;
}

/**
Expand Down Expand Up @@ -104,4 +98,17 @@ public function getQuality(): int

return (int) $q;
}

protected function interlace(ImageInterface $image, DriverInterface $driver): ImageInterface
{
$img = $image->core()->native();

if ($driver instanceof ImagickDriver) {
$img->setInterlaceScheme(\Imagick::INTERLACE_PLANE);

Check warning on line 107 in src/Manipulators/Encode.php

View check run for this annotation

Codecov / codecov/patch

src/Manipulators/Encode.php#L107

Added line #L107 was not covered by tests
} elseif ($driver instanceof GdDriver) {
imageinterlace($img, true);
}

return $image;
}
}

0 comments on commit 10c8b36

Please sign in to comment.