【问题标题】:How to resize already uploaded Paperclip images to s3 using reprocess? (Rails)如何使用重新处理将已上传的回形针图像调整为 s3? (导轨)
【发布时间】:2014-10-27 12:12:17
【问题描述】:

我有一个模型:

class PropertyImage < ActiveRecord::Base
  has_attached_file :picture,
    storage: :s3,
    s3_credentials: CONFIG['s3'],
    s3_protocol: (Rails.env.development? ? "http": "https"),
    styles: {
      thumb: '100x100>',
      large: '633x460>', 
      medium: '301x240>'      
    }
end

我正在使用 Rails (4.0.1)、可卡因 (0.5.4) 和回形针 (3.5.4)。

我想迁移旧图像(没有拇指,大和中)并调整每个图像的大小,所以我创建了一个 rake 脚本:

namespace :migrate_images do
  desc "Resize Images in PropertyImage"

  task start_migration: :environment do

    PropertyImage.not_migrated.find_each do |pi|      
      ImageConverter.perform(pi)
    end      
  end

end

和ImageConverter类:

class ImageConverter

  def self.perform(pi)
    begin
      pi.picture.reprocess!
      pi.update_attributes!({migrated: true})
      puts "PropertyImage [#{pi.id}] has been migrated."
    rescue Exception => e
      puts "PropertyImage [#{pi.id}] has an error. #{e}"
    end
  end
end

现在当我运行脚本时,我不断收到以下错误:

⇒  bundle exec rake migrate_images:start_migration
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
PropertyImage [11] has an error. Validation failed: Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError
PropertyImage [12] has an error. Validation failed: Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError

请注意,我阅读了类似的文章,并在 application.rb 的末尾添加了以下内容:

Paperclip.options[:command_path] = "/usr/local/bin/identify"

我确定我已经安装了 imagemagick:

⇒  brew install imagemagick
Warning: You have an outdated version of /usr/bin/install_name_tool installed.
This will cause binary package installations to fail.
This can happen if you install osx-gcc-installer or RailsInstaller.
To restore it, you must reinstall OS X or restore the binary from
the OS packages.
Warning: imagemagick-6.8.9-7 already installed

任何帮助将不胜感激。

如果我尝试使用 rails 控制台重现问题,请注意:

>> p = Property.find(93746)
>> p.property_images.each do |pi|
?> pi.picture.reprocess!
>> end
  PropertyImage Load (1.4ms)  SELECT "property_images".* FROM "property_images" WHERE "property_images"."invalid_image" = 'f' AND "property_images"."property_id" = $1 ORDER BY "property_images"."apartment_main" DESC  [["property_id", 93746]]
[paperclip] copying /property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg to local file /var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg
[AWS S3 200 1.212736 0 retries] get_object(:bucket_name=>"my_bucket_development",:key=>"property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg")

Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
[paperclip] saving /property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg
[AWS S3 200 0.293473 0 retries] put_object(:acl=>:public_read,:bucket_name=>"my_bucket_development",:content_length=>0,:content_type=>"image/jpeg",:data=>Paperclip::AttachmentAdapter: 722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg,:key=>"property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg")

   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK

【问题讨论】:

  • 表示文件为not recognized as an image。运行brew update imagemagick
  • 感谢您愿意帮助@Substantial,我做到了,并且已经安装了最新的 imagemagick 版本imagemagick-6.8.9-7。我假设你的意思是 brew upgrade imagemagick
  • 您是否也卸载/重新安装了rmagick gem?
  • @Substantial 查看 README 的“要求”部分:github.com/thoughtbot/paperclip,回形针不需要 rmagick。我一直在使用回形针,而不需要 rmagic。我安装它以防万一,但它也没有帮助:(

标签: ruby-on-rails imagemagick paperclip


【解决方案1】:

确保您正在使用的服务器上的 Imagemagick 支持您尝试上传的格式。

要确保这一点,请使用以下命令:

convert -list format

如果您尝试使用 Paperclip 上传的格式不在列表中,您需要安装所需的库,然后重新安装/重新编译 Imagemagick。

Here 给出了如何在 Unix 机器上执行此操作的示例。要安装 jpg 库,请使用以下命令:

yum install libjpeg libjpeg-devel

如何安装库和重新安装/重新编译 Imagemagick 取决于您使用的操作系统以及您需要的图像格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多