2

how do you send the command from this answer: Crop or mask an image into a circle

in carrierwave?

edit: I have this right now:

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

And I don't know how to proceed from there. What I wanted to do is on the commented line

Community
  • 1
  • 1
David
  • 4,235
  • 12
  • 44
  • 52

2 Answers2

1
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

Edited: This will circularize an image.

Nox
  • 395
  • 3
  • 22
1

You can pass in the necessary arguments (aka. commands) using process. For example:

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

For more information, take a look here and here (suppose you're using MiniMagick as the backend).

Limbo Peng
  • 2,485
  • 19
  • 12
  • i need a little more help than the link you provided and your example. I am getting error: undefined method `xc' or wrong number of arguments (2 for 0) for :size. I may know that I need to write the definitions for this but not really quite sure how to. – David Feb 12 '12 at 06:41
  • for instance on the link it says this: process :sepiatone, :vignette but does that mean i have to define those when it already exist in image magick? – David Feb 12 '12 at 06:47
  • Sorry, it seems that I misunderstood the documentation. – Limbo Peng Feb 12 '12 at 06:58
  • 2
    okay thanks for trying to help though. i am looking for something as easy as how you would do it with Paperclip. With Paperclip you can just do something like this: ":convert_options => { :all => '-background white -flatten +matte' }" - passing the parameters, does Carrierwave have something like that? – David Feb 12 '12 at 07:14
  • @David, did you find the answer for white backgrounds for png to jpg in carrierwave? – Luccas Jul 11 '12 at 01:23