Giter Site home page Giter Site logo

mtgrosser / rszr Goto Github PK

View Code? Open in Web Editor NEW
32.0 1.0 1.0 3.09 MB

Fast image resizer for Ruby

Home Page: https://mtgrosser.github.io/rszr/resizing.html

License: MIT License

Ruby 67.27% Shell 0.17% C 32.56%
ruby image-resizer rails activestorage image-resizing image-processing imlib2 autorotation image-filtering

rszr's People

Contributors

dependabot[bot] avatar mtgrosser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

mantas

rszr's Issues

Rszr::SaveError: Unsupported format

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!

cannot load such file -- rszr/image_processing (LoadError)

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)

Greyscale?

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?

GC test failing on Ruby 2.4+

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.

Async conversion to webp fails

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.

load_data does not work

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

Thread safe issues with imlib2

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.

FloatDomainError: Infinity caused by illegal resize dimensions

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'

The example in the README is misleading

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.