image copying

This commit is contained in:
2022-12-30 21:10:33 +02:00
parent ee157a18d1
commit 3e9667470d
4 changed files with 25 additions and 10 deletions

View File

@ -1,4 +1,5 @@
require "mini_magick"
require "mini_magick"
require "fileutils"
module Merged
class Image < ActiveBase
@ -15,13 +16,21 @@ module Merged
ratio = self.ratio
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) ]
[(ratio * (min_index + 1) ).round(0).to_i , (min_index + 1) ]
end
def ratio
self.width.to_f / self.height
end
def copy()
image = Image.new name: "#{name}_copy" , type: type , tags: (tags || "")
Image.insert(image) # assigns next id
FileUtils.cp full_filename , image.full_filename
image.init_file_data
image
end
#save an io as new image. The filename is the id, type taken from io
def self.create_new(name , tags, io)
original , ending = io.original_filename.split("/").last.split(".")