crop_avatar.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. {% extends "misago/usercp/base.html" %}
  2. {% load i18n staticfiles %}
  3. {% block title %}
  4. {% trans "Crop avatar" %}{{ block.super }}
  5. {% endblock title %}
  6. {% block page %}
  7. <div class="form-panel">
  8. <form method="POST" role="form" class="upload-form">
  9. <input type="hidden" id="crop" value="" name="crop">
  10. {% csrf_token %}
  11. <div class="form-header">
  12. <h2>
  13. <span class="fa fa-arrows-alt"></span>
  14. {% trans "Crop avatar" %}
  15. </h2>
  16. </div>
  17. <div class="form-body form-crop-avatar">
  18. <div class="crop-form-container">
  19. <div class="cropped-image-border">
  20. <img id="crop-image" src="{{ avatar_url }}" alt="{% trans "Source image" %}">
  21. </div>
  22. {% if crop %}
  23. <button id="btn-crop" class="btn btn-primary">{% trans "Save changes" %}</button>
  24. {% else %}
  25. <button id="btn-crop" class="btn btn-primary">{% trans "Set avatar" %}</button>
  26. {% endif %}
  27. </div>
  28. </div>
  29. <div class="form-footer">
  30. <a href="{% url 'misago:usercp_change_avatar' %}" class="btn btn-default">{% trans "Cancel" %}</a>
  31. </div>
  32. </form>
  33. </div>
  34. {% endblock page %}
  35. {% block javascripts %}
  36. <script type="text/javascript" src="{% static "misago/js/jquery.color.js" %}" charset="utf-8"></script>
  37. <script type="text/javascript" src="{% static "misago/js/jquery.Jcrop.js" %}" charset="utf-8"></script>
  38. <script type="text/javascript">
  39. $(function() {
  40. function registerCrop($image) {
  41. var max_height = $(window).height() * .6;
  42. var width = $image.width();
  43. var height = $image.height();
  44. if (height > max_height) {
  45. $image.css('width', $image.width() / ($image.height() / max_height))
  46. $image.css('height', max_height);
  47. var width = $image.width();
  48. var height = $image.height();
  49. }
  50. $image.parent().parent().width(width + 8);
  51. var width = $image.width();
  52. var height = $image.height();
  53. {% if crop %}
  54. original_width = {{ crop.image_width }};
  55. change_ratio = width / original_width;
  56. selection_len = change_ratio * {{ crop.selection_len }};
  57. start_x = change_ratio * {{ crop.start_x }};
  58. start_y = change_ratio * {{ crop.start_y }};
  59. {% else %}
  60. if (width < height) {
  61. selection_len = width;
  62. } else {
  63. selection_len = height;
  64. }
  65. start_x = (width - selection_len) / 2;
  66. start_y = (height - selection_len) / 2;
  67. {% endif %}
  68. var $btn = $('#btn-crop');
  69. var $input = $('#crop');
  70. function updateValue(c) {
  71. var crop = [width, height, c.h, c.w, c.x, c.x2, c.y, c.y2];
  72. $input.val(crop.join(','));
  73. $btn.removeAttr('disabled');
  74. }
  75. $image.Jcrop({
  76. aspectRatio: 1,
  77. minSize: [40, 40],
  78. bgColor: '#fff',
  79. bgOpacity: 0.25,
  80. setSelect: [start_x, start_y, start_x + selection_len, start_y + selection_len],
  81. onSelect: updateValue,
  82. onChange: updateValue,
  83. onRelease: function() {$btn.attr('disabled', 'disabled');}
  84. });
  85. }
  86. var interval_id = setInterval(function() {
  87. var $image = $("#crop-image");
  88. if ($image.width() && $image.height()) {
  89. registerCrop($image);
  90. clearInterval(interval_id);
  91. }
  92. }, 300);
  93. });
  94. </script>
  95. {% endblock javascripts %}