Can't resize uploaded image - ruby-on-rails

I'm using paperclip gem for uploading and resizing images. This setup works fine. I'm able to display the uploaded images. The problem comes when I try to resize the uploaded image.
Here is snippet from the model file
has_attached_file :photo,
:size => {:small => "150x150>"}
When I try to upload the image I get this error.
Photo /var/folders/gm/gm-SegRMHuOkSlYtTMkO8U+++TI/-Tmp-/file.jpg is not recognized by the 'identify' command.
I'm sure that the file is jpg. Here is the output of the file command
file.jpg: JPEG image data, JFIF standard 1.01, comment: "CREATOR: gd-jpeg v1.0 (using IJ"

I'm not sure but in our application we do the same thing and it works. Our code looks like this:
has_attached_file :image,
:styles => {:small => "280x173#", :medium => "635x393#"},
:convert_options => {:all => "-quality 80"},#,
:default_style => :medium,
:default_url => "/images/study/nophoto.jpg"
validates_attachment_size :image, :less_than => 10.megabyte
validates_attachment_content_type :image, :content_type => ['image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg']
The difference I see, is that you might have to provide convert_options to be able to resize.

Have you tried any other jpg file, maybe with a simpler path also?

Related

Paperclip post processing conversion does not update file_name, content_type attributes

I would like to restrict File uploads to images only, and convert them automatically to .png. To do so, I use this class:
class ImageAttachment < ActiveRecord::Base
attr_accessible :file, :file_file_name, :file_content_type, :file_file_size
validates_attachment :file,
:content_type => { :content_type => ["image/jpg", "image/tiff", "image/png"] }
has_attached_file :file,
:styles => { :original => ["100%", :png],
:large => ["500x500", :png],
:medium => ["150x150", :png],
:thumb => ["75x100", :png]
},
:default_url => "/system/missing_thumb.png"
end
As I understand, the :styles => { :original => ["100%", :png], ...} should convert all uploaded files that pass validation to .png files. Therefore, I expect the following things to happen when uploading a file example.tiff:
convert the file to .png
change the file name accordingly to example.png
change the content type accordingly to "image/png"
Here's a spec I use:
it "should convert all image types to .png" do
test_file = File.new(Rails.root + "spec/fixtures/images/test.tiff")
attachment = ImageAttachment.create :file => test_file
attachment.file.url.should == "some/paperclip/path/.../test.png"
attachment.file_file_name.should == "test.png"
attachment.file_content_type.should == "image/png"
end
The first assertion is true, and I can also see ImageMagick output in the terminal,
but attachment.file_file_name still returns example.tiff, and attachment.file_content_type returns "image/tiff".
Is my assumption that paperclip automatically updates the file_file_name and the file_content_type attributes wrong?
If so, how would I best do this on my own?

Paperclip not generating thumbnails, failing silently

I am pulling my hair out here... I have no idea why this isn't working.
I am using ruby 1.8.7, rails 3.0.19, paperclip 2.7.4.
My model:
has_attached_file :photo, :styles => {
:logo => "60x20",
:widget => "60x40",
:thumb=> "100x100",
:small => "150x150>" },
:url => "/images/companies/:id/:style/:basename.:extension",
:path => ":rails_root/public/images/companies/:id/:style/:basename.:extension",
:default_url => "/images/bb_noimage.png"
#validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 3.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png','image/gif','image/jpg']
In views:
<%= image_tag "#{get_image_url(company.id,company.photo_file_name,"small")}"%>
The problem is some folders will have a "large" and "thumb" size but will not have a "small" and "logo" size - I am revisiting this site after a long time of having not used it. I want the missing styles to be generated. I tried these commands and the first shows no error, but the second shows the error below. Niether generate the needed thumbnails. Thank you for your help!
bundle exec rake paperclip:refresh:thumbnails CLASS=Company
bundle exec rake paperclip:refresh:missing_styles --trace
** Execute paperclip:refresh:thumbnails
rake aborted!
No such file or directory - /Users/q/Sites/baiabase-old/public/system/paperclip_attachments.yml
/Users/q/.rvm/gems/ruby-1.8.7-p302#baia-old/gems/paperclip-2.7.4/lib/paperclip/missing_attachment_styles.rb:25:in `initialize'
add to your model
attr_accessible :photo, :photo_file_name
has_attached_file :photo, :styles => {.....
....your code...
and in views
<%= image_tag #company.photo.url(:small) %>
And more to you as a gift, it is
For photos of the original 512x512 pixels will be better, will take up less space on your hard drive on the server
add style :original => "512x512",
all attached photo converted maximum to this resolution, that save space on your hard drive.
:less_than => 3.megabytes change to :less_than => 5.megabytes
If the user tries to attach a photo larger than 3 MB then it will not do it, change it to 5 MB
the user can add photos which has a resolution of 3000 x 3000 or more (<= 5 mb) but it was all smooth will be converted to 512 x 512 pixels which weight will be no more than 20 KB.
And if you want to convert all attached photos to jpg (format) try this :thumb => {:geometry => '100x100#', :format => :jpg},
and attentively with the Aspect ratio 4:3 '100x100#' and Aspect ratio 16:9 '100x100>'.

Paperclip, copy image without reprocessing it

When I copy a paperclip attachment it applies the default cropping defined in the style and ignores the cropper I have installed for cropping on the site via user input (CropperJs). What I need is just a raw copy of the image as it was cropped on the source image. I solve this by bluntly copying the file right now, but is there a better way?
has_attached_file :avatar,
:name => 'avatar',
:styles => {
:cropped => {
:geometry => "55x55#",
:processors => [:cropper]
},
:large => "600x600>"
}

Paperclip avatars image missing if user didnt uploaded image

I am using Paperclip for uploading the Profile image in my application (rails)
My User model is having a
has_attached_file :avatar,
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
:styles => { :medium => "300x300>", :small => "100x100>", :thumb => "50x50>", :micro => "30x30>" }
In my VIew If i have a profile image uploaded then its pointing correctly as
/avatars/1/thumb/iamge
BUt if the image is not there if they didn't uploaded in that case its pointing as
/avatars/thumb/missing.png which doesn't have any image.
Please give suggestions what to do if the user didnt uploaded any profile image..
You can make a default image and put it there and name it missing.png. It is like extra functionality :)

How to pass additional convert options to paperclip on Heroku?

class User < ActiveRecord::Base
has_attached_file :photo, :styles => { :square => "100%", :large => "100%" },
:convert_options => {
:square => "-auto-orient -geometry 70X70#",
:large => "-auto-orient -geometry X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
validates_attachment_size :photo,
:less_than => 5.megabyte
end
Works great on local machine, but gives me an error on Heroku: There was an error processing the thumbnail for stream.20143
The thing is I want to auto-orient photos before resizing, so they resized properly.
The only working variant now(thanks to jonnii) is resizing without auto-orient:
...
as_attached_file :photo, :styles => { :square => "70X70#", :large => "X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
...
How to pass additional convert options to paperclip on Heroku?
UPD
I discover, the trouble in "-auto-orient" option. It seems like this option is broken in version of ImageMagick used by Heroku. I created custom paperclip image processor inherited from paperclip's standard thumbnail:
module Paperclip
class Ao < Thumbnail
def transformation_command
super + " -auto-orient"
end
end
end
It works perfect on local machine, but fails on Heroku.
These are the sizes I use. They all work fine on heroku:
SIZES = {
:original => "640x480>",
:thumb => "150x150#",
:mini => "60x60#",
:micro => "30x30#"
}
Make sure your gem version of paperclip is the same as heroku's. You can specify the specific gem version in your .gems file and in your environment.rb to make sure they line up.
I'm not sure exactly why your convert_options are causing problems, but if I remember correctly paperclip uses ImageScience directly and your chosen options might be incompatible with the read only heroku file system.
If this is critical and you need an answer right now I'd raise a support ticket on heroku. If you get a response make sure you post it back here!

Resources