Rails 3 & Paperclip: "not recognized by the 'identify' command" - ruby-on-rails

A few system details:
Mac OS X Lion 10.7.2, Rails 3, Paperclip gem.
ImageMagick 6.7.3-0, binaries installed using MacPorts in /opt/local/bin.
Using WEBrick in development environment.
I'm trying to upload photos that should get resized down into thumbnails. When I do so, the original uploaded file is saved and can be accessed via HTTP correctly. When I try to accessed the thumbnail version, I get this error:
Routing Error
No route matches "[file_URL]"
Before that, when uploading a valid PNG file, I see this error in my WEBrick log:
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: /var/folders/n4/62q22gb52rjd0h13cx_j8vv40000gq/T/stream20111020-24984-17560xt-0.png is not recognized by the 'identify' command.>
Doing which identify outputs:
/opt/local/bin/identify
Calling identify with the path of the uploaded file correctly identifies the file as a PNG file.
I confirmed rails server starts the development environment. I added the following in config/environments/development.rb:
Paperclip.options[:command_path] = "/opt/local/bin"
My Photo model has the following:
class Photo < ActiveRecord::Base
has_attached_file :file, :default_style => :view, :styles => {
:view => { :geometry => '520x390>', :format => 'jpg' },
:preview => { :geometry => '160x120>', :format => 'jpg' } }
validates_attachment_content_type :file,
:content_type => [ 'image/jpeg', 'image/pjpeg', 'image/png' ]
end
My Photos controller:
class PhotosController < ApplicationController
# ...
def create
#photo = Photo.create params[:photo]
end
end
Like I said, I can access the original images in the URL path system/files/:id/original, but the resized versions I want aren't accessible and are not found in the filesystem. WEBrick's log suggest Paperclip can't even have ImageMagick identify the images before resizing them.
Any ideas? Thanks in advance!

This may be a duplicate question, as stated above, but this problem was caused by a recent bug in ImageMagick 6.7.3-0 fixed soon after in 6.7.3-1. MacPorts has also been updated with the new release.
In 6.7.3-0, stating the frame number in the file path (e.g. identify /path/to/file.png[0]) caused ImageMagick to segfault ("Segmentation fault 11"). Paperclip specified the frame number to ensure its processing the first frame or page of an image file.
The new release of ImageMagick solved this problem. The bug was mentioned on MacPorts:
https://trac.macports.org/ticket/31643

Related

Can't upload audio file to Cloudinary using Rails

I'm trying to add the audio file to Cloudinary storage. I got stuck when
error show:
CloudinaryException (Invalid resource type audio):
app/controllers/musics_controller.rb:23:in `create'
I've added configuration in my Music.rb file from another help in stack overflow:
has_attached_file :song, :storage => :cloudinary, path: 'muvent/musics/:filename', :cloudinary_resource_type => :audio
validates_with AttachmentSizeValidator, attributes: :song, less_than: 10.megabytes
validates_attachment_content_type :song, :content_type => [ 'application/octet-stream', 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]
But, I still can't upload the file to Cloudinary. I also added permit for song column in my controller music_params which the error come. How can I fix this?
Update
I've fixed the error above. But why another error in logs server show file -b --mime in rails? Gemfile I use in here paperclip and paperclip-cloudinary.
Please make sure to use the right resource_type (a Cloudinary parameter) according to the file's type. The default resource type is image. Set it to video when uploading audio/video files or raw when uploading non-media files (such as doc,csv,css,js, etc).

Paperclip not showing image even though URL/images in correct places

Using Paperclip to attach avatars to User profiles for my rails application. I followed the instructions on the paperclip github to initialize and attach to my app.
I have an image in the public/images/medium/missing.png and for both cases (when I upload or when I fallback on the default) I get no image. I've checked my directory and there is an image where it says it is looking but does not grab it. Additionally when I have tried uploading images, I know the image is uploaded correctly because when calling the User in rails console shows all the information properly attached.
I am calling the image in my view like:
<%= image_tag(#blog.user.avatar.url(":medium"), :class => "image-circle avatar") %>
my Paperclip declaration in the User model looks like the following:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#"}, :default_url => "/public/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
Really not sure what is going on. The route errors that appear when I inspect the improperly loaded image point directly to the image in my local server. And the fact that it can't grab either the missing or the uploaded file also has me at a loss. Any help would be super appreciated!!
And for good measure the output when I examine a user with an uploaded avatar:
avatar_file_name: "11390219_10206114805925816_6595348111261743639_n.j...", avatar_content_type: "image/jpeg", avatar_file_size: 101926, avatar_updated_at: "2015-07-10 18:51:44">
Thanks in advance!
EDIT
This is the URL that is providing the 404 error:
http://localhost:3000/images/medium/missing.png
while in my local directory it goes "root/public/images/medium/missing.png"
not sure how its not grabbing it, unless I am just missing something really obvious somewhere. (i tried hard routing the public in there as well, but to no avail).
EDIT
There is the possibility that you're simply not serving the static assets, add:
config.serve_static_assets = true
to your development.rb
ORIGINAL POST
In you application.rb ( or an environment specific file ), you should have a config.paperclip_defaults = { ... }, here is the link in the docs: https://github.com/thoughtbot/paperclip#defaults
Here is an example one, using fog:
config.paperclip_defaults = {
:storage => :fog,
:fog_credentials => {
:provider => "Local",
:local_root => "#{Rails.root}/public"
},
:fog_directory => "",
:fog_host => "localhost:3000"
}
Do you have something like that in your application? I just tested on an app of mine, and I was able to upload an image, but not to see any without the paperclip_defaults hash. Also, don't forget to restart your app after you update the config files. I hope this helps!
Have you tried killing the quotes around the image style? Around :medium?
<%= image_tag(#blog.user.avatar.url(:medium), :class => "image-circle avatar") %>
As show here in the Paperclip Docs:
https://github.com/thoughtbot/paperclip#show-view
Same problem, solved it by reinstalling imagemagic with brew
Details: In case you're imagemagic, try to update the imagemagic and if it asks you to link it,
Try with this:
brew link --overwrite imagemagick
It worked for me. Hope its helpful

ruby on rails - 1 error prohibited this listing from being saved: Image has contents that are not what they are reported to be

1 error prohibited this listing from being saved:
Image has contents that are not what they are reported to be
That's the error that I get when I try to upload a picture for a listing. I've tried various types of validations and nothing is working.
This is what my Model looks like.
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb =>"100x100>" }, :default_url => "default.jpg"
validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }
end
Can someone please explain to me what I did wrong, and what I can do to fix it. I really want to keep working on this application but I've hit a problem!
You need to change the following
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
Also make sure that you are indeed uploading images with the mentioned extensions
It may be caused because you don't have file program (probably you don't use Unix system?)
file is used by paperclip to check whether sent file is what it should be or not (based on your validator).
If you are on Windows, you should manually download and install file for Windows and add it to either
Windows PATH environment variable,
project environments (config/environments/development.rb, add line Paperclip.options[:command_path] = '{your_path_to_dir_with_file/bin}' )
After that, restart console, server and it should work.
This is what you can do only for windows :
Install the exe by
download the exe from this link file.exe
test if is well installed by running your cmd and put the following instructions convert logo: logo.miff then run ' imdisplay logo.miff '
you will get custom logo image,that will pop up on your windows screen.
From here now you can start configuring everything on rails app
Open config/environments/development.rb
Add the following line: Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
If your rails server is currently running,brake the server and then run again rails s.After that you should be ready to go.Upload image on your app

Paperclip don't generate styles and original image appears as broken

I having a big issue here. I really tried, but I can't solve this problem by myself, so I hope people can help me here.
Before talk about my problem, I must say I'm using Paperclip and IMGKit in my Project, but I think the problem is with Paperclip.
I create a Rails Task to take snapshots from the home page of some sites. Sometime ago everything is working fine, but now everything goes down. I import my real database from Heroku to localhost (without any images and migrations of paperclip), run the migrations, delete all old files from 'public/system' and run my task again (to take snapshot of all websites).
So, now I have:
The paths and original images are generated, but when I try to load them in View, this just show as a broken image.
Paperclip doesn't generate the path and converted images of :styles.
Sites that don't have image, I can see my default image correctly.
ImageMagick seems to be working, I try convert some images and worked like a charm.
Let's take a look at the code. Assume that I'm running a task that will perform this task to all sites in my database. The sites are called "items" in my architecture.
Development.rb
#config/environments/development.rb
# "which convert" give me this path
Paperclip.options[:command_path] = "/usr/local/bin/"
Item.rb (model)
My task just call "object.save" of every site in the DB, so my code starts on before_save.
has_attached_file :image,
:styles => { :small => "200x147#" },
:convert_options => { :small => "-quality 75 -strip" },
:default_url => '/images/:style/bitcoin-earth.jpg'
before_save :generate_data
def generate_data
self.image = get_image(self.id, self.url_original)
end
# Take snapshot of the website
def get_image(filename, link)
kit = IMGKit.new(link.to_s, :quality => 100, :width => 1024, :height => 768)
file = Tempfile.new(["template_#{filename}", 'png'], 'tmp',
:encoding => 'ascii-8bit')
file.write(kit.to_img(:png))
file.flush
return file
end
View
<%= image_tag store.image.url %>
Gemfile
gem "paperclip"
If I try to run rake paperclip:refresh:missing_styles, the task finish very fast without any error. But if I try to run rake paperclip:refresh CLASS=Item I got:
Image Paperclip::Errors::NotIdentifiedByImageMagickError
And yes, I already search for it and I didn't found a solution for my case.
A little tip?
When I "inspect element" in my project and try to see the source of the item image, I saw:
http://localhost:3000/public/system/items/images/000/000/216/original/template_21620140109-14507-1c0yszzpng?1389305824
But if I go to my project folder, I just see a image called template_21620140109-21209-1yls03opng. Note that doesn't exist any "?1389305824" there. See the image above.
Well, I think that's it. What can be the problem? I really need solve this issue, please, help me :/
[ Edited on Jan 10, 2013 ]
Item.rb (model):
before_save :generate_data
def generate_data
file = File.open(get_image(self.id, self.url_original))
self.image = file
file.close
end
def get_image(filename, link)
kit = IMGKit.new(link.to_s, :quality => 100,
:width => 1024, :height => 768)
file = Tempfile.new(["template_#{filename}", '.png'], 'tmp',
:encoding => 'ascii-8bit')
file.write(kit.to_img(:png))
file.flush
return file
end
Now I don't have any error on console while getting images and saving on DB, but Paperclip still don't generate my :styles. When I go to log/development.log, I can see this error, but I don't know what I can do to solve:
Command :: file -b --mime 'tmp/template_24320140110-17577-80zj1c.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/template_24320140110-17577-80zj1c20140110-17577-mqa2q3.png[0]'
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
I think we're getting closer, please, keep helping me :)
I think your problem is here:
template_21620140109-14507-1c0yszzpng?1389305824 #-> should have .png (not a valid image)
Image
This might not be the problem, but maybe you could streamline your method to exclude the temporary file:
# Take snapshot of the website
def get_image(filename, link)
kit = IMGKit.new(link.to_s, :quality => 100, :width => 1024, :height => 768)
file = kit.to_file("system/temp/template_#{filename}")
return file
end
I think the issue is that ImageMagick is not being passed a "real" file, and consequently you're getting the unrecognized image issues

Paperclip::NotIdentifiedByImageMagickError image is not recognized by the 'identify' command

i´m getting this error when editing a model specifically when i delete an image associated to it and I select another:
Paperclip::NotIdentifiedByImageMagickError in Admin/packsController#update
Chrysanthemumprueba4.jpg is not recognized by the 'identify' command.
C:/Users/.../vendor/plugins/thoughtbot-paperclip-fc792c8/lib/paperclip/geometry.rb:24:in `from_file'
But when i create a new pack and I select images for it, it works ok.
I have two tables: packs and pack_images,and pack_images has the photos for the pack associated, here are the relations:
class Pack < ActiveRecord::Base
has_many :pack_images, :dependent => :destroy
end
class PackImage < ActiveRecord::Base
belongs_to :pack
attr_accessor :height, :width
has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => {:principal => "240x240>", :original => "400x400>", ...}
end
This is the controller's action that throws me the error:
def update
#pack = Pack.find(params[:id])
#pack.pack_products
unless params[:pack][:pack_images_attributes].nil?
params[:pack][:pack_images_attributes].count.times do |i|
unless params[:pack][:pack_images_attributes][:"#{i.to_s}"][:photo].blank?
file = params[:pack][:pack_images_attributes][:"#{i.to_s}"][:photo]
dimensions = Paperclip::Geometry.from_file(file)
#pack.pack_images[i].width = dimensions.width
#pack.pack_images[i].height = dimensions.height
end
end
end
respond_to do |format|
#pack.update_attributes(params[:pack])
format.html { redirect_to(admin_pack_path(#pack.id), :notice => 'Pack updated') }
end
I noticed that, when updating I get less parameters (only the photo's name) than when creating (photo's name,file type,width,height,etc).
I hope you can help me
Thank you very much
it used to work fine for pdf and images, tried out for an hour or so, followed everything I googled later the problem was found in my model has_attached_file :attachment,
:styles => {:original=> "125x125#"}
had to comment this line, and it worked for other attachments like docx or odt etc..
so in your case :styles => {:principal => "240x240>", :original => "400x400>"}
check out and comment.
Locate the path of the identify command like this:
$ which identify
For me the above command prints this: /usr/local/bin/identify
Add this in some initializer file:
Paperclip::Attachment.default_options[:command_path] = "/usr/local/bin"
One reason this error occurs is when you try to determine the dimensions of an image that does not exist:
Paperclip::Geometry.from_file(nil)
This command will hang and cause this error.
Try to run the "identify" command from ImageMagick on this image. It seems something with your ImageMagick install.
I had the exact same issue. Windows 8 64bit, Rails 4, ImageMagick-6.8.7-1-Q16-x64-static.exe. Do this :
In the root of your rails app (from Git Bash)
$ which identify
/c/Program Files/ImageMagick-6.8.7-Q16/./identify
Then
$ cd "/c/Program Files/ImageMagick-6.8.7-Q16"
Don't forget the quotes. Copy all executables to you /bin directory. I actually copied all these files to be certain.
$ cp * /bin
And voila paperclip works!

Resources