5

たとえば、ペーパークリップでは、これを追加して、.png を .jpg に変換するときに白い背景を設定することができます。

:convert_options => { :all => '-background white -flatten +matte'}

Carrierwave も rmagick を使用したら、どうすればよいですか?

Obs .: 私のファイルは S3 に保存されています。

私のコード:

version :square do
    process :resize_to_fill => [200, 200]
    process :convert => 'jpg'
end
4

4 に答える 4

1

私は問題を解決しましたが、これが最善のアプローチであるかどうかはわかりません:

def resize_to_fill(width, height, gravity = 'Center', color = "white")
    manipulate! do |img|
      cols, rows = img[:dimensions]
      img.combine_options do |cmd|
        if width != cols || height != rows
          scale = [width/cols.to_f, height/rows.to_f].max
          cols = (scale * (cols + 0.5)).round
          rows = (scale * (rows + 0.5)).round
          cmd.resize "#{cols}x#{rows}"
        end
        cmd.gravity gravity
        cmd.background "rgba(255,255,255,0.0)"
        cmd.extent "#{width}x#{height}" if cols != width || rows != height
      end
      ilist = Magick::ImageList.new
      rows < cols ? dim = rows : dim = cols
      ilist.new_image(dim, dim) { self.background_color = "#{color}" }
      ilist.from_blob(img.to_blob)
      img = ilist.flatten_images
      img = yield(img) if block_given?
      img
    end
  end
于 2012-07-12T04:20:47.360 に答える