@live-change/image-frontend 0.9.25 → 0.9.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,16 @@
1
1
  import pica from 'pica'
2
2
 
3
3
  const imageResizer = pica({
4
- createCanvas: (w, h) => new OffscreenCanvas(w, h)
4
+ features : ['js', 'wasm', 'ww'],
5
+ createCanvas: (w, h) => {
6
+ /* if(window.OffscreenCanvas) {
7
+ return new OffscreenCanvas(w, h)
8
+ }*/
9
+ const canvas = document.createElement('canvas')
10
+ canvas.width = w
11
+ canvas.height = h
12
+ return canvas
13
+ }
5
14
  })
6
15
 
7
16
  export default imageResizer
@@ -15,8 +15,8 @@ async function handleUpload(upload, options) {
15
15
  export function uploadImage(purpose, uploadable, options = {}) {
16
16
  const upload = new ImageUpload(purpose, uploadable, options)
17
17
  if(options.id) {
18
- const existingIndex = imageUploads.value.findIndex(u => u.id == options.id)
19
- if( existingIndex != -1 ) {
18
+ const existingIndex = imageUploads.value.findIndex(u => u.id === options.id)
19
+ if( existingIndex !== -1 ) {
20
20
  imageUploads.value[existingIndex].cancel()
21
21
  imageUploads.value[existingIndex] = upload
22
22
  handleUpload(upload, options)
@@ -13,22 +13,22 @@ function getOrientation(jpegData) {
13
13
  if(window.forceOrientation) return window.forceOrientation
14
14
 
15
15
  let view = new DataView(jpegData)
16
- if (view.getUint16(0, false) != 0xFFD8) return undefined
16
+ if (view.getUint16(0, false) !== 0xFFD8) return undefined
17
17
  let length = view.byteLength, offset = 2
18
18
  while (offset < length) {
19
19
  let marker = view.getUint16(offset, false)
20
20
  offset += 2
21
- if (marker == 0xFFE1) {
22
- if (view.getUint32(offset += 2, false) != 0x45786966) return undefined
23
- let little = view.getUint16(offset += 6, false) == 0x4949
21
+ if (marker === 0xFFE1) {
22
+ if (view.getUint32(offset += 2, false) !== 0x45786966) return undefined
23
+ let little = view.getUint16(offset += 6, false) === 0x4949
24
24
  offset += view.getUint32(offset + 4, little)
25
25
  let tags = view.getUint16(offset, little)
26
26
  offset += 2
27
27
  for (let i = 0; i < tags; i++)
28
- if (view.getUint16(offset + (i * 12), little) == 0x0112)
28
+ if (view.getUint16(offset + (i * 12), little) === 0x0112)
29
29
  return (view.getUint16(offset + (i * 12) + 8, little))
30
30
  }
31
- else if ((marker & 0xFF00) != 0xFF00) break;
31
+ else if ((marker & 0xFF00) !== 0xFF00) break;
32
32
  else offset += view.getUint16(offset, false)
33
33
  }
34
34
  return undefined
@@ -52,36 +52,45 @@ function loadImageUpload(file) {
52
52
  let img = document.createElement("img")
53
53
  let reader = new FileReader()
54
54
  reader.onload = async function(e) {
55
- if(file.type == "image/jpeg") {
56
- let headerReader = new FileReader()
57
- headerReader.onload=function(he) {
58
- let orientation = getOrientation(he.target.result)
55
+ try {
56
+ /* const arrayBuffer = event.target.result
57
+ const blob = new Blob([arrayBuffer], { type: file.type })
58
+ const objectURL = URL.createObjectURL(blob)
59
+ throw new Error("F"+file.type)*/
60
+ const objectURL = e.target.result
61
+ if(file.type === "image/jpeg") {
62
+ let headerReader = new FileReader()
63
+ headerReader.onload = function(he) {
64
+ let orientation = getOrientation(he.target.result)
65
+ img.onload = function(e) {
66
+ resolve({
67
+ image: img,
68
+ type: file.type,
69
+ orientation: orientation,
70
+ sizeSwap: orientation > 4,
71
+ width: orientation > 4 ? img.height : img.width,
72
+ height: orientation > 4 ? img.width : img.height
73
+ })
74
+ }
75
+ img.src = objectURL
76
+ }
77
+ headerReader.onerror = function(e) {
78
+ reject(e.target.error)
79
+ }
80
+ headerReader.readAsArrayBuffer(file.slice(0, 64 * 1024))
81
+ } else {
59
82
  img.onload = function(e) {
60
83
  resolve({
61
84
  image: img,
62
85
  type: file.type,
63
- orientation: orientation,
64
- sizeSwap: orientation>4,
65
- width: orientation>4 ? img.height : img.width,
66
- height: orientation>4 ? img.width : img.height
86
+ width: img.width,
87
+ height: img.height
67
88
  })
68
89
  }
69
- img.src = e.target.result
70
- }
71
- headerReader.onerror = function(e) {
72
- reject(e.target.error)
73
- }
74
- headerReader.readAsArrayBuffer(file.slice(0, 64 * 1024))
75
- } else {
76
- img.onload = function(e) {
77
- resolve({
78
- image: img,
79
- type: file.type,
80
- width: img.width,
81
- height: img.height
82
- })
90
+ img.src = objectURL
83
91
  }
84
- img.src = e.target.result
92
+ } catch(e) {
93
+ reject(e)
85
94
  }
86
95
  }
87
96
  reader.onerror = function(e) {
@@ -1,5 +1,5 @@
1
1
  import {
2
- imageToCanvas, loadImageUpload, cancelOrientation, hasAlpha, isExifOrientationSupported
2
+ imageToCanvas, loadImageUpload, cancelOrientation, hasAlpha, isExifOrientationSupported, loadImage
3
3
  } from "./imageUtils.js"
4
4
  import imageResizer from "./imageResizer"
5
5
 
@@ -68,6 +68,8 @@ async function preProcessImageFile({ file, image, canvas }, config) {
68
68
 
69
69
  if(!canvas) canvas = imageToCanvas(image.image)
70
70
 
71
+ const exifOrientationSupported = (await isExifOrientationSupported())
72
+
71
73
  if(image.width > maxUploadWidth || image.height > maxUploadHeight ) { /// RESIZING NEEDED
72
74
  const maxRatio = maxUploadWidth / maxUploadHeight
73
75
  const inputRatio = image.width / image.height
@@ -77,30 +79,38 @@ async function preProcessImageFile({ file, image, canvas }, config) {
77
79
  targetWidth = maxUploadWidth
78
80
  targetHeight = Math.round(maxUploadWidth / inputRatio)
79
81
  } else { /// scale to max height
82
+ let scale = maxUploadHeight / image.height
80
83
  targetWidth = Math.round(maxUploadHeight * inputRatio)
81
84
  targetHeight = maxUploadHeight
82
85
  }
83
86
 
84
87
  console.log(`RESIZING ${image.width}x${image.height} => ${targetWidth}x${targetHeight}`)
85
88
 
89
+ let [destWidth, destHeight] =
90
+ (image.orientation > 4/* && !exifOrientationSupported*/) /// use orientation when scaling even if supported!
91
+ ? [targetHeight, targetWidth]
92
+ : [targetWidth, targetHeight]
86
93
  let destCanvas = document.createElement('canvas')
87
- if(image.orientation > 4 && !(await isExifOrientationSupported())) { // Swap dimmensions
88
- destCanvas.width = targetHeight
89
- destCanvas.height = targetWidth
90
- } else {
91
- destCanvas.width = targetWidth
92
- destCanvas.height = targetHeight
94
+ destCanvas.width = destWidth
95
+ destCanvas.height = destHeight
96
+ try {
97
+ await imageResizer.resize(canvas, destCanvas, {
98
+ unsharpAmount: 80,
99
+ unsharpRadius: 0.6,
100
+ unsharpThreshold: 2,
101
+ alpha: hasAlpha(canvas)
102
+ })
103
+ } catch (e) {
104
+ const context = destCanvas.getContext('2d')
105
+ context.imageSmoothingEnabled = true
106
+ context.imageSmoothingQuality = 'high'
107
+ context.drawImage(canvas, 0, 0, destCanvas.width, destCanvas.height)
93
108
  }
94
- await imageResizer.resize(canvas, destCanvas, {
95
- unsharpAmount: 80,
96
- unsharpRadius: 0.6,
97
- unsharpThreshold: 2,
98
- alpha: hasAlpha(canvas)
99
- })
100
109
  canvas = destCanvas
101
110
  }
102
111
 
103
- if(image.orientation && !(await isExifOrientationSupported())) {
112
+ if(image.orientation && !exifOrientationSupported) {
113
+ // if exif orientation is supoorted it will be automatically canceled!
104
114
  console.log("CANCEL ORIENTATION", image.orientation)
105
115
  canvas = cancelOrientation(canvas, image.orientation)
106
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/image-frontend",
3
- "version": "0.9.25",
3
+ "version": "0.9.27",
4
4
  "scripts": {
5
5
  "memDev": "node server/start.js memDev --enableSessions --initScript ./init.js --dbAccess",
6
6
  "localDevInit": "rm tmp.db; lcli localDev --enableSessions --initScript ./init.js",
@@ -22,15 +22,15 @@
22
22
  "type": "module",
23
23
  "dependencies": {
24
24
  "@fortawesome/fontawesome-free": "^6.7.2",
25
- "@live-change/cli": "^0.9.25",
26
- "@live-change/dao": "^0.9.25",
27
- "@live-change/dao-vue3": "^0.9.25",
28
- "@live-change/dao-websocket": "^0.9.25",
29
- "@live-change/framework": "^0.9.25",
30
- "@live-change/image-service": "^0.9.25",
31
- "@live-change/session-service": "^0.9.25",
32
- "@live-change/vue3-components": "^0.9.25",
33
- "@live-change/vue3-ssr": "^0.9.25",
25
+ "@live-change/cli": "^0.9.27",
26
+ "@live-change/dao": "^0.9.27",
27
+ "@live-change/dao-vue3": "^0.9.27",
28
+ "@live-change/dao-websocket": "^0.9.27",
29
+ "@live-change/framework": "^0.9.27",
30
+ "@live-change/image-service": "^0.9.27",
31
+ "@live-change/session-service": "^0.9.27",
32
+ "@live-change/vue3-components": "^0.9.27",
33
+ "@live-change/vue3-ssr": "^0.9.27",
34
34
  "@vueuse/core": "^12.3.0",
35
35
  "codeceptjs-assert": "^0.0.5",
36
36
  "compression": "^1.7.5",
@@ -50,7 +50,7 @@
50
50
  "vue3-scroll-border": "0.1.6"
51
51
  },
52
52
  "devDependencies": {
53
- "@live-change/codeceptjs-helper": "^0.9.25",
53
+ "@live-change/codeceptjs-helper": "^0.9.27",
54
54
  "codeceptjs": "^3.6.10",
55
55
  "generate-password": "1.7.1",
56
56
  "playwright": "1.49.1",
@@ -61,5 +61,5 @@
61
61
  "author": "Michał Łaszczewski <michal@laszczewski.pl>",
62
62
  "license": "BSD-3-Clause",
63
63
  "description": "",
64
- "gitHead": "04e2869238087adc1a48784048614e1ee738ad1e"
64
+ "gitHead": "110f38f6d6a07310e7bf5ba0eaa15426795624e8"
65
65
  }