2014-10-27 29 views
2

我有一個模型:如何使用重新處理將已上傳的Paperclip圖像調整爲s3? (Rails)的

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

我要遷移舊圖像(無拇指,大中),並調整每個,所以我創建了一個耙腳本:

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 

請注意,我讀了類似的ar ticles並添加以下內容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 
+1

這意味着該文件[未被識別爲圖像](http://www.rubydoc.info/gems/paperclip/Paperclip/Errors/NotIdentifiedByImageMagickError)。運行'brew update imagemagick'。 – Substantial 2014-10-27 14:50:07

+0

可能重複的[回形針錯誤 - NotIdentifiedByImageMagickError](http://stackoverflow.com/questions/23689576/paperclip-error-notidentifiedbyimagemagickerror) – Substantial 2014-10-27 14:50:38

+0

謝謝你願意幫助@Substantial,我做到了,我有最新的imagemagick versionimagemagick- 6.8.9-7已經安裝。我假設你的意思是沖泡升級imagemagick – 2014-10-27 14:54:59

回答

1

確保您要上傳格式由ImageMagick的您正在使用的服務器上的支持。

爲了確保這一點,使用這個命令:

convert -list format 

如果你想用回形針上傳格式不在列表中,您需要安裝所需的庫,然後重新安裝/重新編譯的ImageMagick 。

Here給出了一個在Unix機器上如何做到這一點的例子。要安裝JPG庫,使用它的命令:

yum install libjpeg libjpeg-devel 

如何安裝庫,並重新安裝/重新編譯的ImageMagick取決於您所使用的操作系統和其上的圖像,你需要的格式。

相關問題