datasync-blob 1.1.19 → 1.1.22

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.
@@ -17,7 +17,7 @@ class DsBlob extends _react.Component {
17
17
  }
18
18
 
19
19
  //-----------------------------------------------------------------------------------------------------------------------------------------
20
- reduceImage = e => {
20
+ reduceBinaryImage = e => {
21
21
  let imageSource = e.path && e.path[0] || e.srcElement; //Safari compliancy
22
22
 
23
23
  if (this.debugging) console.log("imageSource = ", imageSource);
@@ -91,11 +91,93 @@ class DsBlob extends _react.Component {
91
91
  });
92
92
  }
93
93
  };
94
+ //-----------------------------------------------------------------------------------------------------------------------------------------
95
+ reduceBinaryImage = e => {
96
+ let imageSource = e.path && e.path[0] || e.srcElement; //Safari compliancy
97
+
98
+ alert("reduceBinaryImage !");
99
+ if (this.debugging) console.log("imageSource = ", imageSource);
100
+ var canvas = document.createElement('canvas'),
101
+ context,
102
+ width = imageSource.width,
103
+ height = imageSource.height;
104
+
105
+ //Exact size props are set
106
+ if (this.props.width && this.props.height) {
107
+ //Check image size
108
+ if (width > this.props.width || height > this.props.height) {
109
+ alert("L'image est trop grande, le format requis est " + this.props.width + "x" + this.props.height);
110
+ return;
111
+ }
112
+
113
+ //Check image size
114
+ if (width < this.props.width || height < this.props.height) {
115
+ alert("L'image est trop petite, le format requis est " + this.props.width + "x" + this.props.height);
116
+ return;
117
+ }
118
+ }
119
+
120
+ //Max size props are set
121
+ if (this.props.maxWidth && this.props.maxHeight) {
122
+ if (width > height) {
123
+ //Check resize is enabled
124
+ if (!this.props.reduceImage) {
125
+ alert("L'image est trop large ! la largeur maximale est de " + this.props.maxWidth);
126
+ return;
127
+ }
128
+ if (width > this.props.maxWidth) {
129
+ height *= this.props.maxWidth / width;
130
+ width = this.props.maxWidth;
131
+ }
132
+ } else {
133
+ if (height > this.props.maxHeight) {
134
+ //Check resize is enabled
135
+ if (!this.props.reduceImage) {
136
+ alert("L'image est trop haute, la hauteur maximale est de " + this.props.maxHeight);
137
+ return;
138
+ }
139
+ width *= this.props.maxHeight / height;
140
+ height = this.props.maxHeight;
141
+ }
142
+ }
143
+ }
144
+
145
+ //Build 2d picture
146
+ canvas.width = width;
147
+ canvas.height = height;
148
+ context = canvas.getContext('2d');
149
+ context.drawImage(imageSource, 0, 0, width, height);
150
+
151
+ //Binary conversion using toBlob to maintain binary output
152
+ let jpegCompressionRatio = this.props.jpegQuality && this.props.jpegQuality > 0 && this.props.jpegQuality < 1 ? this.props.jpegQuality : 1;
153
+ canvas.toBlob(blob => {
154
+ if (blob) {
155
+ console.log("Binary blob created, size:", blob.size);
156
+ if (this.debugging) console.log("jpegCompressionRatio->", jpegCompressionRatio, " blob.size = ", blob.size);
157
+
158
+ // Convert blob to ArrayBuffer for binary data
159
+ const reader = new FileReader();
160
+ reader.onload = () => {
161
+ const binaryData = reader.result; // ArrayBuffer
162
+ console.log("Binary data ArrayBuffer:", binaryData);
163
+
164
+ // Pass binary data directly to uploadPicture
165
+ this.props.uploadPicture({
166
+ data: binaryData,
167
+ item_id: this.props.item_id
168
+ });
169
+ };
170
+ reader.readAsArrayBuffer(blob);
171
+ } else {
172
+ console.error("Failed to create blob from canvas");
173
+ }
174
+ }, 'image/jpeg', jpegCompressionRatio);
175
+ };
94
176
 
95
177
  //-----------------------------------------------------------------------------------------------------------------------------------------
96
178
  storeBase64ImageToImg = e => {
97
179
  var image = document.createElement('img');
98
- image.onload = this.reduceImage;
180
+ image.onload = this.reduceBinaryImage;
99
181
  console.log("storeImageToImg:e.currentTarget.result = ", e.currentTarget.result);
100
182
  alert("storeImageToImg !");
101
183
  image.src = e.currentTarget.result;
@@ -117,13 +199,12 @@ class DsBlob extends _react.Component {
117
199
  image.src = blobUrl;
118
200
 
119
201
  // Clean up blob URL after image loads to prevent memory leaks
120
- /* 2Do Debug
121
- image.onload = (loadEvent) => {
122
- this.reduceImage(loadEvent);
123
- URL.revokeObjectURL(blobUrl);
124
- // Clear the stored image type
125
- this.currentImageType = null;
126
- };*/
202
+ image.onload = loadEvent => {
203
+ this.reduceBinaryImage(loadEvent);
204
+ URL.revokeObjectURL(blobUrl);
205
+ // Clear the stored image type
206
+ this.currentImageType = null;
207
+ };
127
208
  };
128
209
 
129
210
  //-----------------------------------------------------------------------------------------------------------------------------------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datasync-blob",
3
- "version": "1.1.19",
3
+ "version": "1.1.22",
4
4
  "description": "Datasync Blob component",
5
5
  "main": "./dist/components/DsBlob.js",
6
6
  "files": [