Rails carrierwave 和 mini_magic 图片上传和处理问题

runup · 2016年09月05日 · 最后由 maxchen 回复于 2018年02月13日 · 3277 次阅读

问题描述:对于尺寸小 (500*500) 的图片能够正常显示,但是对于尺寸大 (1000*1000) 的图片上传成功,但是显示不出来。controller 设置如下。

class PictureUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

注:使用 process :resize_to_fit=>[1200,1200],也无法完成显示大图片的功能。 解决:查看 chrome 的 network,发现是图片加载失败,错误如下:

net::ERR_BLOCKED_BY_CLIENT

显示被客户端拦截,我的 chrome 装了 adblock 插件,我把http://localhost:3000加入到白名单中,就可以显示图片了。

在这个文件里边给它上传的大小限制一下,比如 process resize_to_fit: [500, 200]

#1 楼 @wuxuan 我想上传大尺寸的图片,比如 1000*300,如何进行设置?

#1 楼 @wuxuan 在这个 controller 文件中设置如下方法

process :resize_to_fit => [800, 800]

但是报错,不存在 resize_to_fit 方法。

include CarrierWave::MiniMagick 放开注释

默认就没处理,不能正常显示是什么情况?

7 楼 已删除
8 楼 已删除

#5 楼 @Rei #6 楼 @wuxuan #4 楼 @guojhq 问题已经更新,能否帮忙解释下,感谢。

#9 楼 @runup 无法显示是什么情况?url 没有响应?上传目录没有文件?文件类型不对?这些用手头的浏览器、命令行不能自己确认吗?

#10 楼 @Rei 图片上传到相应的文件夹,但是图片显示不出来。

View 里面的生成图片链接的代码和你制定的大图片的 version 名字贴出来看看

#12 楼 @renyuanz

#图片显示代码,500*500可以显示,1000*500不能显示
<%= image_tag @resume.attachment_url %>

其他代码没有变动,包括 version

浏览器直接打开图片链接能不能看,浏览器调试 Network 的状态,css 有没有问题。

@runup 我感觉是因为你没制定大图片 version 导致的问题

如果你有定义图片版本的代码,类似

version :thumb do
  process :resize_to_fit => [50, 50]
end

那么你在 view 里面想指定使用这个 thumb 版本的图片,可以这么写

<%= image_tag @resume.attachment_url(:thumb) %>

你可以试试

因为你也没说具体是什么报错,所以我猜可能是这个原因😅

#14 楼 @Rei 谢谢帮忙找到问题的解决方式。按照您的建议,查看 chrome 的 network,发现是图片加载失败,错误如下:

net::ERR_BLOCKED_BY_CLIENT

显示被客户端拦截,我的 chrome 装了 adblock 插件,我把http://localhost:3000加入到白名单中,就可以显示图片了。

#16 楼 @renyuanz 已经解决,参考楼上的解释,感谢帮忙哈~

两个问题: 1.User 模型中的 image 字段关联了图片上传器,在 uploader 中设置了

small ,:middle

两个 version,请问是用

@user.image.small.url@user.image.middle.url

来分别获取吗? 2.帮助文档中有这样一段

The model variable points to the instance object the uploader is attached to.

请问其中的 model 指的是 User 还是字段 image 呢?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号