Novis\Image\Image::keepingMetadata
$image->keepingMetadata(): ImageKeeps the EXIF data of the input file in the output file.
EXIF data is information that a camera or a phone writes into a photo. It can include the date, the camera model and the place where the photo was taken. By default, encode and encodeVariants remove all of it, so a photo you publish does not show where a user lives.
keepingMetadata returns a copy that writes the EXIF data back into the output. This works for JPEG, PNG and WebP files. An AVIF file never has EXIF data.
When open turns a photo upright, it also sets the orientation in the kept EXIF data to 1. Because of this, the photo is not turned a second time when a program opens the file.
Good to know: only keep the metadata when you need it, for example for a photo archive. For an image that strangers will see, the default is the safe choice.
Return value
Returns Image.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff. |
Examples
Removed by default
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows that `encode` removes the EXIF data of a photo unless you keep it.use Novis\Image\Image;
// A JPEG with an EXIF block replaces a photo from a phone. The block has orientation 6.tainted bytes $jpeg = Image::create(400, 200, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();bytes $exif = Core\Encoding::fromHex('ffe1002245786966000049492a0008000000010012010300010000000600000000000000');array<tainted bytes> $parts = [Core\Bytes::slice($jpeg, 0, 2), $exif, Core\Bytes::slice($jpeg, 2)];tainted bytes $photo = Core\Bytes::join($parts);// This prints 6.echo Image::info($photo)->orientation, "\n";
// `autoOrient: false` leaves the pixels as they are. `encode` removes the EXIF block. This prints 1.tainted bytes $published = Image::open($photo, { autoOrient: false })->encode();echo Image::info($published)->orientation, "\n";61Keep the exif data
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows how to keep the EXIF data of a photo, for example in a photo archive.use Novis\Image\Image;
// This JPEG has an EXIF block, like a photo from a phone. The block has orientation 6.tainted bytes $jpeg = Image::create(400, 200, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();bytes $exif = Core\Encoding::fromHex('ffe1002245786966000049492a0008000000010012010300010000000600000000000000');array<tainted bytes> $parts = [Core\Bytes::slice($jpeg, 0, 2), $exif, Core\Bytes::slice($jpeg, 2)];tainted bytes $photo = Core\Bytes::join($parts);
// `keepingMetadata` writes the EXIF block into the new file. This prints 6.tainted bytes $archived = Image::open($photo, { autoOrient: false })->keepingMetadata()->encode();echo Image::info($archived)->orientation, "\n";6Keep in every variant
<?nvsuse Novis\Image\Color;use Novis\Image\Format;// Shows that `keepingMetadata` keeps the EXIF data in every file that `encodeVariants` writes.use Novis\Image\Image;
// This JPEG has an EXIF block, like a photo from a phone. The block has orientation 6.tainted bytes $jpeg = Image::create(400, 200, Color::hex('#336699'))->withFormat(Format::Jpeg)->encode();bytes $exif = Core\Encoding::fromHex('ffe1002245786966000049492a0008000000010012010300010000000600000000000000');array<tainted bytes> $parts = [Core\Bytes::slice($jpeg, 0, 2), $exif, Core\Bytes::slice($jpeg, 2)];tainted bytes $photo = Core\Bytes::join($parts);
Image $kept = Image::open($photo, { autoOrient: false })->keepingMetadata();array<tainted bytes> $files = $kept->encodeVariants([{ width: 200 }, { width: 100 }]);
// Both smaller files still have orientation 6. This prints 200 6 and 100 6.foreach ($files as tainted bytes $file) { var $info = Image::info($file); echo $info->width, ' ', $info->orientation, "\n";}200 6100 6