47 lines
1.1 KiB
Ruby
47 lines
1.1 KiB
Ruby
module IdCard
|
|
class NetworkLogo < ApplicationRecord
|
|
validates :filename, :image_data, :content_type, :aspect_ratio, presence: true
|
|
validates :filename, uniqueness: true
|
|
|
|
before_validation :process_image, if: :image_data_changed?
|
|
|
|
scope :defaults, -> { where(default: true) }
|
|
|
|
class << self
|
|
|
|
def medcost
|
|
defaults.where("filename LIKE ?", "%MedCost%")
|
|
end
|
|
|
|
def cigna
|
|
defaults.where("filename LIKE ?", "%Cigna%")
|
|
end
|
|
|
|
end
|
|
|
|
private
|
|
|
|
def process_image
|
|
image = Vips::Image.new_from_buffer(self.image_data, "")
|
|
|
|
resized_image = ImageProcessing::Vips
|
|
.source(image)
|
|
.resize_to_limit(nil, 400)
|
|
|
|
processed_image = resized_image.convert("png").call
|
|
new_image_data = processed_image.read
|
|
if new_image_data
|
|
self.image_data = new_image_data
|
|
self.filename = File.basename(self.filename, File.extname(self.filename)) + ".png"
|
|
self.content_type = "image/png"
|
|
end
|
|
|
|
image_ratio = image.width.to_f / image.height
|
|
if image_ratio
|
|
self.aspect_ratio = image_ratio.round(2)
|
|
end
|
|
end
|
|
|
|
end
|
|
end
|