avatar_crop.html 4.7 KB

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