images get ratios

This commit is contained in:
2022-12-14 13:11:36 +02:00
parent 2685963fd0
commit d7045946da
11 changed files with 88 additions and 58 deletions

View File

@ -1,3 +1,5 @@
require "mini_magick"
module Merged
class Image
include ActiveModel::API
@ -15,18 +17,27 @@ module Merged
end
end
attr_reader :name , :type , :size , :created_at , :updated_at
attr_reader :name , :type , :size , :created_at , :height , :width
def initialize(filename)
@name , @type = filename.split(".")
file = File.new(Rails.root.join(Image.asset_root,filename))
fullname = Rails.root.join(Image.asset_root,filename)
file = File.new(fullname)
image = MiniMagick::Image.new(fullname)
@width = image.width
@height = image.height
@created_at = file.birthtime
@updated_at = file.ctime
@size = (file.size/1024).to_i
@@all[@name] = self
end
def aspect_ratio
ratio = @width.to_f / @height
ratios = (1..9).collect{ |i| ((ratio * i) - (ratio * i).round(0)).abs }
min , min_index = ratios.each_with_index.min
[(ratio * (min_index + 1) ).to_i , (min_index + 1) ]
end
#save an io with given name (without ending, that is taken from io)
def self.create_new(filename , io)
original , ending = io.original_filename.split("/").last.split(".")