从使用 Shrine 上传到 S3 的图像动态创建 MiniMagick 蒙太奇

Creating a MiniMagick Montage on the fly from images uploaded with Shrine to S3

我正在尝试创建一个蒙太奇文件,其中包含使用 Shrine 上传(私下)到 S3 的各种缩略图。

此操作的代码位于 Sidekiq 工作人员(在 Heroku)中,该工作人员应发送一封电子邮件,其中包含上述处理后的图像作为电子邮件附件。 (然后只是转储图像)

这是我的尝试:

images = []

@user.photos.first(25).each do |photo|
  images << File.read(photo.image[:thumb].url)
end

processed_image = MiniMagick::Tool::Montage.new do |image|
        images.each {|i| image << i} 
        image.tile "5x5"
        image << "output.jpg"        
end

attachments.inline['images.jpg'] = processed_image

虽然我收到错误:

2019-11-04T18:17:59.638Z 30695 TID-ot0uksdbv WARN: Errno::ENOENT: No such file or directory @ rb_sysopen - https://mysite.s3.eu-west-1.amazonaws.com/photo/thumb/5cb924406fa8944e5279a15b46f250f6.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIJJCEGJCEVP2A%2F20191104%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20191104T181759Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host&X-Amz-Signature=38706b7526fd0a8095a2f387521063d4d8901c4523696ff7e1f60ae2d

似乎无法在 S3 上打开拇指(我已经混淆了上面的 link,但是当粘贴到浏览器中时它 returns 图像正确)

我试过直接在块中传递 link :

images << photo.image[:thumb].url

但是这次我似乎从 MiniMagick 得到了错误

montage-im6.q16: not authorized HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file': No such file or directory @ error/constitute.c/ReadImage/544. montage-im6.q16: not authorized HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file': Operation not permitted @ error/constitute.c/ReadImage/544. montage-im6.q16: not authorized HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file': Operation not permitted @ error/constitute.c/ReadImage/544. montage-im6.q16: not authorized HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file': Operation not permitted @ error/constitute.c/ReadImage/544. montage-im6.q16: not authorized `HTTPS' @ error/delegate.c/InvokeDelegate/1717.

不太确定如何处理这个..

您需要在处理前将文件下载到磁盘,并在您的蒙太奇命令中使用本地路径。此外,您需要从输出路径读取结果文件,因为蒙太奇命令 returns 仅标准输出。

放在一起看起来像这样:

# download input images to disk
input_images = @user.photos.first(25)
  .map { |photo| photo.image[:thumb] }
  .map(&:download)

# create temporary file for output image
processed_image = Tempfile.new ["montage", ".jpg"], binmode: true

MiniMagick::Tool::Montage.new do |montage|
  montage.merge! input_images.map(&:path)
  montage.tile "5x5"
  montage << processed_image.path
end

attachments.inline['images.jpg'] = File.binread(processed_image.path)

# close and delete temporary files
[*input_images, processed_image].each(&:close!)