Skip to content

Instantly share code, notes, and snippets.

@patsanch
Created May 6, 2011 07:51
Show Gist options
  • Save patsanch/958580 to your computer and use it in GitHub Desktop.
Save patsanch/958580 to your computer and use it in GitHub Desktop.
Cropper View
<% content_for(:head) do %>
<%= stylesheet_link_tag "jquery.Jcrop" %>
<%= 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, 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_profile.avatar_geometry(:large).width %>) + 'px',
height: Math.round(ry * <%= @user_profile.avatar_geometry(:large).height %>) + 'px',
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
marginTop: '-' + Math.round(ry * coords.y) + 'px'
});
var ratio = <%= @user_profile.avatar_geometry(:original).width %> / <%= @user_profile.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_profile.avatar.url(:large), :id => "cropbox" %>
<h4>Preview:</h4>
<div style="width:100px; height:100px; overflow:hidden">
<%= image_tag @user_profile.avatar.url(:large), :id => "preview" %>
</div>
<% form_for @user_profile 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 %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment