Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize already uploaded Paperclip images to s3 using reprocess? (Rails)

I have a model:

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

I'm using Rails (4.0.1), cocaine (0.5.4) and paperclip (3.5.4).

I want to migrate old images (with no thumb, large and medium) and resize each, so I created a rake script:

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

And ImageConverter class:

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

Now when I run the script I keep getting the following error:

⇒  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

Please note that I read similar articles and added the following to the end of application.rb:

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

And I made sure I have imagemagick installed:

⇒  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

Any help would be highly appreciated.

Please note if I try to reproduce the problem using 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
like image 869
Eki Eqbal Avatar asked Jan 20 '26 04:01

Eki Eqbal


1 Answers

Make sure that the format you are trying to upload is supported by Imagemagick on the server you are using.

To make sure of this, use this command:

convert -list format

If the format you are trying to upload with Paperclip is not in the list, you need to install the libraries needed and then reinstall/recompile Imagemagick.

Here is given an example of how to do that on a Unix machine. To install the jpg library, it is used the command:

yum install libjpeg libjpeg-devel

How to install the library and reinstall/recompile Imagemagick depends on which OS you use and on which image format you need.

like image 67
clami219 Avatar answered Jan 22 '26 16:01

clami219



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!