@@ -214,7 +214,7 @@ module.exports = baseVw.extend({
214214 saveOnBlur : true ,
215215 placeholder : window . polyglot . t ( 'KeywordsPlaceholder' ) ,
216216 onBeforeTagAdd : ( event , tag ) => {
217- if ( tag . length > self . maxTagChars ) {
217+ if ( tag . length > self . maxTagChars ) {
218218 app . simpleMessageModal . open ( {
219219 title : window . polyglot . t ( 'errorMessages.tagIsTooLongHeadline' ) ,
220220 message : window . polyglot . t ( 'errorMessages.tagIsTooLongBody' , { smart_count : self . maxTagChars } )
@@ -370,6 +370,39 @@ module.exports = baseVw.extend({
370370 this . resizeImage ( ) ;
371371 } ,
372372
373+ getOrientation : function ( file , callback ) {
374+ const reader = new FileReader ( ) ;
375+ reader . onload = function ( e ) {
376+ const dataView = new DataView ( e . target . result ) ; // eslint-disable-line no-undef
377+ let offset = 2 ;
378+
379+ if ( dataView . getUint16 ( 0 , false ) != 0xFFD8 ) return callback ( - 2 ) ;
380+
381+ while ( offset < dataView . byteLength ) {
382+ const marker = dataView . getUint16 ( offset , false ) ;
383+ offset += 2 ;
384+ if ( marker == 0xFFE1 ) {
385+ if ( dataView . getUint32 ( offset += 2 , false ) != 0x45786966 ) return callback ( - 1 ) ;
386+ const little = dataView . getUint16 ( offset += 6 , false ) == 0x4949 ;
387+ offset += dataView . getUint32 ( offset + 4 , little ) ;
388+ const tags = dataView . getUint16 ( offset , little ) ;
389+ offset += 2 ;
390+ for ( var i = 0 ; i < tags ; i ++ ) {
391+ if ( dataView . getUint16 ( offset + ( i * 12 ) , little ) == 0x0112 ) {
392+ return callback ( dataView . getUint16 ( offset + ( i * 12 ) + 8 , little ) ) ;
393+ }
394+ }
395+ } else if ( ( marker & 0xFF00 ) != 0xFF00 ) {
396+ break ;
397+ } else {
398+ offset += dataView . getUint16 ( offset , false ) ;
399+ }
400+ }
401+ return callback ( - 1 ) ;
402+ } ;
403+ reader . readAsArrayBuffer ( file . slice ( 0 , 64 * 1024 ) ) ;
404+ } ,
405+
373406 resizeImage : function ( imageFiles ) {
374407 var self = this ,
375408 $imageInput = this . $el . find ( '.js-itemImageUpload' ) ,
@@ -406,7 +439,14 @@ module.exports = baseVw.extend({
406439
407440 __ . each ( imageFiles , function ( imageFile ) {
408441 var newImage = document . createElement ( "img" ) ,
409- ctx ;
442+ ctx ,
443+ orientation ;
444+
445+ self . getOrientation ( imageFile , function ( val ) {
446+ if ( val === - 2 ) throw new Error ( 'The image is not a jpeg.' ) ;
447+ if ( val === - 1 ) throw new Error ( 'The image is undefined.' ) ;
448+ orientation = val ;
449+ } ) ;
410450
411451 newImage . src = imageFile . path ;
412452
@@ -430,6 +470,19 @@ module.exports = baseVw.extend({
430470 canvas . width = imgW ;
431471 canvas . height = imgH ;
432472 ctx = canvas . getContext ( '2d' ) ;
473+ if ( orientation > 4 ) {
474+ canvas . width = imgH ;
475+ canvas . height = imgW ;
476+ }
477+ switch ( orientation ) {
478+ case 2 : ctx . translate ( imgW , 0 ) ; ctx . scale ( - 1 , 1 ) ; break ;
479+ case 3 : ctx . translate ( imgW , imgH ) ; ctx . rotate ( Math . PI ) ; break ;
480+ case 4 : ctx . translate ( 0 , imgH ) ; ctx . scale ( 1 , - 1 ) ; break ;
481+ case 5 : ctx . rotate ( 0.5 * Math . PI ) ; ctx . scale ( 1 , - 1 ) ; break ;
482+ case 6 : ctx . rotate ( 0.5 * Math . PI ) ; ctx . translate ( 0 , - imgH ) ; break ;
483+ case 7 : ctx . rotate ( 0.5 * Math . PI ) ; ctx . translate ( imgW , - imgH ) ; ctx . scale ( - 1 , 1 ) ; break ;
484+ case 8 : ctx . rotate ( - 0.5 * Math . PI ) ; ctx . translate ( - imgW , 0 ) ; break ;
485+ }
433486 ctx . drawImage ( newImage , 0 , 0 , imgW , imgH ) ;
434487 dataURI = canvas . toDataURL ( 'image/jpeg' , 0.7 ) ;
435488 dataURI = dataURI . replace ( / ^ d a t a : i m a g e \/ ( p n g | j p e g | w e b p ) ; b a s e 6 4 , / , "" ) ;
0 commit comments