@@ -20,32 +20,7 @@ $(function () {
2020 var actionsNode = $ ( '#actions' )
2121 var currentFile
2222 var coordinates
23- function replaceResults ( img ) {
24- var content
25- if ( ! ( img . src || img instanceof HTMLCanvasElement ) ) {
26- content = $ ( '<span>Loading image file failed</span>' )
27- } else {
28- content = $ ( '<a target="_blank">' ) . append ( img )
29- . attr ( 'download' , currentFile . name )
30- . attr ( 'href' , img . src || img . toDataURL ( ) )
31- }
32- result . children ( ) . replaceWith ( content )
33- if ( img . getContext ) {
34- actionsNode . show ( )
35- }
36- }
37- function displayImage ( file , options ) {
38- currentFile = file
39- if ( ! loadImage (
40- file ,
41- replaceResults ,
42- options
43- ) ) {
44- result . children ( ) . replaceWith (
45- $ ( '<span>Your browser does not support the URL or FileReader API.</span>' )
46- )
47- }
48- }
23+
4924 function displayExifData ( exif ) {
5025 var thumbnail = exif . get ( 'Thumbnail' )
5126 var tags = exif . getAll ( )
@@ -70,6 +45,40 @@ $(function () {
7045 }
7146 exifNode . show ( )
7247 }
48+
49+ function updateResults ( img , data ) {
50+ var content
51+ if ( ! ( img . src || img instanceof HTMLCanvasElement ) ) {
52+ content = $ ( '<span>Loading image file failed</span>' )
53+ } else {
54+ content = $ ( '<a target="_blank">' ) . append ( img )
55+ . attr ( 'download' , currentFile . name )
56+ . attr ( 'href' , img . src || img . toDataURL ( ) )
57+ }
58+ result . children ( ) . replaceWith ( content )
59+ if ( img . getContext ) {
60+ actionsNode . show ( )
61+ }
62+ if ( data && data . exif ) {
63+ displayExifData ( data . exif )
64+ }
65+ }
66+
67+ function displayImage ( file , options ) {
68+ currentFile = file
69+ if ( ! loadImage (
70+ file ,
71+ updateResults ,
72+ options
73+ ) ) {
74+ result . children ( ) . replaceWith (
75+ $ ( '<span>' +
76+ 'Your browser does not support the URL or FileReader API.' +
77+ '</span>' )
78+ )
79+ }
80+ }
81+
7382 function dropChangeHandler ( e ) {
7483 e . preventDefault ( )
7584 e = e . originalEvent
@@ -79,71 +88,75 @@ $(function () {
7988 maxWidth : result . width ( ) ,
8089 canvas : true ,
8190 pixelRatio : window . devicePixelRatio ,
82- downsamplingRatio : 0.5
91+ downsamplingRatio : 0.5 ,
92+ orientation : true
8393 }
8494 if ( ! file ) {
8595 return
8696 }
8797 exifNode . hide ( )
8898 thumbNode . hide ( )
89- loadImage . parseMetaData ( file , function ( data ) {
90- if ( data . exif ) {
91- options . orientation = data . exif . get ( 'Orientation' )
92- displayExifData ( data . exif )
93- }
94- displayImage ( file , options )
95- } )
99+ displayImage ( file , options )
96100 }
101+
97102 // Hide URL/FileReader API requirement message in capable browsers:
98- if ( window . createObjectURL || window . URL || window . webkitURL || window . FileReader ) {
103+ if ( window . createObjectURL || window . URL || window . webkitURL ||
104+ window . FileReader ) {
99105 result . children ( ) . hide ( )
100106 }
107+
101108 $ ( document )
102109 . on ( 'dragover' , function ( e ) {
103110 e . preventDefault ( )
104111 e = e . originalEvent
105112 e . dataTransfer . dropEffect = 'copy'
106113 } )
107114 . on ( 'drop' , dropChangeHandler )
108- $ ( '#file-input' ) . on ( 'change' , dropChangeHandler )
109- $ ( '#edit' ) . 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 ( ) {
115+
116+ $ ( '#file-input' )
117+ . on ( 'change' , dropChangeHandler )
118+
119+ $ ( '#edit' )
120+ . on ( 'click' , function ( event ) {
121+ event . preventDefault ( )
122+ var imgNode = result . find ( 'img, canvas' )
123+ var img = imgNode [ 0 ]
124+ var pixelRatio = window . devicePixelRatio || 1
125+ imgNode . Jcrop ( {
126+ setSelect : [
127+ 40 ,
128+ 40 ,
129+ ( img . width / pixelRatio ) - 40 ,
130+ ( img . height / pixelRatio ) - 40
131+ ] ,
132+ onSelect : function ( coords ) {
133+ coordinates = coords
134+ } ,
135+ onRelease : function ( ) {
136+ coordinates = null
137+ }
138+ } ) . parent ( ) . on ( 'click' , function ( event ) {
139+ event . preventDefault ( )
140+ } )
141+ } )
142+
143+ $ ( '#crop' )
144+ . on ( 'click' , function ( event ) {
145+ event . preventDefault ( )
146+ var img = result . find ( 'img, canvas' ) [ 0 ]
147+ var pixelRatio = window . devicePixelRatio || 1
148+ if ( img && coordinates ) {
149+ updateResults ( loadImage . scale ( img , {
150+ left : coordinates . x * pixelRatio ,
151+ top : coordinates . y * pixelRatio ,
152+ sourceWidth : coordinates . w * pixelRatio ,
153+ sourceHeight : coordinates . h * pixelRatio ,
154+ minWidth : result . width ( ) ,
155+ maxWidth : result . width ( ) ,
156+ pixelRatio : pixelRatio ,
157+ downsamplingRatio : 0.5
158+ } ) )
125159 coordinates = null
126160 }
127- } ) . parent ( ) . on ( 'click' , function ( event ) {
128- event . preventDefault ( )
129161 } )
130- } )
131- $ ( '#crop' ) . on ( 'click' , function ( event ) {
132- event . preventDefault ( )
133- var img = result . find ( 'img, canvas' ) [ 0 ]
134- var pixelRatio = window . devicePixelRatio || 1
135- if ( img && coordinates ) {
136- replaceResults ( loadImage . scale ( img , {
137- left : coordinates . x * pixelRatio ,
138- top : coordinates . y * pixelRatio ,
139- sourceWidth : coordinates . w * pixelRatio ,
140- sourceHeight : coordinates . h * pixelRatio ,
141- minWidth : result . width ( ) ,
142- maxWidth : result . width ( ) ,
143- pixelRatio : pixelRatio ,
144- downsamplingRatio : 0.5
145- } ) )
146- coordinates = null
147- }
148- } )
149162} )
0 commit comments