我可以使用 CarrierWave recreate_versions 存在原始文件吗?
Can I recreate_versions original file exists using CarrierWave?
我在 rails 中使用载波进行图像上传。
由于文件大小,我想调整原始图像文件的大小。
所以我删除版本 :thumb 块位置过程代码不在版本块中,如下所示。
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
..
#version :thumb do
# process :resize_to_limit => [1024, nil]
#end
process :resize_to_limit => [1024, nil] # added not in version block
..
end
问题是我不知道如何重新创建存在的原始图像文件并使用 CarrierWave 上传。
.recreate_versions!我认为该方法仅适用于版本块..
#mounted uploader above
model.list_image.recreate_versions!
# => [:store_versions!]
# original file not change
如何重新创建已经存在的原始文件?
(我不想添加版本块,因为如果添加版本块,那么无论何时上传图像文件,图像文件都是多个文件。(原始文件和特定版本文件))
您可以通过两种方式解决您的问题:
您可以在循环中为所有对象调用保存事件,并在存在图像的模型中使用 imagemagick (RMagick) 调整实际图像的大小。
class Model < ActiveRecord::Base
before_save do
self.image = self.image.resize "1024x"
end
end
您将为所有图像相关数据创建新记录并销毁之前的记录。而不是使用 recreate_versions
方法。
Model.all.each { |old|
new = Modle.new(foo_id: old.foo_id, image: old.image)
new.save!
old.destroy
}
我在 rails 中使用载波进行图像上传。
由于文件大小,我想调整原始图像文件的大小。
所以我删除版本 :thumb 块位置过程代码不在版本块中,如下所示。
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
..
#version :thumb do
# process :resize_to_limit => [1024, nil]
#end
process :resize_to_limit => [1024, nil] # added not in version block
..
end
问题是我不知道如何重新创建存在的原始图像文件并使用 CarrierWave 上传。
.recreate_versions!我认为该方法仅适用于版本块..
#mounted uploader above
model.list_image.recreate_versions!
# => [:store_versions!]
# original file not change
如何重新创建已经存在的原始文件?
(我不想添加版本块,因为如果添加版本块,那么无论何时上传图像文件,图像文件都是多个文件。(原始文件和特定版本文件))
您可以通过两种方式解决您的问题:
您可以在循环中为所有对象调用保存事件,并在存在图像的模型中使用 imagemagick (RMagick) 调整实际图像的大小。
class Model < ActiveRecord::Base before_save do self.image = self.image.resize "1024x" end end
您将为所有图像相关数据创建新记录并销毁之前的记录。而不是使用
recreate_versions
方法。Model.all.each { |old| new = Modle.new(foo_id: old.foo_id, image: old.image) new.save! old.destroy }