All threads / How to do convert this ImageMagick command and parameters into a Ruby code to be used with Carrierwave

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to do convert this ImageMagick command and parameters into a Ruby code to be used with Carrierwave

W.M. asked in Rails

Basically I am asking how to do this:

convert input.png -bordercolor white -border 1 -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]" -virtual-pixel edge -distort SRT 0 output.png

inside a Carrierwave uploader class.

Note: this command makes rectangular images into squares with white background.

Thank you.

Thanks to Jancko-m at github, he provided the following solution:

MiniMagick::Tool::Convert.new do |cmd|
  convert << "input.png"
  convert.bordercolor("white")
  convert.border("1"),
  convert.set("option:distort:viewport", "%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]")
  convert.virtual_pixel("edge")
  convert.distort("SRT", "0")
  convert << "output.png"
end
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,885+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.