【问题标题】:retrieving dimensions of a Paperclip image检索回形针图像的尺寸
【发布时间】:2016-09-08 15:38:35
【问题描述】:

我正在尝试检索回形针图像的尺寸。我尝试调用方法 widthheight 应用于我的每个 Imagemodel 实例,但这些返回错误。

然后我试图从这篇文章中获得灵感:https://github.com/thoughtbot/paperclip/wiki/Extracting-image-dimensions

并在控制台中播放了以下内容:

image = Imagemodel.first
geometry = Paperclip::Geometry.from_file(image)

虽然我在控制台中收到以下错误

Paperclip::Errors::NotIdentifiedByImageMagickError: 

所以我有点困惑...我很高兴将高度和宽度添加到表中的特定字段(而是在控制器中的 Create 操作中而不是在模型级别)或直接访问我的视图中的尺寸或控制器...

【问题讨论】:

  • 您确定确实在传递图像。 Iv 刚刚通过控制台,如果传递的文件实际上是空白的,我可以引发与您相同的错误。
  • 你是对的。我检索了包含实际图像顶部不同字段的完整模型实例。我在控制台中做了image = image.image,几何位按预期返回了图像大小。现在我可以在我的视图中使用,也可以在需要时添加一些列..
  • 很高兴它有帮助。 iv 在下面的评论中添加了我所说的作为答案。

标签: ruby-on-rails imagemagick paperclip


【解决方案1】:

看起来你没有传递图像,但实际上是记录本身。

试试这个:

image = ImageModel.first.attachment # this assumes the actual image is named attachment
geometry = Paperclip::Geometry.from_file(image) # will return e.g. => 300x357

【讨论】:

    【解决方案2】:

    在 Rails 4 中对我有用的是:

    image = my_image_model.my_attachment.queued_for_write[:original]
    Paperclip::Geometry.from_file(image)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多