新手问题 求助,minimagick 无法打开本地图片

huanghuang · 2019年05月14日 · 最后由 huanghuang 回复于 2019年05月14日 · 1225 次阅读

环境:Rails 5.2 development

我想要把上传的图片裁剪一下(图片上传存储使用 active storage), 可是在使用 minimagick 读取图片的时候报错。

发帖没法上传图片(500 错误),只能先粘贴报错信息了。

Net::ReadTimeout # 错误信息

def crop_param(url)
      image = MiniMagick::Image.open(url)  # 这一行报错
      width = image.width
      height = image.height
      "50%x45%+#{width*0.25}+#{height*0.17}"

但是直接在浏览器可以访问图片

调用的地方在这里

<td><%= image_tag upload_qr_code.qr_code.variant(crop: UploadQrCode.crop_param(polymorphic_url(upload_qr_code.qr_code)), resize: "150x200") %></td>

url 是什么?

把报错信息发上来吧😹

tinyfeng 回复

url 是这样的

http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBIQT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1d485442831c9ac8fbbb5cae15942c2a8217b8fb/qr_code_test_1.png
4 楼 已删除
ghn645568344 回复

报错信息是这样的

Net::ReadTimeout in UploadQrCodes#index
Showing /Users/fc/app_server/app/views/upload_qr_codes/index.html.erb where line #22 raised:

Net::ReadTimeout
Extracted source (around line #41):
39
40   def crop_param(url)
41      image = MiniMagick::Image.open(url)
42      width = image.width
43      height = image.height
44      "50%x45%+#{width*0.25}+#{height*0.17}"

Rails.root: /Users/fc/app_server

Application Trace | Framework Trace | Full Trace
app/models/upload_qr_code.rb:41:in `crop_param'
app/views/upload_qr_codes/index.html.erb:22:in `block in _app_views_upload_qr_codes_index_html_erb__2215282283866048584_70323507321140'
app/views/upload_qr_codes/index.html.erb:16:in `_app_views_upload_qr_codes_index_html_erb__2215282283866048584_70323507321140'

Full Trace

/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/protocol.rb:181:in `rbuf_fill'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/protocol.rb:157:in `readuntil'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/protocol.rb:167:in `readline'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/http/response.rb:40:in `read_status_line'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/http/response.rb:29:in `read_new'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:1494:in `block in transport_request'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:1491:in `catch'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:1491:in `transport_request'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:1464:in `request'
newrelic_rpm (5.4.0.347) lib/new_relic/agent/instrumentation/net.rb:36:in `block in request_with_newrelic_trace'
newrelic_rpm (5.4.0.347) lib/new_relic/agent.rb:501:in `disable_all_tracing'
newrelic_rpm (5.4.0.347) lib/new_relic/agent/instrumentation/net.rb:35:in `request_with_newrelic_trace'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:343:in `block in open_http'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:910:in `start'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:337:in `open_http'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:755:in `buffer_open'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:226:in `block in open_loop'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:224:in `catch'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:224:in `open_loop'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:165:in `open_uri'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:735:in `open'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/open-uri.rb:35:in `open'
mini_magick (4.9.3) lib/mini_magick/image.rb:94:in `open'
app/models/upload_qr_code.rb:41:in `crop_param'
app/views/upload_qr_codes/index.html.erb:22:in `block in _app_views_upload_qr_codes_index_html_erb__2215282283866048584_70323507321140'
activerecord (5.2.1) lib/active_record/relation/delegation.rb:41:in `each'
activerecord (5.2.1) lib/active_record/relation/delegation.rb:41:in `each'
app/views/upload_qr_codes/index.html.erb:16:in `_app_views_upload_qr_codes_index_html_erb__2215282283866048584_70323507321140'
actionview (5.2.1) lib/action_view/template.rb:159:in `block in render'
activesupport (5.2.1) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.1) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.1) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.2.1) lib/action_view/template.rb:354:in `instrument_render_template'
actionview (5.2.1) lib/action_view/template.rb:157:in `render'
actionview (5.2.1) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (5.2.1) lib/action_view/renderer/abstract_renderer.rb:44:in `block in instrument'
activesupport (5.2.1) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.1) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.1) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.2.1) lib/action_view/renderer/abstract_renderer.rb:43:in `instrument'
actionview (5.2.1) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (5.2.1) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (5.2.1) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (5.2.1) lib/action_view/renderer/template_renderer.rb:16:in `render'
actionview (5.2.1) lib/action_view/renderer/renderer.rb:44:in `render_template'
actionview (5.2.1) lib/action_view/renderer/renderer.rb:25:in `render'
actionview (5.2.1) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.2.1) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionview (5.2.1) lib/action_view/rendering.rb:84:in `render_to_body'
actionpack (5.2.1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.2.1) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (5.2.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (5.2.1) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.2.1) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
activesupport (5.2.1) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/Users/fc/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'

看了下源码 MiniMagick::Image.open 用的是 open-uri 实现,可以在终端自己试试用 open-uri 打开链接看看。

另外代码效率比较低啊,每次渲染都要下载图片解析,activestorage 本身保存了图片 metadata 的。

Rei 回复

感谢,我去试试看。 😂 继续改进。

Rei 回复

在终端下是可以打开的,我改成了使用图片的 metadata。 感谢🙏。

huanghuang 关闭了讨论。 05月15日 10:21
需要 登录 后方可回复, 如果你还没有账号请 注册新账号