My FileHandler model can allows all types of files
class FileHandler < ActiveRecord::Base
serialize :properties, ActiveRecord::Coders::Hstore
mount_uploader :file_path, FileUploader
//I'm already setting some of the file attributes here
def update_file_attributes
if file_path.present? && file_path_changed?
self.file_name = file_path.file.filename
self.file_type = file_path.file.content_type
self.file_size = file_path.file.size
end
end
//I want to get height and width here
#Hstore
%w[ImageHeight ImageWidth].each do |key|
attr_accessible key
define_method(key) do
properties && properties[key]
end
define_method("#{key}=") do |value|
self.properties = (properties || {}).merge(key => value)
end
end
And my fileUploader class
class FileUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
version :big, :if => :image? do
process :resize_to_limit => [760, nil]
end
version :thumb_big, :if => :image? do
process :resize_to_limit => [200, 200]
end
version :thumb, :if => :image? do
process :resize_to_limit => [160, 160]
end
version :tiny, :if => :image? do
process :resize_to_limit => [40, 40]
end
protected
def image?(new_file)
new_file.content_type.include? 'image'
end
end
My question is, how do i get the height and width property of the original image and store it in the hstore field?
Any help would be greatly appreciated.
Try this
class FileUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
process :store_geometry, :if => :image?
#......
#......
#......
def store_geometry
if image?(@file)
img = ::Magick::Image::read(@file.file).first
if model
model.ImageWidth = img.columns
model.ImageHeight = img.rows
end
end
end
end
#Hstore
%w[ImageHeight ImageWidth].each do |key|
attr_accessible key
define_method(key) do
properties && properties[key]
end
define_method("#{key}=") do |value|
self.properties = (properties || {}).merge(key => value)
end
end
Assumptions
I'm assuming there's a reason you have the image method that checks if the file is an image, that must mean you're uploading other file formats as well. Well, i've put it to good use here, it calls process_geometry method only if the file is an image.
Hope it helps.
Well you can get the dimension of Image by using Rmagick
as far as I know
All I remember is that you can do is this
img = Magick::Image::read("image_file").first
img.columns => width of image
img.rows => height of image
Perhaps then you can set them in HSTORE
Hope this help
Thanks
As an addition to Tommy's comment. This is what I did to save size of every particular image version not just a size of original image. You have to add "geometry" field to your model so you'll have something like: "phone=146x220&tablet=292x440"
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
version :thumb do
process :resize_to_fill => [80, 80]
end
version :phone do
process :resize_to_limit => [290, 220]
process :store_geometry => :phone
process :quality => 75
end
version :tablet do
process :resize_to_limit => [580, 440]
process :store_geometry => :tablet
process :quality => 35
end
process
def extension_white_list
%w(jpg jpeg gif png)
end
def store_geometry(version)
manipulate! do |img|
model.geometry ||= ""
model.geometry << "&" unless model.geometry.empty?
model.geometry << "#{version}=#{img.columns}x#{img.rows}"
img
end
end
end