载波获取图像的宽度和高度,并将其存储在hstore字段中 [英] Carrierwave getting image width and height and storing it in an hstore field
问题描述
class FileHandler< ActiveRecord :: Base
序列化:属性,ActiveRecord ::ª:: Hstore
mount_uploader:file_path,FileUploader
//我已经在这里设置了一些文件属性
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
//我想在这里得到高度和宽度
#Hstore
%w [ImageHeight ImageWidth] .each do | key |
attr_accessible key
define_method(key)do
properties&&属性[键]
结束
define_method(#{key} =)do | value |
self.properties =(properties || {})。merge(key => value)
end
end
和我的fileUploader类
class FileUploader< CarrierWave :: Uploader :: Base
#包括RMagick或MiniMagick支持:
包括CarrierWave :: RMagick
版本:big,:if => :图片? do
process:resize_to_limit => [760,nil]
结束
版本:thumb_big,:if => :图片? do
process:resize_to_limit => [200,200]
结束
版本:thumb,:if => :图片? do
process:resize_to_limit => [160,160]
结束
版本:tiny,:if => :图片? do
process:resize_to_limit => [40,40]
结束
保护
def image?(new_file)
new_file.content_type.include? 'image'
end
end
我的问题是,如何获得原始图像的高度和宽度属性并将其存储在hstore字段中?
任何帮助将不胜感激。
试试这个
class FileUploader< CarrierWave :: Uploader :: Base
#包括RMagick或MiniMagick支持:
包括CarrierWave :: RMagick
process:store_geometry,:if => :图片?
...... ......
#......
#......
def store_geometry
如果图像?(@文件)
img = :: Magick :: Image :: read(@ file.file).first
如果模型
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&&属性[键]
结束
define_method(#{key} =)do | value |
self.properties =(properties || {})。merge(key => value)
end
end
假设
我假设你有一个图像方法来检查文件是否为图像,这意味着你正在上传其他文件格式也是如此。那么,我已经把它用在这里,它只有在文件是图像时才调用process_geometry方法。
希望有帮助。
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.
这篇关于载波获取图像的宽度和高度,并将其存储在hstore字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!