从 rmagick 图像创建回形针附件

发布于 2024-09-29 13:52:07 字数 386 浏览 6 评论 0原文

我无法找到一种方法将使用 RMagick 创建的图像保存在回形针附件中。

imageList = Magick::ImageList.new
imageList.new("images/apple.gif", "images/overlay.png")
...
picture = imageList.flatten_images

我所在的模型有一个附加文件

has_attached_file :图片, :网址=> ..., :路径=> ...

我只想将 imageList.flatten_images 返回的图像保存为模型的图片。

请问有人知道如何轻松做到吗?

谢谢

I have a problem to find a way to save an image created with RMagick in a paperclip attachment.

imageList = Magick::ImageList.new
imageList.new("images/apple.gif", "images/overlay.png")
...
picture = imageList.flatten_images

I am in a model that have an attached file

has_attached_file :picture,
:url => ...,
:path => ...

and i just want my image returned by imageList.flatten_images to be saved as the picture of my model.

Does anyone know how to do it easily please?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

豆芽 2024-10-06 13:52:07

让我们看看这是否是您所需要的

picture = imageList.flatten_images
file = Tempfile.new('my_picture.jpg')
picture.write(file.path)
YourModel.create(:picture => file, ...)

YourModel 更改为您正在使用的模型...

Let's see if that's what you need

picture = imageList.flatten_images
file = Tempfile.new('my_picture.jpg')
picture.write(file.path)
YourModel.create(:picture => file, ...)

Change YourModel with the model you are using...

七秒鱼° 2024-10-06 13:52:07

您应该在 TempFile.new 上强制扩展;在这种情况下,我从 S3 或类似的东西中提取原始图像,这当然发生在模型中:

orig_img = Magick::ImageList.new(self.photo.url(:original))

#process image here

# Force extension with array form:
file = Tempfile.new(['processed','.jpg'])
orig_img.write(file.path)
self.photo = file
self.save

You should force the extension on TempFile.new; in this case I pull the original image from S3 or some such, this is happening in the model of course:

orig_img = Magick::ImageList.new(self.photo.url(:original))

#process image here

# Force extension with array form:
file = Tempfile.new(['processed','.jpg'])
orig_img.write(file.path)
self.photo = file
self.save
隔岸观火 2024-10-06 13:52:07

在 Paperclip 的更高版本中(我的是 5.0.0),您需要提供 Paperclip 自己的 Tempfile 实例:

file = Paperclip::Tempfile.new(["processed", ".jpg"])
thumb.write(file.path)
result = YourModel.create(image: file)

这会保留文件名末尾的文件扩展名,以便回形针在上传时能够识别它。

In the later versions of Paperclip (mine is 5.0.0), you'll need to provide Paperclip's own Tempfile instance:

file = Paperclip::Tempfile.new(["processed", ".jpg"])
thumb.write(file.path)
result = YourModel.create(image: file)

This preserves the file extension at the end of the filename, so that it is recognized by Paperclip when it's uploaded.

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