在活动存储中创建图像变体时,Rails 7 中的图像处理和迷你魔法宝石出现问题

问题描述 投票:0回答:1

在我的 Rails 应用程序中,我有以下代码。

    class ProjectImage < ApplicationRecord
      belongs_to :project

      has_many_attached :images do |attachable|
        attachable.variant :thumb, resize_to_limit: [100, 100], saver: { quality: 80 }
        attachable.variant :medium, resize_to_limit: [800, 800], saver: { quality: 75 }
      end
    end

在 gemfile 中

    gem 'image_processing', '~> 1.2'
    gem 'mini_magick'

我收到如下错误

/home/dinesh/.rvm/gems/[电子邮件受保护]_app/gems/image_processing-1.2.0/lib/image_processing/chainable.rb:15:在`saver'中:参数数量错误(给定1,预期 0) (参数错误)

在 Rails 控制台中尝试创建附加图像的缩略图和中等变体

  dinesh@DESKTOP-635K3MC:/mnt/c/Workspace/my_app$ rails c
  No executable found at . Will fall back to
  Loading development environment (Rails 7.0.4)
  3.2.0 :001 > require 'image_processing/mini_magick'
  [METRICS]  Completed in 2.4ms | Allocations: 241
  => true
  3.2.0 :002 > image = ProjectImage.last.images.first
    ProjectImage Load (0.7ms)  SELECT `project_images`.* FROM `project_images` ORDER BY `project_images`.`id` DESC LIMIT 1
    ActiveStorage::Attachment Load (60.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 88 AND `active_storage_attachments`.`record_type` = 'ProjectImage' AND `active_storage_attachments`.`name` = 'images' ORDER BY `active_storage_attachments`.`id` ASC LIMIT 1
  [METRICS]  Completed in 126.8ms | Allocations: 14520 | ActiveRecord: 63.8ms (queries: 6)
  => #<ActiveStorage::Attachment:0x00007ff6cfcfc678 id: 144, name: "images", record_type: "ProjectImage", record_id: 88, blob_id: 144, created_at: Wed, 31 Jul 2024 05:17:10.672157000 EDT -04:00>

  3.2.0 :011 > processed = ImageProcessing::MiniMagick
  3.2.0 :012 > .source(image)
  3.2.0 :013 > .resize_to_limit(800, 800)
  3.2.0 :014 > .quality(75)
  3.2.0 :015 > .call(destination: '/tmp/processed_image.jpg')
  /home/dinesh/.rvm/gems/[email protected]_app/gems/image_processing-1.2.0/lib/image_processing/chainable.rb:54:in `branch': wrong number of arguments (given 1, expected 0) (ArgumentError)
  3.2.0 :016 >

  3.2.0 :039 >
  3.2.0 :040 > file = ActiveStorage::Blob.service.send(:path_for, image.key)
  [METRICS]  Completed in 0.2ms | Allocations: 59
  => "/mnt/c/Workspace/my_app/storage/dh/en/dhenjruof4jlv35411oxebu9j0tt"
  3.2.0 :041 >
  3.2.0 :042 > processed = ImageProcessing::MiniMagick
  3.2.0 :043 >   .source(file)
  3.2.0 :044 >   .resize_to_limit(800, 800)
  3.2.0 :045 >   .quality(75)
  3.2.0 :046 >   .call(destination: '/tmp/processed_image.jpg')
  /home/dinesh/.rvm/gems/[email protected]_app/gems/image_processing-1.2.0/lib/image_processing/chainable.rb:54:in `branch': wrong number of arguments (given 1, expected 0) (ArgumentError)
  3.2.0 :047 >

  3.2.0 :047 >
  3.2.0 :048 > include Rails.application.routes.url_helpers
  [METRICS]  Completed in 0.5ms | Allocations: 175
  => Object
  3.2.0 :049 >
  3.2.0 :050 >
  3.2.0 :051 > Rails.application.routes.default_url_options[:host] = 'localhost:3000'
  [METRICS]  Completed in 0.1ms | Allocations: 52
  => "localhost:3000"
  3.2.0 :052 >
  3.2.0 :053 > project_image = ProjectImage.last
    ProjectImage Load (0.4ms)  SELECT `project_images`.* FROM `project_images` ORDER BY `project_images`.`id` DESC LIMIT 1
  [METRICS]  Completed in 1.4ms | Allocations: 217 | ActiveRecord: 0.4ms (queries: 1)
  =>
  #<ProjectImage:0x00007ff6cfbeb7c0
  ...
  3.2.0 :054 > or(image)
  3.2.0 :054 > project_image.images.each do |image|
  3.2.0 :055 >   original_url = url_for(image)
  3.2.0 :056 >   thumbnail_url = url_for(image.variant(:thumb).processed)
  3.2.0 :057 >   medium_url = url_for(image.variant(:medium).processed)
  3.2.0 :058 >
  3.2.0 :059 >   puts "Original: #{original_url}"
  3.2.0 :060 >   puts "Thumbnail: #{thumbnail_url}"
  3.2.0 :061 >   puts "Medium: #{medium_url}"
    ActiveStorage::Attachment Load (0.4ms)  SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 88 AND `active_storage_attachments`.`record_type` = 'ProjectImage' AND `active_storage_attachments`.`name` = 'images'
    ActiveStorage::Blob Load (0.3ms)  SELECT `active_storage_blobs`.* FROM `active_storage_blobs` WHERE `active_storage_blobs`.`id` = 144 LIMIT 1
    ActiveStorage::VariantRecord Load (0.5ms)  SELECT `active_storage_variant_records`.* FROM `active_storage_variant_records` WHERE `active_storage_variant_records`.`blob_id` = 144 AND `active_storage_variant_records`.`variation_digest` = 'ENcXmpHy8uaefOEARzwsAvf/nEc=' LIMIT 1
    Disk Storage (11.1ms) Downloaded file from key: dhenjruof4jlv35411oxebu9j0tt
  /home/dinesh/.rvm/gems/[email protected]_app/gems/image_processing-1.2.0/lib/image_processing/chainable.rb:15:in `saver': wrong number of arguments (given 1, expected 0) (ArgumentError)
  3.2.0 :063 >
  3.2.0 :064 >
image-processing rails-activestorage ruby-on-rails-7 minimagick imageprocessor
1个回答
0
投票

图像处理gem的最新版本是1.13.0

1.2.0版本自6年前以来就没有更新过。

在 Gemfile 中更改此设置

gem "image_processing", "~> 1.0"

然后再次测试,如果可能的话,不要使用 Minimagick gem,因为问题似乎与此无关。

© www.soinside.com 2019 - 2024. All rights reserved.