Paperclip not replacing the cropped image. at s3 - ruby-on-rails-3

I am using paperclip for uploading the images. My storage space is at S3. I am using Jcrop with paperclip for crop the images and have followed the #RailsCast182.
Everything is working smoothly, there were some issues in the RailsCast which i have resolved.
I want to process the crop file and upload it at s3.
Here is my log showing that image is cropped
Command :: file -b --mime-type '/tmp/249ac5b855195d2e761e123315dd229b20140701-3395-1mz9lji'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' 2>/dev/null
Command :: identify -format %m '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]'
Command :: convert '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' -crop 2586x2586+2534+0 -auto-orient -resize "100x100>" '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-stdr75'
convert.im6: geometry does not contain image `/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl' # warning/transform.c/CropImage/574.
Command :: file -b --mime '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-stdr75'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' 2>/dev/null
Command :: identify -format %m '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]'
Command :: convert '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl[0]' -crop 2586x2586+2534+0 -auto-orient -resize "50x50>" '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-bhth1i'
convert.im6: geometry does not contain image `/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl' # warning/transform.c/CropImage/574.
Command :: file -b --mime '/tmp/4e6c43285b27dbc35603a428c4e4ca9920140701-3395-moimyl20140701-3395-bhth1i'
After that it shows that image is uploaded at s3 but it was the same org image that uploaded.
My attachment model:
class Attachment < ActiveRecord::Base
has_attached_file :file, styles: { medium: "100x100>", thumb: "50x50#" }, :processors => [:cropper]
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
end
#paperclip custom cropper
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
But paperclip never change the image at S3.
I have gone through many link but didn't touch the sky.
Looking forward a help from great community.
Regards
Adam

You need to change cropper.rb in your lib. Cropper work correct but take only image center. If you change you cropper.rb your images saves fine
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
original_command = super
if original_command.include?('-crop')
original_command.delete_at(super.index('-crop') + 1)
original_command.delete_at(super.index('-crop'))
end
crop_command + original_command
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
and then you need check your code in model
def avatar_geometry(style = :original)
#geometry ||= {}
avatar_path = (avatar.options[:storage] == :s3) ? avatar.url(style) : avatar.path(style)
#geometry[style] ||= Paperclip::Geometry.from_file(avatar_path)
end
if s3 save all file in one size you need to change this code to
has_attached_file :avatar, :styles => {
:thumb => {:geometry => "90x50#", :processors => [:cropper, :thumbnail]},
Attention! You need restart your server after change this file

Related

Carrierwave + s3 + fog (Excon::Errors::SocketError)

I'm currently getting the following error: Excon::Errors::SocketError - Broken pipe (Errno::EPIPE) when uploading images bigger than about 150kb. Images under 150kb work correctly. Research indicates that others have also experienced this problem but I'm yet to find a solution.
Error message
Excon::Errors::SocketError at /photos
Message Broken pipe (Errno::EPIPE)
File /Users/thmsmxwll/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/openssl/buffering.rb
Line 375
image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :large do
process :resize_to_limit => [800, 600]
end
end
carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
:region => 'us-east-1'
}
config.fog_directory = 'abcd'
config.fog_public = true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
For me, the solution required me to recreate the bucket in the US-Standard region. Originally, the bucket was in the Oregon region and while I wasn't specifying a region in my carrierwave settings, I could not get an upload to complete, even with very small files.
I'm having the same issue, i noticed that only happend when i upload big files (400kb), with a smaller (100kb) it works fine.

How to process only on original image file using CarrierWave?

I use CarrierWave to generate versions (thumbnails with different sizes) and also to add a watermark on each versions.
I have currently manage to apply the watermark for each thumbnails but I would like to add it on the original image to.
Here is what I tried:
def watermark
manipulate! do |img|
watermark = Magick::Image.read(Rails.root.join('app/assets/images/watermark_512.png')).first
img = img.composite(watermark, Magick::CenterGravity, Magick::OverCompositeOp)
end
end
version :original do
process :watermark
end
version :thumb_512 do
process :resize_to_fit => [512, 512]
process :watermark
end
version :thumb_256 do
process :resize_to_fit => [256, 256]
process :watermark
end
But this does not work. However I tried to simply add
process :watermark
outside any "version" block but all it does is adding twice the watermark on my thumbnails.
You can use store callbacks provided by CarrierWave in your uploader class like this
class SomeUploader < CarrierWave::Uploader::Base
# Your code ...
before :store, :watermark_method
def watermark_method(*args)
# watermark it!
end
end

Image invalid on facebook/twitter user image uploading to S3

I'm trying to upload to amazon s3 an existing image on facebook or twitter from an user that has just signed up in my application, but some validation don't let me save the user object, throws: Image is invalid. I thought that was for my extension_white_list but I removed it and it's keeping saying Image is invalid.
This it's not an error, it's just a message from a validation on carrierwave I think, even if the image url string is correct.
AvatarUploader
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
include CarrierWave::RMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include CarrierWave::MimeTypes
process :set_content_type
def store_dir
"avatar/#{model.id}"
end
version :thumb do
process resize_to_fill: [50, 50]
end
# def extension_white_list
# %w(jpg jpeg gif png bmp)
# end
end
Creating user:
...
new_user = User.create( :name => auth['info']['name'],
:email => User.email_from_auth(auth) )
auth_image_url = Authentication.larger_image(auth) # a string of user image url from social network authentication data. ie: http://a0.twimg.com/profile_images/1255111511/skyline.jpg
unless auth_image_url.blank?
new_user.remote_image_url = auth_image_url
new_user.save
end
...
Fixed! The error has nothing to do with carrierwave, just the fact that the object does not exist on database in the moment where the image is upload, first I save the new user and then:
after_create :upload_image_from_auth
def upload_image_from_auth
auth = self.authentications.first
unless auth.nil?
self.remote_image_url = auth.larger_image
self.save
end
end

rails carrierwave / s3 signature doesn't match. Wrong private key

I'm trying to get carrierwave working with S3, and I am getting a signature doesn't match error. The weird thing is that it doesn't look like carrierwave is sending the right secret key. In the error, the post is given as:
"Authorization"=>"AWS AKIAIOLWQKSJFH6E3I5Q:vKgyAw2z4c8zzqGWoxLUbw7I5oI="
Which I assume is supposed to be my publickey:privatekey. The thing is that vKgyAw2z4c8zzqGWoxLUbw7I5oI= is not the private key I have stored in fog.rb. Is that right?
Any help is appreciated.
Request/Response:
request => {:chunk_size=>1048576, :connect_timeout=>60, :headers=>{"Content-Length"=>1557, "Content-Type"=>"image/
gif", "x-amz-acl"=>"public-read", "Date"=>"Wed, 24 Oct 2012 12:45:17 +0000", "Authorization"=>"AWS AKIAIOLWQKSJFH6E3
I5Q:vKgyAw2z4c8zzqGWoxLUbw7I5oI=", "Host"=>"s3.amazonaws.com:443"}, :instrumentor_name=>"excon", :mock=>false, :nonb
lock=>true, :read_timeout=>60, :retry_limit=>4, :ssl_ca_file=>"/home/tim/.rvm/gems/ruby-1.9.3-p194/gems/excon-0.16.5
/data/cacert.pem", :ssl_verify_peer=>true, :write_timeout=>60, :host=>"myeasybnb.s3.amazonaws.com", :host_port=>"s3.
amazonaws.com:443", :path=>"/images%2Fb1bb6639-dc08-4981-9a9b-7175093ac970.gif", :port=>"443", :query=>nil, :scheme=
>"https", :body=>#<File:/home/tim/Dropbox/myeasybnb/tmp/uploads/20121024-0845-20225-1170/240x240.gif>, :expects=>200
, :idempotent=>true, :method=>"PUT"}
response => #<Excon::Response:0xa7a1098 #body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>SignatureD
oesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your
key and signing method.</Message>
fog.rb:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'AKIAIOLWQKSJFH6E3I5Q', # required
:aws_secret_access_key => '[my secret key]' # required
}
config.fog_directory = 'myeasybnb' # required
config.fog_public = true # optional, defaults to true
end
Uploader.rb:
# encoding: utf-8
class PhotoUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"images"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
class MyUploader < CarrierWave::Uploader::Base
def default_url
asset_path("seeds/" + [version_name, "default.png"].compact.join('_'))
end
end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# Create different versions of your uploaded files:
process :convert => 'jpg'
version :sidecarousel, :if => :is_side_carousel? do
process :resize_to_fit => [2000,1000]
end
version :thumbnail, :if => :is_thumbnail? do
process :resize_to_fill => [240,240]
end
version :linetext, :if => :is_line? do
process :resize_to_fill => [400,200]
end
version :carousel, :if => :is_carousel? do
process :resize_to_fit => [2200, 1000]
end
version :phone do
process :resize_to_fit => [900,900]
end
# def scale(width, height)
# # do something
# end
def is_side_carousel? photo
model.location == 1
end
def is_thumbnail? photo
model.location == 2
end
def is_line? photo
model.location == 3
end
def is_carousel? photo
model.location == 4
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
def filename
file.nil? ? #filename = nil : #filename = "#{secure_token}.#{file.extension}"
end
def secure_token
var = :"##{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
end
I managed to fix this. I'm not exactly sure what I did, but there was a couple issues. (1) I think I was getting the auth error because I was specifying a folder in my uploader. Now, I set my directory in the uploader to "" and specify the folder through the fog configuration.
(2) Another error I was getting was a time mismatch. I run mint in a virtual machine for development, and the time was different from reality. Amazon threw an error at that. Once I set the correct time, that went away.

Carrierwave resize is not working (Rails 3 and MiniMagick)

what I want to do is to save a website url with a full-size snapshot via IMGKit. In one of the views I also want to have a thumbnail version of the snapshot. I'm using carrierwave in order to associate the snapshot with the object an MiniMagick to manipulate it, the problem is that it generates the 'thumbnail' image but doesn't resize it so as a result, I have two full-size snapshots, one of then with 'thumb' as prefix.
I have this model in rails
class Webpage
mount_uploader :snapshot, SnapshotUploader
field :url, type: String
field :title, type: String
after_create :get_snapshot
private
def get_snapshot
file = Tempfile.new(["#{id}#{title}".downcase, '.jpg'], 'tmp', :encoding => 'ascii-8bit')
image = IMGKit.new(url, quality: 90).to_jpg
file.write(image)
file.flush
self.snapshot= file
self.save
file.unlink
end
end
And I have this in the Uploader in order to create the thumbnail version:
class SnapshotUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
version :thumb do
process resize_to_fill: [180, 180]
end
end
Using the console I tried MiniMagick for resizing an image and it works fine son I don't know what is happening.
I'm not sure if I'm doing it right so any help would be appreciated. Thanks.
OK, I'm stupid. I had an initializer with
config.enable_processing = false
so it will never process the image. Just setting it to true or deleting the line solved my problem.