You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			41 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
		
		
			
		
	
	
			41 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			JavaScript
		
	
| 
											8 years ago
										 | const loadImage = require('blueimp-load-image'); | ||
|  | 
 | ||
|  | const DEFAULT_JPEG_QUALITY = 0.85; | ||
|  | 
 | ||
|  | // File | Blob | URLString -> LoadImageOptions -> Promise<DataURLString>
 | ||
|  | //
 | ||
|  | // Documentation for `options` (`LoadImageOptions`):
 | ||
|  | // https://github.com/blueimp/JavaScript-Load-Image/tree/v2.18.0#options
 | ||
|  | exports.autoOrientImage = (fileOrBlobOrURL, options = {}) => { | ||
|  |   const optionsWithDefaults = Object.assign( | ||
|  |     { | ||
|  |       type: 'image/jpeg', | ||
|  |       quality: DEFAULT_JPEG_QUALITY, | ||
|  |     }, | ||
|  |     options, | ||
|  |     { | ||
|  |       canvas: true, | ||
|  |       orientation: true, | ||
|  |     } | ||
|  |   ); | ||
|  | 
 | ||
|  |   return new Promise((resolve, reject) => { | ||
|  |     loadImage(fileOrBlobOrURL, (canvasOrError) => { | ||
|  |       if (canvasOrError.type === 'error') { | ||
|  |         const error = new Error('autoOrientImage: Failed to process image'); | ||
|  |         error.cause = canvasOrError; | ||
|  |         reject(error); | ||
|  |         return; | ||
|  |       } | ||
|  | 
 | ||
|  |       const canvas = canvasOrError; | ||
|  |       const dataURL = canvas.toDataURL( | ||
|  |         optionsWithDefaults.type, | ||
|  |         optionsWithDefaults.quality | ||
|  |       ); | ||
|  | 
 | ||
|  |       resolve(dataURL); | ||
|  |     }, optionsWithDefaults); | ||
|  |   }); | ||
|  | }; |