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

Change image processing from ImageMagick to libvips #30090

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Gargron
Copy link
Member

@Gargron Gargron commented Apr 26, 2024

  • Removes metadata
  • Retains color profile
  • Reads pixels for blurhash
  • Reads histograms for color extraction
  • Crops animated GIFs when needed

Fixes MAS-223

@Gargron Gargron added the performance Runtime performance label Apr 26, 2024
@renchap renchap linked an issue Apr 26, 2024 that may be closed by this pull request
Copy link
Sponsor Member

@renchap renchap left a comment

Choose a reason for hiding this comment

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

The Github actions will need to install libvips42 instead of imagemagick.

There are probably some ImageMagick related files to remove, for example config/imagemagick.

The Docker image will also need to be changed to install vips, @vmstan started to work on it. Maybe you can update it in this PR to simply install libvips42 in place of imagemagick, and @vmstan can open another PR later to get it installed better (because the current Debian package enabled far too many formats for our usage).

We will also need to take great care in the upgrade instructions to tell people that they need to install VIPS now (and uninstall IM?). This will also be an issue with people running on nightly. The alternative is to support both IM and VIPS for 4.3, with a config option to enable VIPS, show a deprecation warning with IM is used, and remove IM support in 4.4, but I do not know if this is worth it.

lib/paperclip/blurhash_transcoder.rb Show resolved Hide resolved
@renchap
Copy link
Sponsor Member

renchap commented May 5, 2024

It is also probably a good idea to call Vips.block_untrusted (see libvips/ruby-vips#382) so only the trusted loaders (ie, that are covered by the fuzzing testing) are enabled.

Untrusted operations can be seen with vips -l | grep untrusted. There are more details in the 8.13 release notes.

@Gargron Gargron force-pushed the feature-libvips branch 6 times, most recently from 4e72551 to f29cce2 Compare May 6, 2024 03:05
@Gargron Gargron force-pushed the feature-libvips branch 2 times, most recently from e40fea5 to 9bd1761 Compare May 6, 2024 19:07
@Gargron Gargron marked this pull request as ready for review May 7, 2024 00:17
Copy link
Contributor

github-actions bot commented May 7, 2024

This pull request has merge conflicts that must be resolved before it can be merged.

@shleeable
Copy link
Contributor

Worth adding libheif?

@vmstan
Copy link
Contributor

vmstan commented May 8, 2024

Worth adding libheif?

It's included by the libvips42 package.

@vmstan
Copy link
Contributor

vmstan commented May 8, 2024

I’ve been running this PR for the last 20 hours on vmst.io and it’s been largely successful. I have had no user reported issues uploading and processing image content, but noticed a few entries in Sidekiq dead queue triggered by a few remote posts.

extract_area: bad extract area

https://plasmatrap.com/notes/9t1auqkbl0
(There are many entries all related to this note via replies, etc, but this is the source post)

Related backtrace:

/usr/local/bundle/gems/ruby-vips-2.2.1/lib/vips/operation.rb:228:in `build'
/usr/local/bundle/gems/ruby-vips-2.2.1/lib/vips/operation.rb:481:in `call'
/usr/local/bundle/gems/ruby-vips-2.2.1/lib/vips/image.rb:229:in `method_missing'

hist_find_ndim: image is not 1 - 3 bands

https://graphics.social/users/metin/statuses/112404645439099091
https://mastodon.online/users/spocko/statuses/112401772350190886
https://mastodon.social/users/wearenew_public/statuses/112401409861904050
https://grafana.social/@grafana/112406569119122877

Related backtrace:

/usr/local/bundle/gems/ruby-vips-2.2.1/lib/vips/operation.rb:228:in `build'
/usr/local/bundle/gems/ruby-vips-2.2.1/lib/vips/operation.rb:481:in `call'
/usr/local/bundle/gems/ruby-vips-2.2.1/lib/vips/image.rb:229:in `method_missing'

VipsForeignSave: "/tmp/c0ddd4cf006863154a6ba8d7b30396bd20240507-7-ivyc3w.jfif" is not a known file format

https://mastodon.art/users/catrionaroberts/statuses/112400639207525684

Related backtrace:

/usr/local/bundle/gems/ruby-vips-2.2.1/lib/vips/image.rb:597:in `write_to_file'
/opt/mastodon/lib/paperclip/lazy_thumbnail.rb:47:in `make'
/usr/local/bundle/gems/kt-paperclip-7.2.2/lib/paperclip/processor.rb:33:in `make'

The JFIF file format error appears to be related to libvips/libvips#3775 which was fixed by a later version of libvips than what comes from the Debian repo. After this error appeared I switched to using the latest version of libvips compiled into the Docker container using vmstan#27 which will be submitted as a change after this PR has merged.

@vmstan
Copy link
Contributor

vmstan commented May 8, 2024

As far as performance data is concerned, so far I would say libvips has led to slightly lower container CPU utilization. I don't have any fine grained metrics on image processing performance, but comparing the last day worth of traffic to the same time period in the previous week, which had similar amounts of Sidekiq traffic processed:

Tuesday 7 into Wednesday 8 May 2024
CleanShot 2024-05-08 at 09 04 34@2x

Tuesday 30 April into Wednesday 1 May 2024
CleanShot 2024-05-08 at 09 05 49@2x

Average CPU usage (blue line) appears consistently lower, as do all load averages.

These are on three, 4x dedicated vCPU droplets at Digital Ocean. Average Sidekiq jobs per day approx 1.4 million.

@vmstan
Copy link
Contributor

vmstan commented May 9, 2024

I've run into an issue where uploading a PNG file that exceeds the allowed dimensions/megapixels results in an error of 422 Validation failed: File must be less than 16 MB, File file size must be less than 16 MB even if the file itself is less than 16 MB.

CleanShot 2024-05-09 at 16 09 20@2x

Uploading a file that is exactly 2160x3840 works, but a file that is 2161x3842 will result in the above error. In both cases the file is roughly 4.6 MB in size. So not only is the wrong type of error being triggered, it doesn't seem like any error should be triggered if the file was rescaled by vips.

I do not see this issue with JPG or HEIF files.

@LeoEurope
Copy link

I’ve been running this PR for the last 20 hours on vmst.io and it’s been largely successful.

@vmstan As a switch to libvips supposedly allows WebP and AVIF files to be rendered in link previews (closing #27370 and #14983, and probably many more) do you see that many of the issues listed here are resolved?

@vmstan
Copy link
Contributor

vmstan commented May 10, 2024

I’ve been running this PR for the last 20 hours on vmst.io and it’s been largely successful.

@vmstan As a switch to libvips supposedly allows WebP and AVIF files to be rendered in link previews (closing #27370 and #14983, and probably many more) do you see that many of the issues listed here are resolved?

I test posted all of the links shared in that comment and they looked the same on my instance running with vips as they do on mastodon.online running ImageMagick and similar build of 4.3.

@LeoEurope
Copy link

LeoEurope commented May 11, 2024

I’ve been running this PR for the last 20 hours on vmst.io and it’s been largely successful.

@vmstan As a switch to libvips supposedly allows WebP and AVIF files to be rendered in link previews (closing #27370 and #14983, and probably many more) do you see that many of the issues listed here are resolved?

I test posted all of the links shared in that comment and they looked the same on my instance running with vips as they do on mastodon.online running ImageMagick and similar build of 4.3.

OK, that's odd. As I've already bug spammed this ticket enough and this is a libvips implementation ticket let's get to the bottom of the issue using ticket #14983 and consider using ticket #27370 as a potential stopgap until the system accepts WebP and AVIF. Once libvips has properly landed I can be more helpful with troubleshooting.

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

Successfully merging this pull request may close these issues.

Consider: Switching from ImageMagick to Vips
5 participants