我有很大麻烦...
我已经通过我的Rails应用程序将大约500张图像上传到S3,有几种不同的尺寸。现在我意识到我需要将这些图像尺寸存储到我的数据库中。我这样做的方法是进行迁移:
class AddImageDimensionsToImages < ActiveRecord::Migration
def change
# add image columns
add_column :images, :small_width, :integer
add_column :images, :small_height, :integer
add_column :images, :medium_width, :integer
add_column :images, :medium_height, :integer
add_column :images, :large_width, :integer
add_column :images, :large_height, :integer
add_column :images, :original_width, :integer
add_column :images, :original_height, :integer
# loop all images
Image.all.each do |image|
# get sizes
geo_small = Paperclip::Geometry.from_file(image.image.to_file(:small))
geo_medium = Paperclip::Geometry.from_file(image.image.to_file(:medium))
geo_large = Paperclip::Geometry.from_file(image.image.to_file(:large))
geo_original = Paperclip::Geometry.from_file(image.image.to_file(:original))
# set sizes
image.small_width = geo_small.width if geo_small
image.small_height = geo_small.height if geo_small
image.medium_width = geo_medium.width if geo_medium
image.medium_height = geo_medium.height if geo_medium
image.large_width = geo_large.width if geo_large
image.large_height = geo_large.height if geo_large
image.original_width = geo_original.width if geo_original
image.original_height = geo_original.height if geo_original
# save image
image.save
end
end
end
当我运行此迁移时,它会像这样:
...
-- add_column(:images, :small_width, :integer)
-> 2.0866s
-- add_column(:images, :small_height, :integer)
-> 0.0092s
-- add_column(:images, :medium_width, :integer)
-> 0.0073s
...
Command :: identify -format %wx%h '/app/tmp/HayUnFinal_1620120408-4-qpzyym.jpg[0]'
[AWS S3 200 0.106467] get_object(:bucket_name=>"aa",:key=>"original/34/HayUnFinal_16.jpg")
Command :: identify -format %wx%h '/app/tmp/HayUnFinal_1620120408-4-1vqo71y.jpg[0]'
[paperclip] Saving attachments.
[AWS S3 200 0.152898] get_object(:bucket_name=>"aa",:key=>"small/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-i1ohlr.jpg[0]'
[AWS S3 200 0.055378] get_object(:bucket_name=>"aa",:key=>"medium/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-viizzf.jpg[0]'
[AWS S3 200 0.079235] get_object(:bucket_name=>"aa",:key=>"large/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-1d2u00o.jpg[0]'
[AWS S3 200 0.066724] get_object(:bucket_name=>"aa",:key=>"original/64/Portrait_2.jpg")
Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-jq9wmg.jpg[0]'
[paperclip] Saving attachments.
[AWS S3 200 0.104891] get_object(:bucket_name=>"aa",:key=>"small/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-an2mpt.jpg[0]'
[AWS S3 200 0.064148] get_object(:bucket_name=>"aa",:key=>"medium/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-tzmnup.jpg[0]'
[AWS S3 200 0.051090] get_object(:bucket_name=>"aa",:key=>"large/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-1pa0rxh.jpg[0]'
[AWS S3 200 0.063531] get_object(:bucket_name=>"aa",:key=>"original/58/Bansah_24.jpg")
Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-kogk2g.jpg[0]'
[paperclip] Saving attachments.
rake aborted!
An error has occurred, this and all later migrations canceled:
can't convert nil into String
耙子流产了!它似乎适用于几个图像,但随后迁移失败。我无法弄清楚那些来自哪里,以及如何防止它。任何人都可以帮我这个吗?
红利问题:这是否是一种保留已保存已上传图片图像尺寸的重要方法?
提前多多感谢!
答案 0 :(得分:0)
数据库中的某些图像行具有空图像值。像这样的支票就成了诀窍:
Image.all.each do |image|
# check that we have an image
if image.image_file_size
...
end
end