Skip to content

Instantly share code, notes, and snippets.

@ksaynice
Created September 22, 2015 17:59
Show Gist options
  • Save ksaynice/09f8790711d64a2969aa to your computer and use it in GitHub Desktop.
Save ksaynice/09f8790711d64a2969aa to your computer and use it in GitHub Desktop.
class ProcessImage
class ResizeCover < ProcessImage::Base
include Interactor
include MomCatalogModels::Fileable
GRID_W = 960
GRID_H = 1200
def call
magic_resize_cover
end
private
def magic_resize_cover
resize_worker
rescue => error
context.fail! :message => "Failed to resize image file #{work_path_file} with errors: #{error}"
end
def image
@_image ||= GraphicsMagick::Image.new work_path_file
end
def work_file_ratio
@_work_file_ratio ||= get_ratio
end
def height
@_height ||= image.height
end
def width
@_width ||= image.width
end
def bg_img_file
@_bg_img_file ||= File.join(File.expand_path(work_path), "tmp_bg_#{File.basename(work_path_file)}")
end
def resize_worker
if work_file_ratio >= 0.75 && work_file_ratio <= 0.85
expand_img_with_4x5
elsif work_file_ratio < 0.8 # TODO: is this correct, shouldn't it be 0.75? Check with K.
doing_img_for_less_75
else
doing_img_for_greater_85
end
end
def expand_img_with_4x5
grid = if height >= GRID_H
"#{GRID_W}x#{GRID_H}!"
else
"#{(height * 0.8).to_i}x#{height}!"
end
image.resize grid
image.write dest_file
rescue
nil
end
def doing_img_for_less_75
if width >= GRID_W
# img > default grid
bg_grid_w = GRID_W
bg_grid_h = GRID_H
else
# img < default grid
bg_grid_w = (height * 0.8).to_i
bg_grid_h = height
end
# calculate all image params
bg_resize_after_h = (bg_grid_w / work_file_ratio).to_i
bg_resize_opt = "#{bg_grid_w}x#{bg_resize_after_h}!"
bg_crop_offset = (bg_resize_after_h - bg_grid_h) / 2
bg_crop_opt = "#{bg_grid_w}x#{bg_grid_h}+0+#{bg_crop_offset}"
over_resize_w = (bg_grid_h * work_file_ratio).to_i
over_resize_opt = "#{over_resize_w}x#{bg_grid_h}!"
over_off_set = (bg_grid_w - over_resize_w) / 2
over_geometry_opt = "+#{over_off_set}"
# making cover image
make_tmp_bg_img bg_resize_opt, bg_crop_opt
make_composite_img over_resize_opt, over_geometry_opt
safe_remove bg_img_file
end
def doing_img_for_greater_85
if height >= GRID_W
# img > default grid
bg_grid_w = GRID_W
bg_grid_h = GRID_H
else
# img < default grid
bg_grid_w = width
bg_grid_h = (width / 0.8).to_i
end
# calculate all image params
bg_resize_after_w = (bg_grid_h * work_file_ratio).to_i
bg_resize_opt = "#{bg_resize_after_w}x#{bg_grid_h}!"
bg_crop_offset = (bg_resize_after_w - bg_grid_w) / 2
bg_crop_opt = "#{bg_grid_w}x#{bg_grid_h}+#{bg_crop_offset}+0"
over_resize_h = (bg_grid_w / work_file_ratio).to_i
over_resize_opt = "#{bg_grid_w}x#{over_resize_h}!"
over_off_set = (bg_grid_h - over_resize_h) / 2
over_geometry_opt = "+0+#{over_off_set}"
# making cover image
make_tmp_bg_img bg_resize_opt, bg_crop_opt
make_composite_img over_resize_opt, over_geometry_opt
safe_remove bg_img_file
end
def get_ratio
(width.to_f / height.to_f).round(2)
end
def make_tmp_bg_img(bg_resize_opt, bg_crop_opt)
image.resize(bg_resize_opt).blur('0x7').crop(bg_crop_opt).fill('black').colorize('50%').quality('92')
image.write bg_img_file
end
def make_composite_img(over_resize_opt, over_geometry_opt)
image.resize(over_resize_opt).composite(bg_img_file).geometry(over_geometry_opt).quality('92')
image.write dest_file
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment