Created
August 5, 2015 07:34
-
-
Save rgalindo33/3f33c3e0081ac99d1545 to your computer and use it in GitHub Desktop.
generate reviews images using rmagick
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' | |
module ImageGenerator | |
class KununuRatingImage | |
include Magick | |
include CorporatePages::ReviewsHelper | |
IMAGES_PATH = "#{Rails.root}/public/assets/companies/img/kununu_ratings" | |
BACKGROUND_COLOR = '#189898' | |
FONT = 'Helvetica' | |
FONT_SIZE = 34 | |
FONT_COLOUR = '#ffffff' | |
attr_reader :rating | |
def initialize(rating) | |
@rating = rating | |
end | |
def generate | |
draw_kununu_rating_avg | |
draw_kununu_stars | |
generate_image | |
end | |
private | |
def canvas | |
@canvas ||= Image.new(128,128) { self.background_color = BACKGROUND_COLOR } | |
end | |
def draw_kununu_rating_avg | |
rating_text = Draw.new | |
canvas.annotate(rating_text, 0, 0, 30, 60, rating.to_s) do | |
rating_text.pointsize = FONT_SIZE | |
rating_text.font = FONT | |
rating_text.fill = FONT_COLOUR | |
rating_text.font_weight = Magick::BoldWeight | |
end | |
end | |
def draw_kununu_stars | |
star_0 = ImageList.new("#{IMAGES_PATH}/stars/white_empty.png").resize_to_fit!(14,14) | |
star_50 = ImageList.new("#{IMAGES_PATH}/stars/white_half.png").resize_to_fit!(14,14) | |
star_100 = ImageList.new("#{IMAGES_PATH}/stars/white_full.png").resize_to_fit!(14,14) | |
left_margin = 24 | |
star_size = 16 | |
5.times do |i| | |
star_image_porcentage = kununu_rounded_rating(rating.to_f, i + 1) | |
canvas.composite!(eval("star_#{star_image_porcentage}"), WestGravity, left_margin + (star_size * i), 20, OverCompositeOp) | |
end | |
end | |
def generate_image | |
canvas.write "#{IMAGES_PATH}/kununu_review_#{rating}.png" | |
end | |
end | |
end | |
module ImageGenerator | |
class KununuRatingImagesGenerator | |
RATINGS_ARRAY = (1.0...5.0).step(0.01).map { |i| sprintf("%.2f", i) } | |
def self.run | |
RATINGS_ARRAY.each do |rating| | |
KununuRatingImage.new(rating).generate | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment