我尝试使用给定here的代码来使用Paperclip上传和裁剪图像。我能够上传图像并且图像也被裁剪但没有选择区域。控制台出现以下错误:
[paperclip]确定内容类型时出错:Cocaine :: CommandNotFoundError
我已尝试过针对this和this等其他文章的解决方案,但没有一个解决了这个问题。
欢迎任何其他建议。
注意:我正在使用Windows 7。 安装的宝石是:
gem'trail','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 %>
配置/ development.rb
Paperclip.options[:swallow_stderr] = false
Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.8-Q8/"
答案 0 :(得分:1)
这很可能是因为Paperclip 4使用file
命令来确定文件的内容类型。该命令在普通Windows机器上不可用。
下载file
命令的Win32版本并将其放在搜索路径中的某个位置。 (我正在使用gnuwin32)或者您可以使用3.x版本的Paperclip。