|
| 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 | + |
| 12 | +/*global window, document, loadImage, HTMLCanvasElement, $ */ |
| 13 | + |
| 14 | +$(function () { |
| 15 | + 'use strict'; |
| 16 | + |
| 17 | + var result = $('#result'), |
| 18 | + currentFile, |
| 19 | + replaceResults = function (img) { |
| 20 | + if ((img.src || img instanceof HTMLCanvasElement)) { |
| 21 | + result.children().replaceWith(img); |
| 22 | + } |
| 23 | + }, |
| 24 | + |
| 25 | + showFirst = function (img) { |
| 26 | + if ((img.src || img instanceof HTMLCanvasElement)) { |
| 27 | + result.children().replaceWith(img); |
| 28 | + } |
| 29 | + crop(); |
| 30 | + }, |
| 31 | + |
| 32 | + displayImage = function (file, options) { |
| 33 | + currentFile = file; |
| 34 | + loadImage( |
| 35 | + file, |
| 36 | + showFirst, |
| 37 | + options |
| 38 | + ); |
| 39 | + }, |
| 40 | + dropChangeHandler = function (e) { |
| 41 | + e.preventDefault(); |
| 42 | + e = e.originalEvent; |
| 43 | + var target = e.dataTransfer || e.target, |
| 44 | + file = target && target.files && target.files[0], |
| 45 | + options = { |
| 46 | + maxWidth: result.width(), |
| 47 | + orientation: 2 |
| 48 | + // canvas: true |
| 49 | + }; |
| 50 | + if (!file) { |
| 51 | + return; |
| 52 | + } |
| 53 | + loadImage.parseMetaData(file, function (data) { |
| 54 | + if (data.exif) { |
| 55 | + options.orientation = data.exif.get('Orientation'); |
| 56 | + } |
| 57 | + displayImage(file, options); |
| 58 | + // crop(); |
| 59 | + }); |
| 60 | + }; |
| 61 | + |
| 62 | + // Hide URL/FileReader API requirement message in capable browsers: |
| 63 | + if (window.createObjectURL || window.URL || window.webkitURL || window.FileReader) { |
| 64 | + result.children().hide(); |
| 65 | + } |
| 66 | + $('#file-input').on('change', dropChangeHandler); |
| 67 | + function crop() { |
| 68 | + var img = result.find('img, canvas')[0]; |
| 69 | + var coordinates = { |
| 70 | + x: 500, |
| 71 | + y: 500, |
| 72 | + w: 512, |
| 73 | + h: 512 |
| 74 | + }; |
| 75 | + |
| 76 | + if (img && coordinates) { |
| 77 | + replaceResults(loadImage.scale(img, { |
| 78 | + left: coordinates.x, |
| 79 | + top: coordinates.y, |
| 80 | + sourceWidth: coordinates.w, |
| 81 | + sourceHeight: coordinates.h, |
| 82 | + minWidth: result.width() |
| 83 | + })); |
| 84 | + } |
| 85 | + } |
| 86 | +}); |
0 commit comments