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

Fully supports progressive JPGS and Interlaced images for 3.x #394

Open
wants to merge 10 commits into
base: 3.x
Choose a base branch
from

Conversation

konnng-dev
Copy link

The intent of this PR is to introduce support to generate progressive JPEGs or interlaced images.

Couple days ago, the author of Intervention provided support to accomplish that https://github.com/Intervention/image/releases/tag/3.6.0

With that, I did some adjustments on Glide API to get the desired support.

Highlights:

  • Encode manipulator moved from the $manipulators list, because it needs to be the last manipulator to run when generating the images.
  • Encode manipulator now returns a EncodedImage. This change avoids to double encode image when generating it.
  • Encode also makes more use of Intervention enums to encode the image properly.
  • interlace parameter also was added to create progressive JPEGs or interlaced PNGs or GIFs.

@konnng-dev
Copy link
Author

konnng-dev commented Apr 22, 2024

TODO:

  • work on tests to check for progressive / interlaced images.
  • fix code formatting issues

@konnng-dev
Copy link
Author

@ADmad would you mind to take a look and tell me what you think?

I'm not sure if introducing this new encoding strategy would be the best for this package.

feedbacks are welcome :)

@ADmad
Copy link
Collaborator

ADmad commented Apr 27, 2024

Sorry I haven't had the time to look into this but could you please address the failing tests in the meantime.

@ADmad ADmad mentioned this pull request Apr 27, 2024
@konnng-dev
Copy link
Author

konnng-dev commented May 9, 2024

@ADmad I addressed all the issues that Github Actions was issuing.

Before setting this ready for review, could you point me the best place where I could add the tests for the progressive/interlaced images I am introducing?

{
$format = $this->getFormat($image);
$quality = $this->getQuality();
$driver = $image->driver();
$interlace = false;
$shouldInterlace = filter_var($this->getParam('interlace'), FILTER_VALIDATE_BOOLEAN);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this new API option. It will allow to use progressive/interlaced images.

Example:

  • Progressive PNG: <img src="kayaks.jpg?w=500&interlacer=1">
  • Interlaced PNG: <img src="kayaks.png?w=500&interlacer=1">


if ('pjpg' === $format) {
$interlace = true;

$shouldInterlace = true;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If format is set to pjpeg, it will set automatically the interlace option to output a progressive JPG

@@ -255,7 +254,6 @@ public function getManipulators(): array
new Watermark($this->getWatermarks(), $this->getWatermarksPathPrefix() ?? ''),
new Background(),
new Border(),
new Encode(),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was necessary to remove the Encode manipulator from the list. This manipulator will be invoked in a better place.

}

return $image;
return $image->encodeByExtension(...$encoderOptions);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due the latest changes on Intervention package. It was necessary to change the return type of this class, to return the encoded image instance instead just the image instance.

Comment on lines +99 to +104
$encoder = new Encode();
$encoder->setParams($params);
/** @var \Intervention\Image\Interfaces\EncodedImageInterface */
$encoded = $encoder->run($image);

return $encoded->toString();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there was some important changes on how Intervention process images, it was necessary to move the Encode manipulator to run here instead.

This will make sure that it will run at the end of all manipulators, sine it returns a EncodedImage instance. Also it will avoid the strategy used before, causing double encoding of the given image.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If encoding is no longer a "normal" manipulation then I would rather move/remove the Maipulators/Encode class. We could add it's methods to Api class itself. That would also avoid having to change the return type of ManipulatorInterface::run().

@konnng-dev konnng-dev marked this pull request as ready for review May 20, 2024 13:03
@konnng-dev
Copy link
Author

@ADmad I added some comments to help the review process.

Ideally, it would be good to add tests for the new option added to the image API. But I don't have idea what would be the best place to put it.

break;
case 'gif':
case 'png':
$encoderOptions['interlaced'] = $shouldInterlace;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quality option isn't used for png/gif?

case 'png':
$encoderOptions['interlaced'] = $shouldInterlace;
break;
default:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of completeness we can throw an exception here with message Invalid format provided: {$format}.

{
$format = $this->getFormat($image);
$quality = $this->getQuality();
$driver = $image->driver();
$interlace = false;
$shouldInterlace = filter_var($this->getParam('interlace'), FILTER_VALIDATE_BOOLEAN);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the interlace option to the docs.

Comment on lines +99 to +104
$encoder = new Encode();
$encoder->setParams($params);
/** @var \Intervention\Image\Interfaces\EncodedImageInterface */
$encoded = $encoder->run($image);

return $encoded->toString();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If encoding is no longer a "normal" manipulation then I would rather move/remove the Maipulators/Encode class. We could add it's methods to Api class itself. That would also avoid having to change the return type of ManipulatorInterface::run().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants