demo.js 4.0 KB

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