avatar_crop.html 3.2 KB

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