在 Paperclip 中保存图像尺寸(宽度和高度)? [英] Saving the images Dimensions (width and height) in Paperclip?

查看:28
本文介绍了在 Paperclip 中保存图像尺寸(宽度和高度)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何Paperclip 向导都知道您在使用Paperclip 保存图像时是否可以同时在2 个额外字段中保存图像尺寸(宽度和高度)?在 Paperclip 上传过程中如何获取此类数据?

Any Paperclip wizards out there know if you can when using Paperclip to save an image, also save the image dimensions (width and height) in 2 extra fields? How do you get such data during the Paperclip upload process?

推荐答案

只是为了完整性,即使以前的答案已经提供了足够好的建议.

Just for the sake of completeness, even though previous answers already show good enough suggestions.

您可以使用 Paperclip 事件处理程序代替 Rails 回调.在这种情况下,只有在图像更改时才会重新计算大小.(如果您使用 S3 进行存储,这可以节省相当多的时间)

You can utilize Paperclip event handlers instead of Rails callbacks. In this case, size will be recalculated only when image changes. (If you're using S3 for storage, this can save quite some time)

has_attached_file :image, :styles => ...
after_post_process :save_image_dimensions

def save_image_dimensions
  geo = Paperclip::Geometry.from_file(image.queued_for_write[:original])
  self.image_width = geo.width
  self.image_height = geo.height
end

图像甚至不必从 S3 下载(或从文件中读取),回形针将其提供给事件处理程序本身.

Image don't even have to be downloaded from S3 (or read from a file), paperclip provides it to event handler itself.

有关详细信息,请参阅自述文件事件部分.

See Events section of the readme for details.

这篇关于在 Paperclip 中保存图像尺寸(宽度和高度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆