avatar_crop.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. {% if avatar_crop -%}
  47. setSelect: [{{ avatar_crop[0] }}, {{ avatar_crop[1] }}, {{ avatar_crop[0] + avatar_crop[2] }}, {{ avatar_crop[1] + avatar_crop[2] }}]
  48. {%- else -%}
  49. setSelect: [0, 0, {{ avatar_size }}, {{ avatar_size }}],
  50. {%- endif %}
  51. },function(){
  52. // Use the API to get the real image size
  53. var bounds = this.getBounds();
  54. boundx = bounds[0];
  55. boundy = bounds[1];
  56. $('#crop-b').val(boundx);
  57. // Store the API in the jcrop_api variable
  58. jcrop_api = this;
  59. });
  60. function updatePreview(c)
  61. {
  62. if (parseInt(c.w) > 0)
  63. {
  64. var rx = {{ avatar_size }} / c.w;
  65. var ry = {{ avatar_size }} / c.h;
  66. $(crop_w).val(c.w);
  67. $(crop_x).val(c.x);
  68. $(crop_y).val(c.y);
  69. $('#preview').css({
  70. width: Math.round(rx * boundx * afx) + 'px',
  71. height: Math.round(ry * boundy * afx) + 'px',
  72. marginLeft: '-' + Math.round(rx * c.x * afx) + 'px',
  73. marginTop: '-' + Math.round(ry * c.y * afx) + 'px'
  74. });
  75. }
  76. };
  77. });
  78. </script>
  79. {% endblock %}