【问题标题】:Creating a MiniMagick Montage on the fly from images uploaded with Shrine to S3从通过 Shrine 上传到 S3 的图像动态创建 MiniMagick 蒙太奇
【发布时间】:2020-03-01 02:46:22
【问题描述】:

我正在尝试创建一个由各种缩略图组成的蒙太奇文件,这些缩略图通过 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 警告:Errno::ENOENT:否 这样的文件或目录@ 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 打开拇指(我已经混淆了上面的链接,但是当粘贴到浏览器中时它会正确返回图像)

我尝试直接在块中传递链接:

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

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

montage-im6.q16:未授权HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file':没有这样的文件或目录@error/constitute.c/ReadImage/544。 montage-im6.q16:未授权HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file':不允许操作@error/constitute.c/ReadImage/544。 montage-im6.q16:未授权HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file':不允许操作@error/constitute.c/ReadImage/544。 montage-im6.q16:未授权HTTPS' @ error/delegate.c/InvokeDelegate/1717. montage-im6.q16: unable to open file':不允许操作@错误/构成.c/ReadImage/544。 montage-im6.q16:未授权“HTTPS”@ 错误/delegate.c/InvokeDelegate/1717。

不太清楚如何处理..

【问题讨论】:

    标签: ruby-on-rails imagemagick minimagick shrine


    【解决方案1】:

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

    它可能看起来像这样:

    # 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!)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-19
      • 2015-09-08
      • 2014-12-05
      相关资源
      最近更新 更多