Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
smartcropper document generator
require 'RMagick'
require 'smartcropper'
class DemoChopper
def initialize width, height, filename
@width, @height, @filename = width, height, filename
@files = [{
image: ::Magick::ImageList.new(filename).resize_to_fit!(600, 1000),
name: outfile("orig"),
method: "original",
method_s: "original"
}]
@methods = ["smart_crop", "smart_crop_and_scale", "smart_square"]
@methods.each do |method|
sc = SmartCropper.from_file(filename)
if method == "smart_square"
image = sc.send(method)
method_s = method
else
image = sc.send(method, width, height)
method_s = "#{method} #{@height}, #{@width}"
end
image.resize!(0.5)
@files << {image: image, name: outfile(method), method: method, method_s: method_s}
end
end
def write
@files.each {|f| f[:image].write f[:name] }
end
def html
out = "<div class=\"group\">"
@files.each do |file|
out += "
<figure>
<figcaption>#{file[:method_s]}</figcaption>
<img src=\"#{file[:name]}\" alt=\"#{file[:method_s]}\" />
</figure>"
end
out += "</div>"
end
private
def outfile method
"#{File.basename(@filename, '.jpg')}_#{method}.jpg"
end
end
['ts.jpg', 'sikh.jpg', 'carice.jpg', 'banksy.jpg'].each do |sample|
dc = DemoChopper.new(300, 400, sample)
dc.write
puts dc.html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment