let canvas ; let context ; function detectionContext ( ) { if ( context ) { return context ; } if ( ! canvas ) { canvas = document . createElement ( 'canvas' ) ; } if ( ! canvas || ! canvas . getContext ) { return null ; } context = canvas . getContext ( '2d' ) ; return context ; } /**
 * Idea taken from https://github.com/stomita/ios-imagefile-megapixel
 * Detects whether the image has been sub sampled by the browser and does not have its original dimensions.
 * This method unfortunately does not work for images that have transparent background.
 */ export function detectSubsampling ( img , width , height ) { if ( ! detectionContext ( ) ) { return false ; } if ( width * height <= 1024 * 1024 ) { return false ; } canvas . width = canvas . height = 1 ; context . fillStyle = '#FF00FF' ; context . fillRect ( 0 , 0 , 1 , 1 ) ; context . drawImage ( img , - width + 1 , 0 ) ; try { const dat = context . getImageData ( 0 , 0 , 1 , 1 ) . data ; return ( dat [ 0 ] === 255 ) && ( dat [ 1 ] === 0 ) && ( dat [ 2 ] === 255 ) ; } catch ( err ) { return false ; } }