Image Resizing Service [Rails 5]

问题内容:

I’m building a project about Uploading image and resizing them, for the moment we are using gem FastImage which is perfect for getting image url. But i can’t find any Resizing Gem.

What i want!

  • Get size of that image
  • Get width
  • Get height
  • Resize that image (would be perfect if i can scale that image, no croping).

问题评论:

答案:

答案1:

I recommend you to take a look on Carrierwave gem. It offers everything you want including the resizing part.

You can upload a image by a remote url as:

<%= form_for @user, html: { multipart: true } do |f| %>
  <p>
    <label>My Avatar URL:</label>
    <%= image_tag(@user.avatar_url) if @user.avatar? %>
    <%= f.text_field :remote_avatar_url %>
  </p>
<% end %>

And maybe resize it as:

class ImageUploader < CarrierWave::Uploader::Base

  version :resized do
    # returns an image with a maximum width of 100px 
    # while maintaining the aspect ratio
    # 10000 is used to tell CW that the height is free 
    # and so that it will hit the 100 px width first
    process :resize_to_fit => [100, 10000]
  end

end

答案评论:

    
Thanks for your answer, but i need to do this manually not (hard code) to model 🙂
– egzonszo
yesterday

原文地址:

https://stackoverflow.com/questions/47739230/image-resizing-service-rails-5

添加评论

友情链接:蝴蝶教程