Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | import { DEFAULT_FILE_TYPE_EXPORTED, DEFAULT_IMAGE_QUALITY_EXPORTED, DEFAULT_IMAGE_ROTATION_DEGREES, FORM_IMAGE_UPLOADER_DEFAULT_FORMAT_TO_BASE_64_OPTIONS } from './config.js' export function formatToBase64({file, item, options = FORM_IMAGE_UPLOADER_DEFAULT_FORMAT_TO_BASE_64_OPTIONS, ...rest}) { if (file) { const reader = new window.FileReader() reader.readAsDataURL(file) let originalBase64 return new Promise((resolve, reject) => { reader.onload = () => { getExifOrientation(file) .then(getExifOrientationResult => { /* * Since Chrome 81, image EXIF orientation is respected by default. * Latest Safari (13.1 as of now) is also working correctly. */ const browserAutoRotates = getComputedStyle(document.body).imageOrientation == 'from-image' // eslint-disable-line if (!browserAutoRotates) { switch (getExifOrientationResult) { case 3: case 4: options.rotation = 180 break case 5: case 6: options.rotation = 90 break case 7: case 8: options.rotation = 270 } } return resizeImage({ base64Image: reader.result, ...options }) }) .then(base64Image => { originalBase64 = base64Image return cropAndRotateImage({ base64Image, ...options }) }) .then(value => base64ToBlob(value)) .then(({blob, base64}) => { resolve({ file, blob, originalBase64, croppedBase64: base64, rotation: options.rotation, ...rest }) }) .catch(e => { resolve({hasErrors: true, ...rest}) }) } }) } else { const {url, id, ...rest} = item return new Promise((resolve, reject) => { cropAndRotateImage({ imageURL: url, ...options }) .then(value => base64ToBlob(value)) .then(({blob, base64}) => { resolve({ url, blob, croppedBase64: base64, id, ...rest }) }) .catch(e => { resolve({url, id, hasErrors: true, ...rest}) }) }) } } export function resizeImage({base64Image, maxImageWidth, maxImageHeight, imageMimeType}) { const canvas = document.createElement('canvas') const context = canvas.getContext('2d') const image = new Image() // eslint-disable-line image.src = base64Image return new Promise((resolve, reject) => { image.onerror = () => { reject(new Error(`Image load fails. Origin: File resizing`)) } image.onload = () => { const inputWidth = image.naturalWidth const inputHeight = image.naturalHeight if (inputHeight > maxImageHeight || inputWidth > maxImageWidth) { const inputImageAspectRatio = inputWidth / inputHeight if (inputHeight > inputWidth) { const scaleFactorHeight = maxImageHeight / inputHeight canvas.width = maxImageHeight * inputImageAspectRatio canvas.height = maxImageHeight context.translate(canvas.width / 2, canvas.height / 2) context.scale(scaleFactorHeight, scaleFactorHeight) context.translate(-canvas.width / 2, -canvas.height / 2) } else { const scaleFactorWidth = maxImageWidth / inputWidth canvas.width = maxImageWidth canvas.height = maxImageWidth / inputImageAspectRatio context.translate(canvas.width / 2, canvas.height / 2) context.scale(scaleFactorWidth, scaleFactorWidth) context.translate(-canvas.width / 2, -canvas.height / 2) } context.drawImage(image, (canvas.width - inputWidth) / 2, (canvas.height - inputHeight) / 2) resolve(canvas.toDataURL(imageMimeType, 1)) } else { resolve(base64Image) } } }) } export function cropAndRotateImage({ base64Image, imageURL, rotation = DEFAULT_IMAGE_ROTATION_DEGREES, outputImageAspectRatio, outputImageAspectRatioDisabled, imageQuality = DEFAULT_IMAGE_QUALITY_EXPORTED, imageMimeType = DEFAULT_FILE_TYPE_EXPORTED }) { const canvas = document.createElement('canvas') const context = canvas.getContext('2d') const image = new Image() // eslint-disable-line if (imageURL) { image.crossOrigin = 'Anonymous' } if (base64Image) { image.src = base64Image } else { image.src = imageURL } return new Promise((resolve, reject) => { image.onerror = () => { reject(new Error(`Image load fails. Origin: ${imageURL ? `URL ${imageURL}` : `File upload`}`)) } image.onload = () => { const inputWidth = image.naturalWidth const inputHeight = image.naturalHeight let outputWidth let outputHeight let inputImageAspectRatio if (rotation === 0 || rotation === 180) { inputImageAspectRatio = inputWidth / inputHeight outputWidth = inputWidth outputHeight = inputHeight } else { inputImageAspectRatio = inputHeight / inputWidth outputWidth = inputHeight outputHeight = inputWidth } if (!outputImageAspectRatioDisabled) { if (inputImageAspectRatio > outputImageAspectRatio) { outputWidth = outputHeight * outputImageAspectRatio } else if (inputImageAspectRatio < outputImageAspectRatio) { outputHeight = outputWidth / outputImageAspectRatio } } canvas.width = outputWidth canvas.height = outputHeight switch (rotation) { case 0: context.translate((outputWidth - inputWidth) * 0.5, (outputHeight - inputHeight) * 0.5) break case 90: context.rotate(0.5 * Math.PI) context.translate((outputHeight - inputWidth) * 0.5, -(outputWidth + inputHeight) * 0.5) break case 180: context.rotate(Math.PI) context.translate(-(outputWidth + inputWidth) * 0.5, -(outputHeight + inputHeight) * 0.5) break case 270: context.rotate((270 * Math.PI) / 180) context.translate(-(outputHeight + inputWidth) * 0.5, (outputWidth - inputHeight) * 0.5) } context.drawImage(image, 0, 0) resolve(canvas.toDataURL(imageMimeType, imageQuality)) } }) } /** * * @param {String} base64 to transform to Blob type * @param {String} imageMimeType to define the Blob's MIME type (default: JPEG) * * returns a new Blob and the original base64 string */ export function base64ToBlob(base64, imageMimeType = DEFAULT_FILE_TYPE_EXPORTED) { var byteString = atob(base64.split(',')[1]) // eslint-disable-line const ab = new ArrayBuffer(byteString.length) const ia = new Uint8Array(ab) for (let i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i) } return new Promise(resolve => resolve({blob: new Blob([ia], {type: imageMimeType}), base64})) // eslint-disable-line } /** * * @param {File} file containing image data * * returns a number, with this possible values: * 1, 2, 3, 4, 5, 6, 7 or 8 : EXIF orientation values * -1 : EXIF orientation value not defined * -2 : not a JPEG type file */ function getExifOrientation(file) { const reader = new window.FileReader() return new Promise((resolve, reject) => { reader.onload = function (e) { const view = new DataView(e.target.result) if (view.getUint16(0, false) !== 0xffd8) { resolve(-2) } const length = view.byteLength let offset = 2 while (offset < length) { if (offset + 2 > view.byteLength) { break } if (view.getUint16(offset + 2, false) <= 8) resolve(-1) const marker = view.getUint16(offset, false) offset += 2 if (marker === 0xffe1) { if (view.getUint32((offset += 2), false) !== 0x45786966) { resolve(-1) } const little = view.getUint16((offset += 6), false) === 0x4949 offset += view.getUint32(offset + 4, little) const tags = view.getUint16(offset, little) offset += 2 for (let i = 0; i < tags; i++) { if (view.getUint16(offset + i * 12, little) === 0x0112) { resolve(view.getUint16(offset + i * 12 + 8, little)) } } } else if ((marker && 0xff00) !== 0xff00) { break } else { offset += view.getUint16(offset, false) } } resolve(-1) } reader.readAsArrayBuffer(file) }) } |