rubyimage-processingruby-on-rails-6minimagickshrine

Converting GIF to JPEG with Shrine and MiniMagick


I have this project in ruby on rails and recently I changed the upload image functionality to Shrine.

I want be able to upload an animated gif and then create an static jpeg (or gif if it's easier) derivative.

I am trying this way

    class GifUploader < Shrine
      Attacher.derivatives do |original|
        magick = ImageProcessing::MiniMagick
    
        {
            static: magick.loader(page: 0)
                        .loader(geometry: "450x250")
                        .call(original),
        }
      end
    end

With this approach I received this error

MiniMagick::Error: `convert /tmp/shrine20211117-94958-ieep9h.gif[0][450x250] -au
to-orient /tmp/image_processing20211117-94958-q3i2qe.gif` failed with error:    
convert-im6.q16: unable to open image `/tmp/shrine20211117-94958-ieep9h.gif[0][450x250]': No such file or directory @ error/blob.c/OpenBlob/2924.
convert-im6.q16: no decode delegate for this image format `' @ error/constitute.c/ReadImage/575.
convert-im6.q16: no images defined `/tmp/image_processing20211117-94958-q3i2qe.gif' @ error/convert.c/ConvertImageCommand/3229.

I also tried a different approach and gave me the same error:

require "image_processing/mini_magick"

class GifUploader < Shrine
  Attacher.derivatives do |original|
    magick = ImageProcessing::MiniMagick.source(original)
    {
        static: magick   # original is a IO object   
                    .loader(page: 0)
                    .loader(geometry: "450x250")
                    .convert!("jpeg")
                    .call(original),
    }
  end
end

This is my first project in ruby on rails and I don't know what is happening. I already searched for it, but no results

I am using ruby on rails 6


Solution

  • Okay, I managed to fix this. Basically I inverted the order and get rid of any "!" points. I also deleted the loader(geometry: "450x250")

    The code:

    require "image_processing/mini_magick"
    
    class GifUploader < Shrine
      Attacher.derivatives do |original|
        magick = ImageProcessing::MiniMagick.source(original)
        {
          static: magick.convert("jpeg").loader(page: 0).call
        }
      end
    end
    

    I don't know why this worked, if anyone knows please explain in the comments