Skip to content

Commit

Permalink
Merge pull request #387 from thephpleague/3.x-interlacing
Browse files Browse the repository at this point in the history
Fix interlacing
  • Loading branch information
ADmad authored Jan 30, 2024
2 parents bec08c7 + 10c8b36 commit 874a11f
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);
} elseif ($driver instanceof GdDriver) {
imageinterlace($img, true);
}

return $image;
}
}

0 comments on commit 874a11f

Please sign in to comment.