当需要时生成 imagemagick/carrierwave 缩略图

发布于 2024-12-13 20:49:00 字数 846 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡の花香 2024-12-20 20:49:02

您可以使用.recreate_versions!

例如:

Product.all.each do |product|
  product.photos.each do |photo|
    photo.recreate_versions!
  end
end

You can use .recreate_versions!

For example:

Product.all.each do |product|
  product.photos.each do |photo|
    photo.recreate_versions!
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文