当需要时生成 imagemagick/carrierwave 缩略图
我在 Rails 应用程序中将 Carrierwave gem 与 Rmagick 一起使用。我在我的上传文件中设置了一个新版本:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :preview do
process :resize_to_fill => [580, 350]
end
end
当然,我在我的 gemfile 中包含了 rmagick 和 Carrierwave。现在,我尝试在视图中加载图像的预览版本:
@product.photos.first.image.preview
这不会给出任何错误,但会加载损坏的图像。如果我复制图像的 url,则会收到路由错误(“没有路由匹配 /path/to_my_imagesfolder/preview_image.png”)。如果我删除预览方法,图像将正确加载。
问题可能是什么?我以为可能是权限问题,但是我把上传文件夹设置为777,还是失败。
有什么想法吗?
编辑:我意识到,如果我再次上传图像,就会创建新版本。是否可以让 Rmagick 在收到请求时创建它们(就像 TimThumb 在 PHP 中所做的那样)或者至少有任何命令可以批量创建所有版本?
一定有比上传所有图像更好的方法......
I am using the carrierwave gem with Rmagick in a Rails app. I've set up a new version in my uploader file:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :preview do
process :resize_to_fill => [580, 350]
end
end
Of course, I included rmagick and carrierwave in my gemfile. Now I try to load the preview version of my images in my views:
@product.photos.first.image.preview
This does not give any errors, but loads a broken image. If I copy the url of the image, I get a routing error ('no route matches /path/to_my_imagesfolder/preview_image.png'). If I remove the preview method, the image loads properly.
What can the problem be? I thought maybe it was a permissions issue, but I set the uploads folder with 777 and it still fails.
Any ideas?
EDIT: I realized if I upload the images again the new versions are created. Is it possible to make Rmagick create them when they are requested (like TimThumb does in PHP) Or at least is there any command to batch create all the versions?
There must be a better way than uploading all the images...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用.recreate_versions!
例如:
You can use .recreate_versions!
For example:
我只是使用这个: https://github.com/markevans/dragonfly
I'd just use this: https://github.com/markevans/dragonfly