RMagick with the direct form presented Image by Sinatra

I am trying to do something very simple using Sinatra and RMagick .

  • Make the image through a simple file upload form.
  • Use RMagick to resize.
  • Then save it to the database for perseverance (inappropriate)

But after passing the RDocs and the endless head-test, I can’t get the shape image for the RMagick object cleanly .

This is a terrible thing that works for me now:

def image_resize(img_data)
    filecount = rand
    writer = File.new("/tmp/#{filecount}.jpg", "w")
    writer.puts(img_data)
    writer.close

    resized_image = Magick::ImageList.new("/tmp/#{filecount}.jpg").first
    resized_image.crop_resized!(100,100, Magick::NorthGravity)
    resized.format = 'jpeg'
    resized_image.to_blob
end

#call the method with my form image data
image_resize(params[:image][:tempfile].read)

So, how can I do the obvious right thing and just bind the form image data directly to the RMagick object without the need to write and read a disc.

Magick:: Image ImageLists, , .


-1,2.

+3
2

tempfile Magick:: Images read.

:

post "/upload-photo" do
  image = Magick::Image.read(params[:image][:tempfile].path)[0]
  image.crop_resized! 100, 100, Magick::CenterGravity
  store_image_data image.to_blob

  redirect "/done"
end
+3

ActionDispatch:: Http:: UploadedFile :

image = Magick::Image.from_blob(params[:image].read)
0

Source: https://habr.com/ru/post/1761837/


All Articles