Skip to content

Instantly share code, notes, and snippets.

@diasjorge
Created December 15, 2008 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diasjorge/35966 to your computer and use it in GitHub Desktop.
Save diasjorge/35966 to your computer and use it in GitHub Desktop.
def create_thumbnail(addendum, new_width, new_height)
width, height = get_resolution
logger.debug "width=#{width}, height=#{height}"
needs_resize = (new_width < width) && (new_height < height)
ratio = new_width.to_f / new_height.to_f # Watch out for the floats
if needs_resize
logger.debug "needs rez"
# NEW FORMULA
# First step crops a rectangle proportional to new dimensions from the center of the original image
# After that, new rectangle is resized to fit new dimensions
if width > height
logger.debug "ancha de caderas"
current_height = height
current_width = current_height * ratio
top = 0
left = width/2 - current_width/2
elsif height > width
logger.debug "estrecha y altota"
current_width = width
current_height = current_width / ratio
left = 0
top = height/2 - current_height/2
else
logger.debug "chica barrilín"
current_width = width
current_height = current_width / ratio
left = 0
top = (height-current_height)/2
end
logger.debug "left=#{left}, top=#{top}, current_w=#{current_width}, current_h=#{current_height} ratio=#{ratio}"
thumb = @image.crop(top, left, current_width, current_height).scale(new_width, new_height)
else
thumb = @image.first
end
thumb.write("#{get_filename_base}#{addendum}.#{get_extension}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment