paperclip Error while determining content type: Co

2019-07-25 00:09发布

问题:

I tried to use the code given here to upload and crop images using Paperclip.I am able to upload image and also the image is cropped but without selection area. The console gives the following error :

[paperclip] Error while determining content type: Cocaine::CommandNotFoundError

I have tried solutions given on other articles of SO like this and this, but none of them solved this problem.

Any other suggestions are most welcomed.

NOTE : I am working on Windows 7. Gems installed are :

gem 'rails', '3.2.1'

gem 'mysql2'

gem 'paperclip'

gem 'mini_magick'

User.rb

require 'mini_magick'
class User < ActiveRecord::Base
#Paperclip.options[:command_path] = "C:\Program Files\ImageMagick-6.8.8-Q8"
#  The foll line works
#  has_attached_file :avatar,:styles => {:thumb => "75x75#",:small => "150x150>"} 

  attr_accessible :name,:email,:avatar,:crop_x,:crop_y,:crop_w,:crop_h
  has_attached_file :avatar,:styles => {:thumb => "75x75>",:small=>"100x100#", :large=>"500x500>"}#,:processors => [:cropper]
  attr_accessor :processing, :crop_x,:crop_y,:crop_w,:crop_h
  after_update :reprocess_avatar,:if=>:cropping?                       

  def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
  end

  def avatar_geometry(style = :original)
    @geometry ||= {}
    @geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
  end

  private

  def reprocess_avatar
    # don't crop if the user isn't updating the photo
    # ...or if the photo is already being processed
     return unless (cropping? && !processing)
     self.processing = true
     avatar.reprocess!
     self.processing = false
  end   

end

users_controller.rb

 def create
  @user = User.new(params[:user])
  if @user.save
    if params[:user][:avatar].blank?
      flash[:notice] = "Successfully created user."
      redirect_to @user
    else
      render :action => "crop"
    end
  else
    render :action => 'new'
  end
end


  # PUT /users/1
  # PUT /users/1.json
  def update
    @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    if params[:user][:avatar].blank?
      flash[:notice] = "Successfully updated user."
      redirect_to @user
    else
      render :action => "crop"
    end
  else
    render :action => 'edit'
  end

 end

crop.html.erb

<% content_for(:head) do %>

<link rel="stylesheet" href="../stylesheets/jquery.Jcrop.css" type="text/css" />
<link rel="stylesheet" href="../stylesheets/jquery.Jcrop.min.css" type="text/css" />
<script src="../javascripts/jquery.min.js"></script>
<script src="../javascripts/jquery.Jcrop.js"></script>

<script type="text/javascript " charset="utf-8">
$(function() {
  $('#cropbox').Jcrop({
    onChange: update_crop,
    onSelect: update_crop,
    setSelect: [0, 0, 500, 500],
    aspectRatio: 1
  });
});

function update_crop(coords) {
  var rx = 100/coords.w;
  var ry = 100/coords.h;
  $('#preview').css({
    width: Math.round(rx * <%= @user.avatar_geometry(:large).width %>) + 'px',
    height: Math.round(ry * <%= @user.avatar_geometry(:large).height %>) + 'px',
    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
    marginTop: '-' + Math.round(ry * coords.y) + 'px'
  });
  var ratio = <%= @user.avatar_geometry(:original).width %> / <%= @user.avatar_geometry(:large).width %>;
  $("#crop_x").val(Math.round(coords.x * ratio));
  $("#crop_y").val(Math.round(coords.y * ratio));
  $("#crop_w").val(Math.round(coords.w * ratio));
  $("#crop_h").val(Math.round(coords.h * ratio));
}
</script>
<% end %>

<%= image_tag @user.avatar.url(:large), :id => "cropbox" %>

<h4>Preview:</h4>
<div style="width:100px; height:100px; overflow:hidden">
  <%= image_tag @user.avatar.url(:large), :id => "preview" %>
</div>

<% form_for @user do |f| %>
  <% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
    <%= f.hidden_field attribute, :id => attribute %>
  <% end %>
  <p><%= f.submit "Crop" %></p>
<% end %>

config/development.rb

 Paperclip.options[:swallow_stderr] = false
  Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.8-Q8/"

回答1:

This is most likely because Paperclip 4 uses the file command to determine the content type of a file. That command is not available on a normal Windows machine.

Either download a Win32 version of the file command and put it somewhere in the search path. (I'm using gnuwin32) Alternatively you can use the 3.x version of Paperclip.