【问题标题】:rails jcrop + papercliprails jcrop + 回形针
【发布时间】:2023-04-01 06:26:01
【问题描述】:

我已经成功实现了 jcrop 和回形针,通过转到另一个页面(即crop.html.erb)来裁剪图像。但我希望能够在当前页面上进行所有裁剪,在弹出的 div / 对话框中上传图像。所以不知何故,我需要在点击时将crop.html.erb 页面加载到弹出 div 中。这是裁剪页面上的代码

    <% content_for :javascript do %>
    <%= stylesheet_link_tag "jquery.Jcrop" %>
    <%= javascript_include_tag "jquery.Jcrop.min" %>

<% end %>

<script type="text/javascript" charset="utf-8">

$(function() {
  $('#cropbox').Jcrop({
    onChange: update_crop,
    onSelect: update_crop
  });
});


function update_crop(coords) {
  var rx = 100/coords.w;
  var ry = 100/coords.h;
  $('#preview').css({
    width: Math.round(rx * <%= @photo.photo_geometry(:biggest).width %>) + 'px',
    height: Math.round(ry * <%= @photo.photo_geometry(:biggest).height %>) + 'px',
    marginLeft: '-' + Math.round(rx * coords.x) + 'px',
    marginTop: '-' + Math.round(ry * coords.y) + 'px'
  });
  var ratio = <%= @photo.photo_geometry(:original).width %> / <%= @photo.photo_geometry(:biggest).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>


 <%= image_tag @photo.photo.url(:biggest), :id => "cropbox" %>

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


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

我可以将它附加到一个 div 或类似的东西上,还是我很远?

顺便说一句,我不只是一次上传一张图片,所以我不能让它进入裁剪页面然后回来。我在页面上的弹出 div 中使用 uploadify 一次上传所有文件,然后希望能够单击图像旁边的裁剪链接。

【问题讨论】:

标签: javascript jquery ruby-on-rails ajax


【解决方案1】:

您需要进行类似这样的更改:

在 photos_controller.rb 中

def create
  @photo = Photo.new(params[:photo])
  if @photo.save
    flash[:notice] = "Successfully created photo."
    redirect_to @photo
  end
end

def update
  @photo = Photo.find(params[:id])
  if @photo.update_attributes(params[:photo])
      flash[:notice] = "Successfully updated photo."
      redirect_to @photo
  end
end

def crop
  @photo = Photo.find(params[:id])
end

在 routes.rb 中

map.resources :photos, :member => {:crop => :get}

在照片/show.html.erb中

<%= link_to "Crop", crop_photo_path(@photo) %>

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 2016-11-19
    相关资源
    最近更新 更多