class WordsToImage::Image

Public Class Methods

new(path) click to toggle source
# File lib/words_to_image/image.rb, line 5
def initialize(path)
  @path = path
  @square_size = 150 #px
end

Public Instance Methods

delete!() click to toggle source
# File lib/words_to_image/image.rb, line 49
def delete!
  File.delete(filename)
rescue
  puts "! could not delete temporary file: #{filename}"
end
download() click to toggle source
# File lib/words_to_image/image.rb, line 33
def download
  open(URI.parse(@path)) { |image|
    File.open(filename, "wb") do |file|
      file.puts image.read
    end
  }

  self
rescue => e
  raise IOError, "image download error: #{e.message}"
end
filename() click to toggle source
# File lib/words_to_image/image.rb, line 45
def filename
  @filename ||= "img_#{Time.now.to_f}#{@path[/\.\w+$/]}"
end
squarize!() click to toggle source
# File lib/words_to_image/image.rb, line 10
def squarize!
  img = MiniMagick::Image.new(filename)

  w_original, h_original = [img[:width].to_f, img[:height].to_f]

  # check proportions
  if w_original < h_original
    bigger_side = (h_original * @square_size / w_original).to_i
    remove = "0x#{ ((bigger_side - @square_size)/2.0).to_i }"
  else
    bigger_side = (w_original * @square_size / h_original).to_i
    remove = "#{ ((bigger_side - @square_size)/2.0).to_i }x0"
  end
  op_resize = [bigger_side, bigger_side].join("x")

  img.resize(op_resize)
  img.shave(remove)

  self
rescue => e
  raise IOError, "failed to modify an image: #{e.message}"
end