我有大麻烦...
我已经通过我的 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
耙中止!它似乎适用于几个图像,但随后迁移失败。我真的不知道那个 nil 来自哪里,以及如何防止它。谁能帮我这个?
额外问题:这是处理保存已上传图像的图像尺寸任务的一种方法吗?
提前非常感谢!