如何使用重新处理将已上传的Paperclip图像调整为s3? (导轨)

时间:2014-10-27 12:12:18

标签: ruby-on-rails imagemagick paperclip

我有一个模特:

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 console重现问题:

>> 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

1 个答案:

答案 0 :(得分:1)

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

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

convert -list format

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

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

yum install libjpeg libjpeg-devel

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