demo.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * JavaScript Load Image Demo JS
  3. * https://github.com/blueimp/JavaScript-Load-Image
  4. *
  5. * Copyright 2013, Sebastian Tschan
  6. * https://blueimp.net
  7. *
  8. * Licensed under the MIT license:
  9. * https://opensource.org/licenses/MIT
  10. */
  11. /* global loadImage, HTMLCanvasElement, $ */
  12. $(function () {
  13. 'use strict'
  14. var result = $('#result')
  15. var exifNode = $('#exif')
  16. var thumbNode = $('#thumbnail')
  17. var actionsNode = $('#actions')
  18. var currentFile
  19. var coordinates
  20. function displayExifData (exif) {
  21. var thumbnail = exif.get('Thumbnail')
  22. var tags = exif.getAll()
  23. var table = exifNode.find('table').empty()
  24. var row = $('<tr></tr>')
  25. var cell = $('<td></td>')
  26. var prop
  27. if (thumbnail) {
  28. thumbNode.empty()
  29. loadImage(thumbnail, function (img) {
  30. thumbNode.append(img).show()
  31. }, {orientation: exif.get('Orientation')})
  32. }
  33. for (prop in tags) {
  34. if (tags.hasOwnProperty(prop)) {
  35. table.append(
  36. row.clone()
  37. .append(cell.clone().text(prop))
  38. .append(cell.clone().text(tags[prop]))
  39. )
  40. }
  41. }
  42. exifNode.show()
  43. }
  44. function updateResults (img, data) {
  45. var content
  46. if (!(img.src || img instanceof HTMLCanvasElement)) {
  47. content = $('<span>Loading image file failed</span>')
  48. } else {
  49. content = $('<a target="_blank">').append(img)
  50. .attr('download', currentFile.name)
  51. .attr('href', img.src || img.toDataURL())
  52. }
  53. result.children().replaceWith(content)
  54. if (img.getContext) {
  55. actionsNode.show()
  56. }
  57. if (data && data.exif) {
  58. displayExifData(data.exif)
  59. }
  60. }
  61. function displayImage (file, options) {
  62. currentFile = file
  63. if (!loadImage(
  64. file,
  65. updateResults,
  66. options
  67. )) {
  68. result.children().replaceWith(
  69. $('<span>' +
  70. 'Your browser does not support the URL or FileReader API.' +
  71. '</span>')
  72. )
  73. }
  74. }
  75. function dropChangeHandler (e) {
  76. e.preventDefault()
  77. e = e.originalEvent
  78. var target = e.dataTransfer || e.target
  79. var file = target && target.files && target.files[0]
  80. var options = {
  81. maxWidth: result.width(),
  82. canvas: true,
  83. pixelRatio: window.devicePixelRatio,
  84. downsamplingRatio: 0.5,
  85. orientation: true
  86. }
  87. if (!file) {
  88. return
  89. }
  90. exifNode.hide()
  91. thumbNode.hide()
  92. displayImage(file, options)
  93. }
  94. // Hide URL/FileReader API requirement message in capable browsers:
  95. if (window.createObjectURL || window.URL || window.webkitURL ||
  96. window.FileReader) {
  97. result.children().hide()
  98. }
  99. $(document)
  100. .on('dragover', function (e) {
  101. e.preventDefault()
  102. e = e.originalEvent
  103. e.dataTransfer.dropEffect = 'copy'
  104. })
  105. .on('drop', dropChangeHandler)
  106. $('#file-input')
  107. .on('change', dropChangeHandler)
  108. $('#edit')
  109. .on('click', function (event) {
  110. event.preventDefault()
  111. var imgNode = result.find('img, canvas')
  112. var img = imgNode[0]
  113. var pixelRatio = window.devicePixelRatio || 1
  114. imgNode.Jcrop({
  115. setSelect: [
  116. 40,
  117. 40,
  118. (img.width / pixelRatio) - 40,
  119. (img.height / pixelRatio) - 40
  120. ],
  121. onSelect: function (coords) {
  122. coordinates = coords
  123. },
  124. onRelease: function () {
  125. coordinates = null
  126. }
  127. }).parent().on('click', function (event) {
  128. event.preventDefault()
  129. })
  130. })
  131. $('#crop')
  132. .on('click', function (event) {
  133. event.preventDefault()
  134. var img = result.find('img, canvas')[0]
  135. var pixelRatio = window.devicePixelRatio || 1
  136. if (img && coordinates) {
  137. updateResults(loadImage.scale(img, {
  138. left: coordinates.x * pixelRatio,
  139. top: coordinates.y * pixelRatio,
  140. sourceWidth: coordinates.w * pixelRatio,
  141. sourceHeight: coordinates.h * pixelRatio,
  142. minWidth: result.width(),
  143. maxWidth: result.width(),
  144. pixelRatio: pixelRatio,
  145. downsamplingRatio: 0.5
  146. }))
  147. coordinates = null
  148. }
  149. })
  150. })