mtgrosser / rszr Goto Github PK
View Code? Open in Web Editor NEWFast image resizer for Ruby
Home Page: https://mtgrosser.github.io/rszr/resizing.html
License: MIT License
Fast image resizer for Ruby
Home Page: https://mtgrosser.github.io/rszr/resizing.html
License: MIT License
If i try to save an image as webp, i get this error:
Rszr::SaveError: Unsupported format
On my development system everything works, but on the production system the script fails, but JPG saving is possible.
Both are Ubuntu 5.4.0-65-generic
Any Ideas? Thanks!
ruby 2.6
5: from /Users/kevin/projects/rails/config/initializers/rszr.rb:1:in `<top (required)>'
4: from /Users/kevin/.rvm/gems/ruby-2.6.6/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:291:in `require'
3: from /Users/kevin/.rvm/gems/ruby-2.6.6/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:257:in `load_dependency'
2: from /Users/kevin/.rvm/gems/ruby-2.6.6/gems/activesupport-5.2.4.4/lib/active_support/dependencies.rb:291:in `block in require'
1: from /Users/kevin/.rvm/gems/ruby-2.6.6/gems/skylight-core-4.3.1/lib/skylight/core/probes.rb:118:in `require'
/Users/kevin/.rvm/gems/ruby-2.6.6/gems/skylight-core-4.3.1/lib/skylight/core/probes.rb:118:in `require': cannot load such file -- rszr/image_processing (LoadError)
I see some color handling here, though am stumped if you could use these to convert a color image to greyscale. Is that possible? How might you do that?
It would be nice to be able to load images from memory directly without requiring to have the image file on disk first. Also reading the raw image data directly would be nice, e.g. for uploading it to S3
The most recently created instance of Rszr::Image
doesn't get garbage collected during the test. All previously created instances are released. This problem does not occur on Ruby 2.3.
I found a strange behaviour while converting to webp:
I use a background job to convert jpg to webp. Performing this job immediately by ConvertJob.new.perform
works perfect. But when i use ConvertJob.perform_async
(managed by Sidekiq) the webp-file is created with a size of zero and the job fails silently.
But conversion to png works async. Trying to convert to a unsupported fileformat gives a regular exception.
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
Rszr should support EXIF orientation.
Today I went into issues with threads. After some research I found your that imlib2 it self has issues with thread safe.
I needed to use Mutex.new
to let it work with threads. I also did some benchmarks, it's still much faster then using libgd for resizing images with 4 puma worker and 32 threads for each.
def self.semaphore
@semaphore ||= Mutex.new
end
def resize_image
self.class.semaphore.synchronize do
# code to resize image
end
end
This information should be as hint somewhere.
A FloatDomainError occured: Infinity
Backtrace:
vendor/bundle/ruby/2.3.0/gems/rszr-0.3.1/lib/rszr/image.rb:158:in `round'
vendor/bundle/ruby/2.3.0/gems/rszr-0.3.1/lib/rszr/image.rb:158:in `create_resized_image'
vendor/bundle/ruby/2.3.0/gems/rszr-0.3.1/lib/rszr/image.rb:62:in `resize'
If I run the example, in the README with rszr 1.2.1, the image
variable doesn't actually change, because resize
returns a new value rather than mutating the image in-place:
image = Rszr::Image.load('image.jpg')
puts image.dimensions # [2623, 3934]
image.resize(400, 300)
puts image.dimensions # [2623, 3934]
I think the example should go:
image = Rszr::Image.load('image.jpg')
-image.resize(400, 300)
+image = image.resize(400, 300)
but I'm not sure if the mistake is in the code or the example.
For some large jpeg pics, will throw error when try to call Rszr::Image.load
.
Tried:
pngcheck
, it shows okpngfix
, still throwA declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.