Welcome to Piexifjs’s documentation!

https://travis-ci.org/hMatoba/piexifjs.svg?branch=master

Exif manipulations with JavaScript. Writing, reading, and more… For client-side and Node.js. https://github.com/hMatoba/piexifjs

About Piexifjs

What for?

Exif manipulations in JS. Writing, reading, and more…

How to Use

There are only just four functions.

  • load(jpegData) - Get exif data as object.
  • dump(exifObj) - Get exif binary as string to insert into JPEG.
  • insert(exifbytes, jpegData) - Insert exif into JPEG.
  • remove(jpegData) - Remove exif from JPEG.

Dependency

No dependencies.

Environment

Tested on IE11, Opera28, and PhantomJS 1.9.8. It runs on even Node.js.

License

The MIT License (MIT)

Copyright (c) 2015 hMatoba

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Functions

Warning

It could set any value in exif without actual value. For example, actual XResolution is 300, whereas XResolution value in exif is 0. Confliction might happen.

Warning

To edit exif tags and values appropriately, read official document from P167-. http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf

load

piexif.load(jpegData)

Get exif data as object. jpegData must be a string that starts with “data:image/jpeg;base64,”(DataURL), “\xff\xd8”, or “Exif”.

Arguments:
  • jpegData (string) – JPEG data
Returns:

Exif data({“0th”:object, “Exif”:object, “GPS”:object, “Interop”:object, “1st”:object, “thumbnail”:string})

Return type:

object

var exifObj = piexif.load(jpegData);
for (var ifd in exifObj) {
    if (ifd == "thumbnail") {
        continue;
    }
    console.log("-" + ifd);
    for (var tag in exifObj[ifd]) {
        console.log("  " + piexif.TAGS[ifd][tag]["name"] + ":" + exifObj[ifd][tag]);
    }
}

dump

piexif.dump(exifObj)

Get exif binary as string to insert into JPEG.

Arguments:
  • exifObj (object) – Exif data({“0th”:0thIFD - object, “Exif”:ExifIFD - object, “GPS”:GPSIFD - object, “Interop”:InteroperabilityIFD - object, “1st”:1stIFD - object, “thumbnail”:JPEG data - string})
Returns:

Exif binary

Return type:

string

var zeroth = {};
var exif = {};
var gps = {};
zeroth[piexif.ImageIFD.Make] = "Make";
zeroth[piexif.ImageIFD.XResolution] = [777, 1];
zeroth[piexif.ImageIFD.YResolution] = [777, 1];
zeroth[piexif.ImageIFD.Software] = "Piexifjs";
exif[piexif.ExifIFD.DateTimeOriginal] = "2010:10:10 10:10:10";
exif[piexif.ExifIFD.LensMake] = "LensMake";
exif[piexif.ExifIFD.Sharpness] = 777;
exif[piexif.ExifIFD.LensSpecification] = [[1, 1], [1, 1], [1, 1], [1, 1]];
gps[piexif.GPSIFD.GPSVersionID] = [7, 7, 7, 7];
gps[piexif.GPSIFD.GPSDateStamp] = "1999:99:99 99:99:99";
var exifObj = {"0th":zeroth, "Exif":exif, "GPS":gps};
var exifbytes = piexif.dump(exifObj);

Properties of piexif.ImageIFD help to make 0thIFD and 1stIFD. piexif.ExifIFD is for ExifIFD. piexif.GPSIFD is for GPSIFD. piexif.InteropIFD is for InteroperabilityIFD.

Note

ExifTag(34665), GPSTag(34853), and InteroperabilityTag(40965) in 0thIFD automatically are set appropriate value.

Note

JPEGInterchangeFormat(513), and JPEGInterchangeFormatLength(514) in 1stIFD automatically are set appropriate value.

Note

If ‘thumbnail’ is contained in exifObj, ‘1st’ must be contained – and vice versa. 1stIFD means thumbnail’s information.

insert

piexif.insert(exifbytes, jpegData)

Insert exif into JPEG.

Arguments:
  • exifbytes (string) – Exif binary
  • jpegData (string) – JPEG data
Returns:

JPEG data

Return type:

string

var exifbytes = piexif.dump(exifObj)
var newJpeg = piexif.insert(exifbytes, jpegData)

remove

piexif.remove(jpegData)

Remove exif from JPEG.

Arguments:
  • jpegData (string) – JPEG data
Returns:

JPEG data

Return type:

string

var newJpeg = piexif.remove("foo.jpg")

Appendices

Exif Data in Piexifjs

Each exif tag has appropriate type of the value. BYTE, ASCII, SHORT, or… See the document of Exif. http://www.cipa.jp/std/documents/e/DC-008-2012_E.pdf

Exif Type JavaScript
BYTE int
ASCII string
SHORT int
LONG int
RATIONAL [int, int]
UNDEFINED string
SRATIONAL [int, int]

If value type is number(BYTE, SHORT, LONG, RATIONAL, or SRATIONAL) and value count is two or more number, it is expressed with Array.

BYTE, SHORT, LONG [int, int, …]
RATIONAL, SRATIONAL [[int, int], [int, int], …]

Note

If value type is number and value count is one, array that is length one value(e.g. [int]) also be accepted.

Samples

Insert Exif into jpeg

<input type="file" id="files" />
<script source="/js/piexif.js" />
<script>
function handleFileSelect(evt) {
    var file = evt.target.files[0];

    var zeroth = {};
    var exif = {};
    var gps = {};
    zeroth[piexif.ImageIFD.Make] = "Make";
    zeroth[piexif.ImageIFD.XResolution] = [777, 1];
    zeroth[piexif.ImageIFD.YResolution] = [777, 1];
    zeroth[piexif.ImageIFD.Software] = "Piexifjs";
    exif[piexif.ExifIFD.DateTimeOriginal] = "2010:10:10 10:10:10";
    exif[piexif.ExifIFD.LensMake] = "LensMake";
    exif[piexif.ExifIFD.Sharpness] = 777;
    exif[piexif.ExifIFD.LensSpecification] = [[1, 1], [1, 1], [1, 1], [1, 1]];
    gps[piexif.GPSIFD.GPSVersionID] = [7, 7, 7, 7];
    gps[piexif.GPSIFD.GPSDateStamp] = "1999:99:99 99:99:99";
    var exifObj = {"0th":zeroth, "Exif":exif, "GPS":gps};
    var exifbytes = piexif.dump(exifObj);

    var reader = new FileReader();
    reader.onload = function(e) {
        var inserted = piexif.insert(exifbytes, e.target.result);

        var image = new Image();
        image.src = inserted;
        image.width = 200;
        var el = $("<div></div>").append(image);
        $("#resized").prepend(el);

    };
    reader.readAsDataURL(file);
}

document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

Read Exif Values

var reader = new FileReader();
reader.onloadend = function(e) {
    var exifObj = piexif.load(e.target.result);
    for (var ifd in exifObj) {
        if (ifd == "thumbnail") {
            continue;
        }
        console.log("-" + ifd);
        for (var tag in exifObj[ifd]) {
            console.log("  " + piexif.TAGS[ifd][tag]["name"] + ":" + exifObj[ifd][tag]);
        }
    }
};
reader.readAsDataURL(file);

Generates Rotated JPEG

function postJpeg (binStr) {
    var array = [];
    for (var p=0; p<data.length; p++) {
        array[p] = data.charCodeAt(p);
    }
    var u8array = new Uint8Array(array);

    var req = new XMLHttpRequest();
    req.open("POST", "/jpeg", false);
    req.setRequestHeader('Content-Type', 'image/jpeg');
    req.send(u8array.buffer);
}


function previewJpeg(evt) {
    var files = evt.target.files;
    var previewDiv = $("#preview");
    for (var i=0; i<files.length; i++) {
        var file = files[i];
        if (!file.type.match('image/jpeg.*')) {
            continue;
        }

        var reader = new FileReader();
        reader.onload = function(e) {
            var exif = piexif.load(e.target.result);
            var image = new Image();
            image.onload = function () {
                var orientation = exif["0th"][piexif.ImageIFD.Orientation];

                var canvas = document.createElement("canvas");
                canvas.width = image.width;
                canvas.height = image.height;
                var ctx = canvas.getContext("2d");
                var x = 0;
                var y = 0;
                ctx.save();
                if (orientation == 2) {
                    x = -canvas.width;
                    ctx.scale(-1, 1);
                } else if (orientation == 3) {
                    x = -canvas.width;
                    y = -canvas.height;
                    ctx.scale(-1, -1);
                } else if (orientation == 3) {
                    x = -canvas.width;
                    y = -canvas.height;
                    ctx.scale(-1, -1);
                } else if (orientation == 4) {
                    y = -canvas.height;
                    ctx.scale(1, -1);
                } else if (orientation == 5) {
                    canvas.width = image.height;
                    canvas.height = image.width;
                    ctx.translate(canvas.width, canvas.height / canvas.width);
                    ctx.rotate(Math.PI / 2);
                    y = -canvas.width;
                    ctx.scale(1, -1);
                } else if (orientation == 6) {
                    canvas.width = image.height;
                    canvas.height = image.width;
                    ctx.translate(canvas.width, canvas.height / canvas.width);
                    ctx.rotate(Math.PI / 2);
                } else if (orientation == 7) {
                    canvas.width = image.height;
                    canvas.height = image.width;
                    ctx.translate(canvas.width, canvas.height / canvas.width);
                    ctx.rotate(Math.PI / 2);
                    x = -canvas.height;
                    ctx.scale(-1, 1);
                } else if (orientation == 8) {
                    canvas.width = image.height;
                    canvas.height = image.width;
                    ctx.translate(canvas.width, canvas.height / canvas.width);
                    ctx.rotate(Math.PI / 2);
                    x = -canvas.height;
                    y = -canvas.width;
                    ctx.scale(-1, -1);
                }
                ctx.drawImage(image, x, y);
                ctx.restore();

                var dataURL = canvas.toDataURL("image/jpeg", 1.0);
                var jpegBinary = atob(dataURL.split(",")[1]);

                var div = $("<div></div>");
                div.append(canvas);
                var button = $("<button>post this image</button>");
                button.click(function () {
                    //postJpeg(jpegBinary);
                });

                previewDiv.prepend(div).prepend(button);
            };
            image.src = e.target.result;
        };

        reader.readAsDataURL(file);
    }
}

document.getElementById("files").onchange = previewJpeg;

GPS Coordinates

var lat = 59.43553989213321;
var lng = 24.73842144012451;
gpsIfd[piexif.GPSIFD.GPSLatitudeRef] = lat < 0 ? 'S' : 'N';
gpsIfd[piexif.GPSIFD.GPSLatitude] = piexif.GPSHelper.degToDmsRational(lat);
gpsIfd[piexif.GPSIFD.GPSLongitudeRef] = lng < 0 ? 'W' : 'E';
gpsIfd[piexif.GPSIFD.GPSLongitude] = piexif.GPSHelper.degToDmsRational(lng);

Node.js

var piexif = require("piexifjs");
var fs = require("fs");

var filename1 = "in.jpg";
var filename2 = "out.jpg";

var jpeg = fs.readFileSync(filename1);
var data = jpeg.toString("binary");

var zeroth = {};
var exif = {};
var gps = {};
zeroth[piexif.ImageIFD.Make] = "Make";
zeroth[piexif.ImageIFD.XResolution] = [777, 1];
zeroth[piexif.ImageIFD.YResolution] = [777, 1];
zeroth[piexif.ImageIFD.Software] = "Piexifjs";
exif[piexif.ExifIFD.DateTimeOriginal] = "2010:10:10 10:10:10";
exif[piexif.ExifIFD.LensMake] = "LensMake";
exif[piexif.ExifIFD.Sharpness] = 777;
exif[piexif.ExifIFD.LensSpecification] = [[1, 1], [1, 1], [1, 1], [1, 1]];
gps[piexif.GPSIFD.GPSVersionID] = [7, 7, 7, 7];
gps[piexif.GPSIFD.GPSDateStamp] = "1999:99:99 99:99:99";
var exifObj = {"0th":zeroth, "Exif":exif, "GPS":gps};
var exifbytes = piexif.dump(exifObj);

var newData = piexif.insert(exifbytes, data);
var newJpeg = new Buffer(newData, "binary");
fs.writeFileSync(filename2, newJpeg);

Changelog

1.03

  • Bug fix. Issues #19.

1.02

  • Bug fix. To remove IFD pointer tag.

1.0

  • Release.

Indices and tables