Ruby on rails 使用回形针、ImageMagick、Jcrop和S3存储进行裁剪:为什么赢得&x27;t';图像,再处理';再加工?

Ruby on rails 使用回形针、ImageMagick、Jcrop和S3存储进行裁剪:为什么赢得&x27;t';图像,再处理';再加工?,ruby-on-rails,amazon-s3,imagemagick,paperclip,jcrop,Ruby On Rails,Amazon S3,Imagemagick,Paperclip,Jcrop,我(松散地)遵循,它使用回形针、ImageMagick和Jcrop来允许自定义裁剪上传的图像 由于我正在使用AmazonS3进行文件存储,因此我不得不重新整理教程中的部分内容以适应需要。一切似乎都运行得很好,除了我的图像的裁剪版本没有被重新处理(或者重新处理的结果没有重新上传到S3)——因此在裁剪过程之后,我只剩下最初上传的相同图像(对于我为每个图像存储的所有图像大小都是如此) 以下是我的功能(如功能图中所示)模型: class功能:裁剪? 如果Rails.env==“生产” S3_凭证={:a

我(松散地)遵循,它使用回形针、ImageMagick和Jcrop来允许自定义裁剪上传的图像

由于我正在使用AmazonS3进行文件存储,因此我不得不重新整理教程中的部分内容以适应需要。一切似乎都运行得很好,除了我的图像的裁剪版本没有被重新处理(或者重新处理的结果没有重新上传到S3)——因此在裁剪过程之后,我只剩下最初上传的相同图像(对于我为每个图像存储的所有图像大小都是如此)

以下是我的功能(如功能图中所示)模型:

class功能:裁剪?
如果Rails.env==“生产”
S3_凭证={:access_key_id=>'',
:secret\u access\u key=>“”,
:bucket=>“”
其他的
S3_凭证={:access_key_id=>'',
:secret\u access\u key=>“”,
:bucket=>“”
结束
已附加文件:图像,
:style=>{:small=>“240x135>”,:crappable=>“960x960>”,:display=>“960x540>”,
:处理器=>[:裁剪器],
:storage=>:s3,
:s3\u凭证=>s3\u凭证,
:path=>“功能/:id/:样式:扩展”
验证附件内容类型:image,:content\u type=>['image/jpeg','image/gif','image/png',
'图像/pjpeg','图像/x-png'],
:message=>'必须是JPEG、GIF或PNG图像'
def裁剪?
!作物×空白?&&!裁剪y.blank?&&!作物w.blank?&!作物h.blank?
结束
def图像_几何体(样式=:原始)
@几何体| |={}
路径=(image.options[:storage]=:s3)?image.url(样式):image.path(样式)
@几何体[样式]| |=回形针::几何体。从_文件(路径)
结束
私有的
def再处理图像
图像,再处理!
结束
结束
这是我的“cropper.rb”(回形针处理器):

模块回形针
类裁剪器<缩略图
def转换命令
如果是命令
crop_命令+super.sub(/-crop\S+/,“”)
其他的
超级的
结束
结束
def crop_命令
target=@attachment.instance
如果目标是裁剪?
“-crop'#{target.crop#w}x#{target.crop#h}+#{target.crop#x}+#{target.crop#y}”
结束
结束
结束
结束
my Features控制器的相关操作:

class FeaturesController < ApplicationController

  def new
    @feature = Feature.new
  end

  def create
    @feature = Feature.new(params[:feature])
    if @feature.save
      if params[:feature][:image].blank?
        flash[:notice] = "New feature added!"
        redirect_to @feature
      else
        render :crop
      end
    else
      @title = "Add a New Feature"
      render :new
    end
  end

  def edit
    @feature = Feature.find(params[:id])
    @title = "Edit #{@feature.headline}"
  end

  def update
    @feature = Feature.find(params[:id])
    if @feature.update_attributes(params[:feature])
      if params[:feature][:image].blank?
        flash[:notice] = "Feature updated!"
        redirect_to @feature
      else
        render :crop
      end
    else
      @title = "Edit Feature"
      render :edit
    end
  end
end
类功能控制器
以及我的“crop.html.erb”视图的相关行:

<% content_for :javascript_includes do %>
    <%= javascript_include_tag 'jquery.Jcrop.min' %>
    <script type="text/javascript" charset="utf-8">
    $(function() {
        $('#cropbox').Jcrop({
            onChange: update_crop,
            onSelect: update_crop,
            setSelect: [0, 0, 960, 540],
            aspectRatio: 960/540
        });
    });

    function update_crop(coords) {
        var ratio = <%= @feature.image_geometry(:original).width %> / <%= @feature.image_geometry(:croppable).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 %>
<% content_for :style_includes do %>
    <%= stylesheet_link_tag 'jquery.Jcrop', :media => 'screen' %>
<% end %>

<%= image_tag @feature.image.url(:croppable), :id => "cropbox" %>

<% form_for @feature 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 %>

$(函数(){
$('#cropbox').Jcrop({
onChange:更新作物,
onSelect:更新作物,
setSelect:[0,0,960,540],
方面:960/540
});
});
功能更新\u裁剪(coords){
风险值比率=/;
$(“#crop_x”).val(数学整数(coords.x*比率));
$(“#crop_y”).val(数学圆(coords.y*比率));
$(“#crop_w”).val(数学圆整(coords.w*ratio));
$(“#crop_h”).val(Math.round(coords.h*ratio));
};
'屏幕'>
“cropbox”%>
属性%>

问题不在于自定义裁剪(偏移、裁剪区域等)有错误,而是当我单击“裁剪”时,没有裁剪发生——我只剩下从原始上传/处理中获得的图像。它似乎不是“图像。重新处理!”正在发生(或者重新处理的结果没有保存到S3)

为什么会这样,我能做些什么呢?

好的,让我试着帮你:)

首先,你可以不包括你的回形针处理器在模型中,让回形针处理它

其次,删除
:after\u update
并替换为
:before\u update
,这应该设置处理器的
图像。选项[:crop]
。在处理器中,尝试以下操作:

def initialize file, options = {}, attachment = nil
  super
  #......

  @crop = options[ :crop ]

  #......
第三,在处理器中修改
transformation\u命令

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.sub(/ -crop \S+/, '')
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping?
        " -crop '#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}'"
      end
    end
  end
end
def transformation_command
  if @crop
      trans = " -quality 75"
      trans << " -crop \"#{<YOUR_CODE>}\" +repage" if @crop
      trans
  end
end
def转换_命令
如果@crop
trans=“-quality 75”

trans我还使用了同样的RailsCast视频来帮助我进行裁剪。我遇到了一个类似的问题,出现了一个NOSUCHKEY错误,我可以追踪到再处理!更新后调用的调用。我能够通过使用维什的答案并修改它来解决这个问题。我的解决方案可能不是您所需要的,因为我每次都会裁剪

我只是删除了要重新处理的after_update调用!把其他的东西都放在原处。这导致使用传入的处理器(在您的情况下为裁剪器)而不是默认处理器,并在将图像上载到S3之前发生。因为你在第一次上传之前就裁剪了文件,所以一切正常

如果你想
def transformation_command
  if @crop
      trans = " -quality 75"
      trans << " -crop \"#{<YOUR_CODE>}\" +repage" if @crop
      trans
  end
end