views:

500

answers:

3

I've been searching on how to solve this issue but could not find any real and working solution.

User model:

validates_attachment_content_type :avatar, :content_type => ['image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png', 'image/gif']

Unnecessary error shows up when invalid file type is uploaded:

Avatar /tmp/sample,23283,0.txt is not recognized by the 'identify' command.

Server details: Ubuntu and Debian

which identify results to /usr/bin/identify

Specifying command path does not solve the issue:

Paperclip.options[:command_path] = "/usr/bin"

Any ideas?

I looked at geometry.rb file and there seems to be something wrong with it.

def self.from_file file
  file = file.path if file.respond_to? "path"
  geometry = begin
               Paperclip.run("identify", %Q[-format "%wx%h" "#{file}"[0]])
             rescue PaperclipCommandLineError
               ""
             end
  parse(geometry) ||
    raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
end

Let me know if anyone has solved the same issue and how. Many thanks.

+1  A: 

You should have imagemagick installed which provides the identify command, and its path has to be in the $PATH variable of the user rails is running as.

It is used by paperclip for the scale and image manipulation options, but it is not documented as dependency because you "can" use paperclip without it if you accept the images as they come.

I hope it helps.

Fer
I added /usr/bin to the path. Still no luck. But I was able to fix the issue by not using <%= error_messages_for :user %> I am using <%@user.messages.each_full{|msg| do something } %>
kgpdeveloper
Did you uploaded that image before the type validation?, may it be a txt file? Does it happens with new files?
Fer
+2  A: 

I've had the same problem - the Paperclip code is not at fault.

In my setup I'm on MacOSX with a binary download of ImageMagick in /usr/local/ImageMagick-6.6.1/bin and I'm running Rails under Apache/Passenger. I've got Paperclip installed as a plugin.

There are 3 steps needed to get this working:

1: Make sure you have ImageMagick working at the UNIX command line level. This involves adding it to your path and exporting these environment variables (pointing to your ImageMagick installation, of course)

MAGICK_HOME=/usr/local/ImageMagick-6.6.1
DYLD_LIBRARY_PATH=/usr/local/ImageMagick-6.6.1/lib

Check that identify works with your images at the command line level.

2: Tell Paperclip where to find the ImageMagick executables In config/environment.rb add this at the bottom of the file

Paperclip.options[:command_path] = "/usr/local/ImageMagick-6.6.1/bin"

At this point, after restarting Passenger, you would see that 'identify' is run from within Paperclip but is not able to identify the file... the final step is...

3: Identify needs those two exported environment variables - and Apache/Passenger (or other web servers probably) does not pass those through by default! In your passenger vhost file add these lines:

  SetEnv MAGICK_HOME /usr/local/ImageMagick-6.6.1
  SetEnv DYLD_LIBRARY_PATH /usr/local/ImageMagick-6.6.1/lib

Restart apache/passenger and it should work

Good Luck!

Rob Jones
kgpdeveloper
This fixed it for me, thanks! Don't forget to restart the server after changing the relevant environment.rb file(s)
gef