【问题标题】:how do you apply this convert command in carrierwave? - crop and circle你如何在carrierwave中应用这个转换命令? - 裁剪和圆圈
【发布时间】:2012-02-12 06:17:42
【问题描述】:

你如何从这个答案发送命令:Crop or mask an image into a circle

在载波中?

编辑:我现在有这个:

version :thumb_circle do    
  # process 'convert -size 350x350 xc:transparent -fill original.jpeg -draw "circle 240,90 300,45" -crop 152x152+164+15 +repage thumb.png'    
  process :crop_to_circle    
end

def crop_to_circle    
  manipulate! do |img|    
    img.crop "152x152+164+15"    
  end     
end

我不知道如何从那里着手。我想做的是在注释行上

【问题讨论】:

    标签: ruby-on-rails carrierwave


    【解决方案1】:

    您可以使用process 传递必要的参数(也称为命令)。例如:

    process :size => [200, 200]
    process :xc => 'none'
    process :fill => 'walter.jpg'
    # etc etc
    

    有关更多信息,请查看 herehere(假设您使用 MiniMagick 作为后端)。

    【讨论】:

    • 我需要比您提供的链接和示例更多的帮助。我收到错误:未定义的方法 `xc' 或 :size 的参数数量错误(2 代表 0)。我可能知道我需要为此编写定义,但不太确定如何编写。
    • 例如在链接上它说:process :sepiatone, :vignette 但这是否意味着当它已经存在于图像魔法中时我必须定义它们?
    • 对不起,我似乎误解了文档。
    • 好的,感谢您的帮助。我正在寻找与使用 Paperclip 一样简单的东西。使用 Paperclip,您可以这样做:“:convert_options => { :all => '-background white -flatten +matte' }” - 传递参数,Carrierwave 有类似的东西吗?
    • @David,你在carrierwave中找到png到jpg白色背景的答案了吗?
    【解决方案2】:
    process convert: :png
    
    def filename
      "#{original_filename.split('.').first}.png" if original_filename
    end
    
    def cache_name
      File.join(cache_id, [version_name, filename].compact.join('_')) if cache_id and original_filename
    end
    
    version :thumb do
      process :crop_to_get_square_image # you already did this
      process :round_image
      process :any_resize_or_process
    end
    
    private
    
    def round_image
      manipulate! do |img|
    
        path = img.path
    
        new_tmp_path = File.join(Rails.root, 'public', cache_dir, "/round_#{File.basename(path)}")
    
        width, height = img[:dimensions]
    
        radius_point = ((width > height) ? [width / 2, height] : [width, height / 2]).join(',')
    
        imagemagick_command = ['convert',
                             "-size #{ width }x#{ height }",
                             'xc:transparent',
                             "-fill #{ path }",
                             "-draw 'circle #{ width / 2 },#{ height / 2 } #{ radius_point }'",
                             "+repage #{new_tmp_path}"].join(' ')
    
        system(imagemagick_command)
        MiniMagick::Image.open(new_tmp_path)
      end
    end
    

    已编辑:这将使图像圆形化。

    【讨论】:

      猜你喜欢
      • 2013-02-05
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      相关资源
      最近更新 更多