Skip to content

Commit f964d09

Browse files
committed
Add loadImage.replaceHead method.
1 parent 2c1d3aa commit f964d09

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

README.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- [crossOrigin](#crossorigin)
3838
- [noRevoke](#norevoke)
3939
- [Meta data parsing](#meta-data-parsing)
40+
- [Image head](#image-head)
4041
- [Exif parser](#exif-parser)
4142
- [IPTC parser](#iptc-parser)
4243
- [License](#license)
@@ -386,15 +387,43 @@ loadImage.parseMetaData(
386387
)
387388
```
388389

389-
**Note:**
390-
Blob objects of resized images can be created via
391-
[canvas.toBlob()](https://github.com/blueimp/JavaScript-Canvas-to-Blob).
392-
393390
The Meta data extension also adds additional options used for the
394391
`parseMetaData` method:
395392

396393
- `maxMetaDataSize`: Maximum number of bytes of meta data to parse.
397394
- `disableImageHead`: Disable parsing the original image head.
395+
- `disableMetaDataParsers`: Disable parsing meta data (image head only)
396+
397+
### Image head
398+
399+
Resized JPEG images can be combined with their original image head via
400+
`loadImage.replaceHead`, which requires the resized image as `Blob` object as
401+
first argument and an `ArrayBuffer` image head as second argument. The third
402+
argument must be a `callback` function, which is called with the new `Blob`
403+
object:
404+
405+
```js
406+
loadImage(
407+
fileOrBlobOrUrl,
408+
function (img, data) {
409+
if (data.imageHead && data.exif) {
410+
img.toBlob(function (blob) {
411+
loadImage.replaceHead(blob, data.imageHead, function (newBlob) {
412+
// do something with newBlob
413+
})
414+
}, 'image/jpeg')
415+
}
416+
},
417+
{ meta: true, canvas: true, maxWidth: 800 }
418+
)
419+
```
420+
421+
**Note:**
422+
Blob objects of resized images can be created via
423+
[canvas.toBlob](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob).
424+
For browsers which don't have native support, a
425+
[canvas.toBlob polyfill](https://github.com/blueimp/JavaScript-Canvas-to-Blob)
426+
is available.
398427

399428
### Exif parser
400429

js/load-image-meta.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
break
119119
}
120120
parsers = loadImage.metaDataParsers.jpeg[markerBytes]
121-
if (parsers) {
121+
if (parsers && !options.disableMetaDataParsers) {
122122
for (i = 0; i < parsers.length; i += 1) {
123123
parsers[i].call(
124124
that,
@@ -162,6 +162,23 @@
162162
}
163163
}
164164

165+
// Replaces the image head of a JPEG blob with the given one.
166+
// Calls the callback with the new Blob:
167+
loadImage.replaceHead = function (blob, head, callback) {
168+
loadImage.parseMetaData(
169+
blob,
170+
function (data) {
171+
callback(
172+
new Blob(
173+
[head, loadImage.blobSlice.call(blob, data.imageHead.byteLength)],
174+
{ type: 'image/jpeg' }
175+
)
176+
)
177+
},
178+
{ maxMetaDataSize: 256, disableMetaDataParsers: true }
179+
)
180+
}
181+
165182
// Determines if meta data should be loaded automatically:
166183
loadImage.hasMetaOption = function (options) {
167184
return options && options.meta

0 commit comments

Comments
 (0)