gpt4 book ai didi

javascript - Rails jcrop + 回形针

转载 作者:行者123 更新时间:2023-11-28 02:58:33 24 4
gpt4 key购买 nike

我已经成功实现了 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 一次性上传所有文件,然后希望能够单击图像旁边的裁剪链接。

最佳答案

您需要进行与此类似的更改:

在 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}

在 photos/show.html.erb

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

关于javascript - Rails jcrop + 回形针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1915879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com