Created
November 28, 2012 14:58
-
-
Save berkes/4161796 to your computer and use it in GitHub Desktop.
smartcropper document generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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