Paperclip cropping error using jcrop and s3

This one is a very specific and somewhat common problem while cropping images via paperclip.

Problem description:

I took inspiration from a railscasts episode: http://goo.gl/Apykh

I am using s3 you can follow this link to setup: http://goo.gl/7pCDp

However, I have to make some changes as that railscasts episode is based on old paperclip versions. Here are some code snippets and changes to be made in that code:

Inside production.rb:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
  :bucket => ENV['AWS_BUCKET'],
  :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
  :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

I have a model as follows:

class UserImage < ActiveRecord::Base
  attr_accessible :user_id, :user_image, :crop_x, :crop_y, :crop_w, :crop_h
  attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

  has_attached_file :user_image,
   :styles => { :original => '360x360!', :large => "90x90>"},
   :path => "/user_images/profile_images/:id/:style/:basename.:extension",
   :url => "/user_images/profile_images/:id/:style/:basename.:extension",
   :processors => [:cropper]

  belongs_to :user

  validates_attachment_content_type :user_image,
    :content_type => ["image/jpg","image/png","image/jpeg","image/bmp","i  mage/pjpeg"]

  after_update :reprocess_user_image, :if => :cropping?

  def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
  end

  def photo_geometry(style = :original)
    @geometry ||= {}
    path = (user_image.options[:storage]==:s3) ? user_image.url(style) : user_image.path(style)
    @geometry[style] ||= Paperclip::Geometry.from_file(path)
  end

  private
  def reprocess_user_image
    user_image.reprocess!
  end
end

The customized lib/paperclip_processors/cropper.rb

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.join(' ').sub(/ -crop \S+/, '').split(' ')
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping?
        ["-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}"]
      end
    end
  end
end
 


When user submits the form with the crop area defined (crop_x, crop_y, crop_h, crop_w), the form submits to a controller action.

Inside controller action:

user.user_image.update_attributes(params[:user_image])

This code is intended to upload and image and crop the image according to the crop area selected by user.

But this code is running into an infinite loop where the convert command is executed infinite times in round-robin fashion for all custom sizes.

Solution:

After a long time, luckily I was able to observe that there some problem with the gem version I was using. If you had came across a similar issue, here is the snapshot of my Gemfile:

gem 'paperclip', '3.1.4'
gem "mini_magick", "~> 3.4"
gem 'cocaine', '0.3.2'
gem 'rmagick'

After making changes, run bundle command to install/modify gems.
Cocaine and paperclip were making the issues because of conflicting versions.

Hope this will help resolve your issue too.

Advertisements

One thought on “Paperclip cropping error using jcrop and s3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s