libvips / libvips/ruby-vips

Vips::Image.new_from_file Swaps Width/Height in Rails Server, but Not Console

Open
#423 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
904
Forks
64
PR merge metrics
No merged PRs in 30d

Description

Bug Description

When loading an image from a file path, Vips::Image.new_from_file(path) returns swapped width and height dimensions. This issue only occurs when the code is executed within a Rails server process (e.g., Puma/Unicorn). The exact same code, reading the exact same file path, returns the correct dimensions when run from a rails console.

A peculiar workaround is that providing any optional argument to the method call (e.g., fail: true or autorotate: false) corrects the behavior in the server environment. This suggests a potential issue with how default options are handled or a caching-related problem specific to the server runtime.

Steps to Reproduce
  1. Place a binding.pry in any Rails controller action.

  2. In the pry session, download an image that has EXIF orientation data:

require 'down'
url = 'https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Landscape_6.jpg'
tempfile = Down.download(url)
  1. Load the image, apply autorot, and overwrite the tempfile:
image = Vips::Image.new_from_file(tempfile.path)
image.autorot.write_to_file(tempfile.path)
  1. Reload the image from the same path without any options and inspect its dimensions. The dimensions will be swapped.
# In the server pry session
reloaded_image = Vips::Image.new_from_file(tempfile.path)
puts "BUG -> In Server: #{reloaded_image.width}x#{reloaded_image.height}" 
# Example Output: BUG -> In Server: 3024x4032
path = tempfile.path # Copy this path for the next step
  1. In a new terminal, open a rails console. Use the path from the previous step to load the same file:
# In the rails console session
console_image = Vips::Image.new_from_file('PASTE_THE_TEMPFILE_PATH_HERE')
puts "OK -> In Console: #{console_image.width}x#{console_image.height}"
# Example Output: OK -> In Console: 4032x3024
  1. Go back to the server pry session. Run the same command but add any option, like fail: true. The dimensions will now be correct.
# Back in the server pry session
workaround_image = Vips::Image.new_from_file(tempfile.path, fail: true)
puts "FIXED -> Server with option: #{workaround_image.width}x#{workaround_image.height}"
# Example Output: FIXED -> Server with option: 4032x3024
Expected Behavior

Vips::Image.new_from_file(tempfile.path) should consistently return the correct dimensions (4032x3024 for the example image) in all execution environments, including the Rails server.

Actual Behavior

The dimensions are swapped (3024x4032) only when run inside a Rails server without any extra options. The dimensions are correct when run in a Rails console or when any option is added to the method call in the server.

Screenshots

This is the correct behaviour after adding an option

Image

Top terminal is the Rails server; bottom terminal is the Rails console:

Image
Desktop
  • OS:Fedora 42(local) and ubuntu (production environment)
  • Ruby-vips 2.2.3
  • libvips 8.16.1 (on all envs)

Thank you in advance

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with Vips::Image.new_from_file in a Rails server pry session and a Rails console, using the EXIF-oriented example image and the same tempfile path. Compare calls with no options and with fail: true, then trace how ruby-vips handles default options and image metadata in each runtime. Done means dimensions remain correct without optional arguments in both environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
backend, computer-vision
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.