压缩图片——mini_magick

上次图片预览,显示压缩图片的一种处理方法:

def set_image_size
    #small_size = [172.0, 90.0] #设置图片大小为基础
    size = [[172.0, 90.0], [280.0, 288.0]] #设置图片大小为基础
    size.each_with_index do |small_size, index|
      img = MiniMagick::Image.open(self.path)
      w,h = img[:width], img[:height]
      zomm_size = 1
      unless w < small_size[0] && h < small_size[1]
        zomm_size = w/small_size[0] >= h/small_size[1] ? small_size[0]/w : small_size[1]/h
      end
      img.resize "#{w*zomm_size}x#{(h*zomm_size)}"
      if index == 0
        temp_image_disk = File.join(APP_CONFIG[:temp_image_path], self.id.to_s, "small")
      else
        temp_image_disk = File.join(APP_CONFIG[:temp_image_path], self.id.to_s, "middle")
      end

      FileUtils.mkdir_p(temp_image_disk) unless File.exist?(temp_image_disk)
      temp_image_path = File.join(temp_image_disk, self.filename)
      img.write(temp_image_path)
    end
  end

 

 

你可能感兴趣的:(Rails,mini_magick)