载波:如果尺寸大于(按条件创建版本),则缩放图像 [英] Carrierwave: Scale image if the size is larger than (conditionally create versions)

查看:75
本文介绍了载波:如果尺寸大于(按条件创建版本),则缩放图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当图像大于版本大小时,才可以使用carrierwave创建一个版本(例如,拇指)?

Is possible with carrierwave create a version (for example thumb) only if the image is larger than the size of the version??

示例:

version :thumb, :if => :is_thumbnable? do 
    process :resize_to_fit => [32,nil]
end

protected

def is_thumbnable?(file)
  image ||= MiniMagick::Image.open(file.path)
  if image.nil?
    if image['width'] >= 32 || image['height'] >= 32
      true
    else
      false
    end
  else
    false
  end
end


推荐答案

我定义了一种方法,如果图像超出给定宽度,则在这种情况下,请将其调整为您的32像素大小。将此代码放在ImageUploader中:

I defined method in which if image exceed given width then manipulate it to your size the 32 pixels in this case. Put this code in your ImageUploader:

  version :thumb do 
    process :resize_to_width => [32, nil]
  end

  def resize_to_width(width, height)
    manipulate! do |img|
      if img[:width] >= width
        img.resize "#{width}x#{img[:height]}"
      end
      img = yield(img) if block_given?
      img
    end
  end

这篇关于载波:如果尺寸大于(按条件创建版本),则缩放图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆