Image resize issue with PaperClip and Rails 3 - ruby-on-rails-3

Having terrible trouble with my application image rendering since an upgrade to Ruby 1.9.3, Rails 3.1.0.rc4 and Paperclip 3.4.0.
No matter what variation of settings I give Paperclip the infile linked to below comes out blurred as is shown by the linked outfile.
The outfile must fit into a box of 620x412 as shown here.
Link to input file
Link to output file this code generates
The full code for the model is below ...
class Propertyimage < ActiveRecord::Base
belongs_to :property
validates_presence_of :description
validates_presence_of :sortorder
has_attached_file :image, :styles => { :export => {:geometry => "620x412#", :quality => 100, :format => 'JPG'} },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
end

I had a similar problem and was able to fix after a lot of trial and error using three criteria: image size specification, convert_options, and scaling the image. For example, in your Propertyimage class try:
has_attached_file :image,
:styles => { :original => ["640x480", :jpg], :export => {:geometry => "620x412#", :quality => 100, :format => 'JPG'} },
:convert_options => { :all => "-quality 100" },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
Then you can play with the sizes of your image tag, or as in my case using a PDF, Id used the scale option:
pdf.image the_file_name, :at => [0, 720], :scale => 0.75

Related

Paperclip-av-transcoder not generating thumbnails in rails app

I am using paperclip-ac-transcoder gem for uploading videos in my rails app. Now, the videos are being successfully saved at the application path, but it's not generating the thumbnails for it. Moreover, the video_tag is not working to display the video also.
Below is my code :-
has_attached_file :movie,
:url => ":assets_host/system/:class/:attachment/:id/:style/:filename",
:path => ":rails_root/public/system/:class/:attachment/:id/:style/:filename",
:medium => { :geometry => "640x480", :format => 'mov', :streaming => true },
:thumb => { :geometry => "100x100#", :format => 'png', :time => 10 },
:processors => [:transcoder], :swallow_stderr => false
For video tag I am using the following:-
<%= video_tag "#{:rails_root}/public/system/sources/uploaded_movies/movies/10/original/MyMov.mov", :size => "320x240", :controls => true %>
Please guide.
Thanks in advance.
That was a known issue with the gem that has been fixed. Try upgrading to the latest version.

Paperclip::Errors::NotIdentifiedByImageMagickError + Heroku

I currently have an application using paperclip that allows users to upload their creatives. This has worked flawless thus far when it comes to a user uploading an image file. We have since tested to upload a .mov file and I get this error:
Creative Paperclip::Errors::NotIdentifiedByImageMagickError
The weird thing, this error is only generated on Heroku. I can upload .mov files just fine on my local host.
My Current Gem Setup:
paperclip (3.4.1, 3.4.0)
paperclip-aws (1.6.7, 1.6.6)
paperclip-ffmpeg (0.10.2)
cocaine (0.5.1, 0.4.2)
Event.rb
has_attached_file :creative,
:processors => [:ffmpeg],
:styles => {
:thumb => [:geometry => "250x150", :format => 'png'],
:custcreative => [:geometry => "275x75", :format => 'png'],
:creativepreview => ["275x195",:png]
},
:url => "***",
:path => "***",
:s3_domain_url => "***",
:storage => :s3,
:s3_credentials => Rails.root.join("config/s3.yml"),
:bucket => '***',
:s3_permissions => :public_read,
:s3_protocol => "http",
:convert_options => { :all => "-auto-orient" },
:encode => 'utf8'
Spending hours trying to figure out why this works locally but throwing error on Heroku.
I even tried removing the :style setting, but still did not work.
TIA
EDIT
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/MidPen20130413-2-1mzetus.mov[0]'
Well, here is the answer in case any other newbies like us come across the same problem. The issue is with the geometry method that is being used for image cropping. The way it is suggested in railscasts assumes the file is in a local system and that needs to be changed.
OLD METHOD:
def avatar_geometry(style = :original)
#geometry ||= {}
#geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
end
NEW METHOD
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

Saving only processed images in paperclip

my paperclip setup is
has_attached_file :avatar, :styles => { :medium => "300x300", :thumb => "100x100"},
:storage => :s3,
:bucket => ENV['BUCKET'],
:s3_credentials => {
:access_key_id => ENV['ACCESS_KEY'],
:secret_access_key => ENV['SECRET_KEY']
}
I don't want to have size limitations, since it will hassle the user to resize his/her images.
so, I put styles in for medium and thumb.
However, the original file is still saved in s3.
How do I avoid saving the original image?
Use a callback to destroy the image after the processing has been completed. This Gist contains an example

inconsistant photo url using paperclip when query in two apps

I am using paperclip to upload photos in my two rails app which share a single database. Now the problem is, if I upload a photo in app-a, for instance, paperclip gives me a url as:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-a/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Here is what I set up in my item model:
class Item < ActiveRecord::Base
attr_accessible :photo, :photo_url, :poll_id, :brand, :number_of_votes
has_attached_file :photo,
:styles => { :thumbnail => "100x100#",
:small => "150x150>",
:medium => "250x250>",
:large => "400x400>" },
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url=>"/item_:id/created_at_:created_at/:style.jpg"
Paperclip.interpolates :created_at do |attachment, style|
attachment.instance.created_at
end
end
In the other app app-b, when I query the url with item.photo.url(:large), it gave me:
http://s3.amazonaws.com/testing-item-photos-akiajbfjoiwfjzd6aypa/Users/yujunwu/app-b/public/item_19/created_at_2012-09-29%2021:52:02%20UTC/large.jpg?1348955522
Therefore I got a wrong url.
Are there any ways I can do that by configuring paperclip? Thanks!
As described here : https://github.com/thoughtbot/paperclip#defaults , you can change the default config, in your case I guess you want to change the :local_root so it doesn't include the app name.

Rails 3, Paperclip - Custom Interpolations

I've been having some troubles making custom Interpolation, gone through every example I could find on web, but no matter what I did, had no success.
At the moment I have this:
Model
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
initializers/paperclip.rb
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
img_name is field populated in form on upload with the image.
The error I get on upload is:
Invalid argument - (C:/Users/.../stream20110410-384-stl2lk20110230-213-1fm2bab, C:/.../photo_upload/public/images/:img_name-original.jpg)
Seems to work if it's directly in the model:
class Model < ActiveRecord::Base
Paperclip.interpolates :img_name do |attachment, style|
attachment.instance.img_name
end
has_attached_file :photo,
:path => ":rails_root/public/images/:img_name-:style.:extension",
:styles => {
:original => '100x100',
:thumb => '30x30'
}
end