【问题标题】:Carrierwave PNG to JPG converter. How to avoid black background?Carrierwave PNG 到 JPG 转换器。如何避免黑色背景?
【发布时间】:2012-07-11 20:05:15
【问题描述】:

例如在回形针中,当.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

【问题讨论】:

    标签: ruby-on-rails imagemagick paperclip carrierwave rmagick


    【解决方案1】:

    这里是只做转换和背景填充的更健全的版本

    def convert_and_fill(format, fill_color)
      manipulate!(format: format) do |img|
        new_img = ::Magick::Image.new(img.columns, img.rows)
        new_img = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(fill_color))
        new_img.composite!(img, ::Magick::CenterGravity, ::Magick::OverCompositeOp)
        new_img = yield(new_img) if block_given?
        new_img
      end
    end
    

    示例用法:

    process convert_and_fill: [:jpg, "#FFFFFF"]
    

    【讨论】:

      【解决方案2】:

      使用 MiniMagick,我可以做到这一点:

      process :resize_and_pad => [140, 80, "#FFFFFF", "Center"]

      【讨论】:

        【解决方案3】:

        我已经解决了这个问题,但我不确定这是否是最好的方法:

        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
        

        【讨论】:

          【解决方案4】:

          我使用 MiniMagick 的解决方案:首先,在您的上传器上定义一个方法,将图像转换为 jpg 格式,同时移除 alpha 通道并将背景颜色设置为白色:

          def convert_to_jpg(bg_color = '#FFFFFF')
            manipulate! do |image|
              image.background(bg_color)
              image.alpha('remove')
              image.format('jpg')
            end
          end
          

          然后添加一个将文件转换为 jpg 的新版本(同时覆盖方法 full_filename 以更改文件名的扩展名):

          version :jpg do
            process :convert_to_jpg
          
            def full_filename(file)
              filename = super(file)
              basename = File.basename(filename, File.extname(filename))
              return "#{basename}.jpg"
            end
          end
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-09-24
            • 1970-01-01
            • 1970-01-01
            • 2013-04-23
            • 2011-08-31
            • 2016-01-12
            • 2019-12-13
            • 2017-11-10
            相关资源
            最近更新 更多