avatar_crop.html 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. {% extends "cranefly/usercp/layout.html" %}
  2. {% import "cranefly/macros.html" as macros with context %}
  3. {% block title %}{{ macros.page_title(title=_('Crop Avatar')) }}{% endblock %}
  4. {% block action %}
  5. <div class="form-container usercp-avatar-crop">
  6. <form action="{% if after_upload %}{{ url('usercp_avatar_upload_crop') }}{% else %}{{ url('usercp_avatar_crop') }}{% endif %}" method="post">
  7. <div class="form-header">
  8. <div class="avatar-crop-preview">
  9. <img src="{{ MEDIA_URL }}{{ source }}" id="preview" alt="{% trans %}Avatar Preview{% endtrans %}" class="jcrop-preview" />
  10. </div>
  11. <h1>{% trans %}Crop Avatar{% endtrans %} <small>{% trans %}Change your Avatar{% endtrans %}</small></h1>
  12. <a href="{{ url('usercp_avatar') }}" class="btn btn-inverse pull-right">{% trans %}Cancel{% endtrans %}</a>
  13. <button name="save" type="submit" class="btn btn-danger pull-right">{% trans %}Crop Avatar{% endtrans %}</button>
  14. </div>
  15. {% if message %}
  16. <div class="messages-list">
  17. {{ macros.draw_message(message) }}
  18. </div>
  19. {% endif %}
  20. <input type="hidden" name="{{ csrf_id }}" value="{{ csrf_token }}">
  21. <input type="hidden" id="crop-b" name="crop_b" value="">
  22. <input type="hidden" id="crop-w" name="crop_w" value="">
  23. <input type="hidden" id="crop-x" name="crop_x" value="">
  24. <input type="hidden" id="crop-y" name="crop_y" value="">
  25. <div class="avatar-crop-target"><img src="{{ MEDIA_URL }}{{ source }}" id="target" alt="{% trans %}Uploaded Image{% endtrans %}"></div>
  26. </form>
  27. </div>
  28. {% endblock %}
  29. {% block javascripts %}
  30. {{ super() }}
  31. <script src="{{ STATIC_URL }}cranefly/js/jquery.Jcrop.min.js"></script>
  32. <script type="text/javascript">
  33. $(function($){
  34. // Create variables (in this scope) to hold the API and image size
  35. var jcrop_api, boundx, boundy;
  36. var crop_b = $('#crop-b');
  37. var crop_w = $('#crop-w');
  38. var crop_x = $('#crop-x');
  39. var crop_y = $('#crop-y');
  40. var afx = $('.avatar-crop-preview').width() / {{ avatar_size }};
  41. $('#target').Jcrop({
  42. onChange: updatePreview,
  43. onSelect: updatePreview,
  44. aspectRatio: 1,
  45. minSize: [50, 50],
  46. setSelect: [ 0, 0, {{ avatar_size }}, {{ avatar_size }} ],
  47. },function(){
  48. // Use the API to get the real image size
  49. var bounds = this.getBounds();
  50. boundx = bounds[0];
  51. boundy = bounds[1];
  52. $('#crop-b').val(boundx);
  53. // Store the API in the jcrop_api variable
  54. jcrop_api = this;
  55. });
  56. function updatePreview(c)
  57. {
  58. if (parseInt(c.w) > 0)
  59. {
  60. var rx = {{ avatar_size }} / c.w;
  61. var ry = {{ avatar_size }} / c.h;
  62. $(crop_w).val(c.w);
  63. $(crop_x).val(c.x);
  64. $(crop_y).val(c.y);
  65. $('#preview').css({
  66. width: Math.round(rx * boundx * afx) + 'px',
  67. height: Math.round(ry * boundy * afx) + 'px',
  68. marginLeft: '-' + Math.round(rx * c.x * afx) + 'px',
  69. marginTop: '-' + Math.round(ry * c.y * afx) + 'px'
  70. });
  71. }
  72. };
  73. });
  74. </script>
  75. {% endblock %}