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

load_data does not work #19

Closed
mantas opened this issue Jan 10, 2024 · 2 comments
Closed

load_data does not work #19

mantas opened this issue Jan 10, 2024 · 2 comments
Assignees

Comments

@mantas
Copy link

mantas commented Jan 10, 2024

Looks like Rszr::Image.load_data does not seem to work correctly. Same data blob loads just fine via Rszr::Image.load from a file. Looks like the problem is that Tempfile.create removes the tempfile right after loading the image. But that tempfile is needed for processing image later on.

Following test replicates the issue:

diff --git a/spec/rszr_spec.rb b/spec/rszr_spec.rb
index e5db69d..02216f9 100644
--- a/spec/rszr_spec.rb
+++ b/spec/rszr_spec.rb
@@ -18,6 +18,12 @@ RSpec.describe 'Rszr' do
       expect(Rszr::Image.load_data(RSpec.root.join('images/test.jpg').binread).format).to eq('jpeg')
     end
 
+    it 'loads image from memory' do
+      img = Rszr::Image.load_data(RSpec.root.join('images/test.jpg').binread)
+
+      expect(img[1,1]).to have_attributes(green: 93, blue: 112, red: 78)
+    end
+
     it 'loads images with uppercase extension' do
       expect(Rszr::Image.load(RSpec.root.join('images/CHUNKY.PNG'))).to be_kind_of(Rszr::Image)
     end

A possible solution would be to make a copy of image before temporary file is removed. But this is not good from performance point of view. On the other hand, is there a better solution? The other way would be to leave temporary file around till OS cleans it up. Which may take up a lot of space during mass image processing.

diff --git a/lib/rszr/image.rb b/lib/rszr/image.rb
index db6d14d..73061fd 100644
--- a/lib/rszr/image.rb
+++ b/lib/rszr/image.rb
@@ -20,7 +20,7 @@ module Rszr
       def load_data(data, autorotate: Rszr.autorotate, **opts)
         raise LoadError, 'Unknown format' unless format = identify(data)
         with_tempfile(format, data) do |file|
-          load(file.path, autorotate: autorotate, **opts)
+          load(file.path, autorotate: autorotate, **opts).dup
         end
       end
@mtgrosser mtgrosser self-assigned this Jan 10, 2024
mtgrosser added a commit that referenced this issue Jan 10, 2024
@mtgrosser
Copy link
Owner

Thank you for reporting and looking into this issue. As of v1.4.0, imlib_load_image_immediately_without_cache is used for loading images from tempfile data.

@mantas
Copy link
Author

mantas commented Jan 11, 2024

Nice. Works great now. Thank you for the quick bugfix!

@mantas mantas closed this as completed Jan 11, 2024
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

No branches or pull requests

2 participants