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

Implement Max Bytes Filter #275

Merged
merged 3 commits into from Nov 29, 2019
Merged

Conversation

@dmitryzuev
Copy link
Contributor

dmitryzuev commented Nov 27, 2019

In some cases I needed to get image with size less than specified (e.g to pass some custom RSS feeds specifications).

Max Bytes

max_bytes:%max_bytes
mb:%max_bytes

This filter automatically degrades the quality of the image until the image is under the specified amount of bytes.

Warning: this filter processes image multiple times to achieve specified image size

Default: 0

@DarthSim

This comment has been minimized.

Copy link
Collaborator

DarthSim commented Nov 27, 2019

Hi,

Thanks for your contribution!

Unfortunately, your solution is not optimal and needs a rework:

  1. You don't need to rerun the whole processing flow since quality affects only image saving. All you should do is to call your quality-trimming wrapper instead of img.Save(...) at the end of processImage if po.MaxBytes isn't zero.
  2. If you aren't satisfied with the result, you should immediately call cancel function returned by img.Save(). The result buffer isn't garbage collected because it's allocated by C. cancel frees this buffer, and if you delay its call, you can run into OOM easily.
  3. It would be good to call img.CopyMemory() at the beginning of your quality-trimming wrapper. This function will prepare the image pixels, so img.Save() won't need to do in at each try.
@dmitryzuev

This comment has been minimized.

Copy link
Contributor Author

dmitryzuev commented Nov 28, 2019

@DarthSim thanks for comment!

I'll try to rewrite it.

At first I tried to work around img.Save(), but didn't realize about cancelling, and always run into OOM

process.go Outdated Show resolved Hide resolved
process.go Outdated Show resolved Hide resolved
process.go Outdated Show resolved Hide resolved
@DarthSim

This comment has been minimized.

Copy link
Collaborator

DarthSim commented Nov 29, 2019

LGTM now. Thanks again for your contribution!

@DarthSim DarthSim merged commit d74c402 into imgproxy:master Nov 29, 2019
11 checks passed
11 checks passed
ci/circleci: checkout_code Your tests passed on CircleCI!
Details
ci/circleci: go1.11_vips8.6 Your tests passed on CircleCI!
Details
ci/circleci: go1.11_vips8.7 Your tests passed on CircleCI!
Details
ci/circleci: go1.11_vips8.8 Your tests passed on CircleCI!
Details
ci/circleci: go1.12_vips8.6 Your tests passed on CircleCI!
Details
ci/circleci: go1.12_vips8.7 Your tests passed on CircleCI!
Details
ci/circleci: go1.12_vips8.8 Your tests passed on CircleCI!
Details
ci/circleci: go1.13_vips8.6 Your tests passed on CircleCI!
Details
ci/circleci: go1.13_vips8.7 Your tests passed on CircleCI!
Details
ci/circleci: go1.13_vips8.8 Your tests passed on CircleCI!
Details
ci/circleci: lint Your tests passed on CircleCI!
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.