加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Carrierwave:如何裁剪和调整大小以使最终图

发布时间:2020-12-17 03:37:37 所属栏目:百科 来源:网络整理
导读:我正在使用Rails 3和carrierwave以及Rmagick. 我希望将图片精确调整为192 x 135 这起初很简单,但直到现在我才尝试过. 有人找到了解决方案吗?这是我的上传代码. class AvatarUploader CarrierWave::Uploader::Base #Include RMagick or MiniMagick support:
我正在使用Rails 3和carrierwave以及Rmagick.
我希望将图片精确调整为192 x 135
这起初很简单,但直到现在我才尝试过.

有人找到了解决方案吗?这是我的上传代码.

class AvatarUploader < CarrierWave::Uploader::Base

   #Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end


  # Provide a default URL as a default if there hasn't been a file uploaded:
   def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
   ActionController::Base.helpers.asset_path("fallback/" + [version_name,"default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name,"default.png"].compact.join('_')
   end
  process :resize_to_fit => [250,250]


  # Process files as they are uploaded:
  # process :scale => [200,300]
  #
  # def scale(width,height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
   version :thumb do
     process :resize_to_fit => [200,200]
   end

   version :medium do
       process :resize_to_fit => [250,250]
    end

    version :mini do
        process :resize_to_fit => [100,nil]
        process crop: '100x100+0+0'
     end

     version :grid do
         process :resize_to_fit => [192,135]
         process crop: '192x135+0+0'
      end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here,see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

  private

    # Simplest way
    def crop(geometry)
      manipulate! do |img|      
        img.crop(geometry)
        img
      end    
    end

    # Resize and crop square from Center
    def resize_and_crop(size)  
      manipulate! do |image|                 
        if image[:width] < image[:height]
          remove = ((image[:height] - image[:width])/2).round 
          image.shave("0x#{remove}") 
        elsif image[:width] > image[:height] 
          remove = ((image[:width] - image[:height])/2).round
          image.shave("#{remove}x0")
        end
        image.resize("#{size}x#{size}")
        image
      end
    end

end

解决方法

改变你的路线

process :resize_to_fit => [250,250]

process :resize_to_fill => [192,135]

一个月前我问了一个similar question.希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读