Skip to content

Instantly share code, notes, and snippets.

@sp3c73r2038
Created October 30, 2012 04:51
Show Gist options
  • Save sp3c73r2038/3978372 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/3978372 to your computer and use it in GitHub Desktop.
php using PEL manipulate the exif data
<?php
error_reporting(E_ALL);
$root_path = dirname(__FILE__);
$lib_path = $root_path . "/lib/";
include_once($lib_path . 'PelJpeg.php');
include_once($lib_path . 'PelTag.php');
include_once($lib_path . 'PelIfd.php');
include_once($lib_path . 'PelDataWindow.php');
$im = imagecreate(1200, 800);
// create PelJpeg object from gd resource, or filename
$jpeg = new PelJpeg($im);
// create PelExif object
$exif = new PelExif();
// bind the PelExif object to PelJpeg
$jpeg->setExif($exif);
// crate PelTiff object
$tiff = new PelTiff();
// the exif info is actually stored in PelTiff
$exif->setTiff($tiff);
// create IFD tag and add entry
$ifd0 = new PelIfd(PelIfd::IFD0);
$tiff->setIfd($ifd0);
$ifd0->addEntry(new PelEntryAscii(PelTag::IMAGE_DESCRIPTION, "hello world!!!"));
// output the file
file_put_contents("new.jpg", $jpeg->getBytes());
// read back and check
$exif = exif_read_data("new.jpg");
var_dump($exif);
/*
sample output
array(9) {
["FileName"]=>
string(7) "new.jpg"
["FileDateTime"]=>
int(1351572944)
["FileSize"]=>
int(15739)
["FileType"]=>
int(2)
["MimeType"]=>
string(10) "image/jpeg"
["SectionsFound"]=>
string(22) "ANY_TAG, IFD0, COMMENT"
["COMPUTED"]=>
array(5) {
["html"]=>
string(25) "width="1200" height="800""
["Height"]=>
int(800)
["Width"]=>
int(1200)
["IsColor"]=>
int(1)
["ByteOrderMotorola"]=>
int(0)
}
["ImageDescription"]=>
string(14) "hello world!!!"
["COMMENT"]=>
array(1) {
[0]=>
string(57) "CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), quality = 75
"
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment