Return to Snippet

Revision: 25122
at March 20, 2010 22:15 by frankyfish


Updated Code
function getImageInfo($img) {
	$ret = array("title"=>"","description"=>"");
	if(!file_exists($img)) return $ret;
	$size = getimagesize($img, $info);
	if(!isset($info['APP13'])) return $ret;
	$iptc = iptcparse($info['APP13']);
	
	if(is_array($iptc["2#025"]) && count($iptc["2#025"])>0) $ret["tag"] = implode(", ", $iptc["2#025"]);
	if($iptc["2#005"][0] != null) $ret["title"] = $iptc["2#005"][0];
	if($iptc["2#080"][0] != null) $ret["author"] = $iptc["2#080"][0];
	if($iptc["2#085"][0] != null) $ret["credit"] = $iptc["2#085"][0];
	if($iptc["2#090"][0] != null) $ret["city"] = $iptc["2#090"][0];
	if($iptc["2#095"][0] != null) $ret["state"] = $iptc["2#095"][0];
	if($iptc["2#101"][0] != null) $ret["country"] = $iptc["2#101"][0];
	if($iptc["2#105"][0] != null) $ret["title"] = $iptc["2#105"][0];
	if($iptc["2#116"][0] != null) $ret["copyright"] = $iptc["2#116"][0];
	if($iptc["2#120"][0] != null) $ret["description"] = $iptc["2#120"][0];
	if($iptc["2#122"][0] != null) $ret["author"] = $iptc["2#122"][0];
	
	$xmp = getImageXMP($img);
	foreach($xmp as $key => $value) {
		if($value != null && $value != "") $ret[$key] = $value;
	}
	return $ret;
}

function getImageXMP($filename) {
    $file = fopen($filename, 'r');
		$source = fread($file, filesize($filename));
    	$xmpdata_start = strpos($source,"<x:xmpmeta");
    	$xmpdata_end = strpos($source,"</x:xmpmeta>");
    	$xmplenght = $xmpdata_end-$xmpdata_start;
    	$xmpdata = substr($source,$xmpdata_start,$xmplenght+12);
	fclose($file);
    $xmp_parsed = array();
    $regexps = array(
		array("name" => "copyright", "regexp" => "/<dc:rights>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:rights>/"),
	    array("name" => "author", "regexp" => "/<dc:creator>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/dc:creator>/"),
		array("name" => "title", "regexp" => "/<dc:title>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:title>/"),
		array("name" => "description", "regexp" => "/<dc:description>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:description>/"),
	    array("name" => "camera model", "regexp" => "/tiff:Model=\"(.[^\"]+)\"/"),
	    array("name" => "maker", "regexp" => "/tiff:Make=\"(.[^\"]+)\"/"),
		array("name" => "width", "regexp" => "/tiff:ImageWidth=\"(.[^\"]+)\"/"),
		array("name" => "height", "regexp" => "/tiff:ImageLength=\"(.[^\"]+)\"/"),
	    array("name" => "exposure time", "regexp" => "/exif:ExposureTime=\"(.[^\"]+)\"/"),
	    array("name" => "f number", "regexp" => "/exif:FNumber=\"(.[^\"]+)\"/"),
	    array("name" => "iso", "regexp" => "/<exif:ISOSpeedRatings>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/exif:ISOSpeedRatings>/"),
	    array("name" => "focal lenght", "regexp" => "/exif:FocalLength=\"(.[^\"]+)\"/"),
		array("name" => "user comment", "regexp" => "/<exif:UserComment>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/exif:UserComment>/"),
		array("name" => "datetime original", "regexp" => "/xmp:CreateDate=\"(.[^\"]+)\"/"),
	    array("name" => "lens", "regexp" => "/aux:Lens=\"(.[^\"]+)\"/")
    );
    foreach ($regexps as $key => $k) {
		unset($r);
		preg_match ($k["regexp"], $xmpdata, $r);
		$xmp_item = @$r[1];
		if(in_array($k["name"], array("f number", "focal lenght"))) eval("\$xmp_item = ".$xmp_item.";");
		$xmp_parsed[$k["name"]] = str_replace("&#xA;", "\n", $xmp_item);
    }
	return $xmp_parsed;
}

Revision: 25121
at March 20, 2010 21:11 by frankyfish


Updated Code
function getImageInfo($img) {
	$ret = array("title"=>"","description"=>"");
	if(!file_exists($img)) return $ret;
	$size = getimagesize($img, $info);
	if(!isset($info['APP13'])) return $ret;
	$iptc = iptcparse($info['APP13']);
	if($iptc["2#005"][0] != null) $ret["title"] = $iptc["2#005"][0];
	if($iptc["2#105"][0] != null) $ret["title"] = $iptc["2#005"][0];
	if($iptc["2#120"][0] != null) $ret["description"] = $iptc["2#120"][0];
	
	$xmp = getImageXMP($img);
	if($xmp[2]["value"] != "") $ret["title"] = $xmp[2]["value"];
	if($xmp[3]["value"] != "") $ret["description"] = $xmp[3]["value"];
	return $ret;
}

function getImageXMP($filename) {
    $file = fopen($filename, 'r');
	$source = fread($file, filesize($filename));
    	$xmpdata_start = strpos($source,"<x:xmpmeta");
    	$xmpdata_end = strpos($source,"</x:xmpmeta>");
    	$xmplenght = $xmpdata_end-$xmpdata_start;
    	$xmpdata = substr($source,$xmpdata_start,$xmplenght+12);
	fclose($file);
    $xmp_parsed = array();
    $regexps = array(
	array("name" => "rights", "regexp" => "/<dc:rights>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:rights>/"),
	array("name" => "creator", "regexp" => "/<dc:creator>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/dc:creator>/"),
	array("name" => "title", "regexp" => "/<dc:title>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:title>/"),
	array("name" => "description", "regexp" => "/<dc:description>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:description>/"),
	array("name" => "camera model", "regexp" => "/tiff:Model=\"(.[^\"]+)\"/"),
	array("name" => "maker", "regexp" => "/tiff:Make=\"(.[^\"]+)\"/"),
	array("name" => "width", "regexp" => "/tiff:ImageWidth=\"(.[^\"]+)\"/"),
	array("name" => "height", "regexp" => "/tiff:ImageLength=\"(.[^\"]+)\"/"),
	array("name" => "exposure time", "regexp" => "/exif:ExposureTime=\"(.[^\"]+)\"/"),
	array("name" => "f number", "regexp" => "/exif:FNumber=\"(.[^\"]+)\"/"),
	array("name" => "iso", "regexp" => "/<exif:ISOSpeedRatings>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/exif:ISOSpeedRatings>/"),
	array("name" => "focal lenght", "regexp" => "/exif:FocalLength=\"(.[^\"]+)\"/"),
	array("name" => "user comment", "regexp" => "/<exif:UserComment>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/exif:UserComment>/"),
	array("name" => "datetime original", "regexp" => "/xmp:CreateDate=\"(.[^\"]+)\"/"),
	array("name" => "lens", "regexp" => "/aux:Lens=\"(.[^\"]+)\"/")
    );
    foreach ($regexps as $key => $k) {
	unset($r);
	preg_match ($k["regexp"], $xmpdata, $r);
	$xmp_item = @$r[1];
	if(in_array($k["name"], array("f number", "focal lenght"))) eval("\$xmp_item = ".$xmp_item.";");
	array_push($xmp_parsed,array("item" => $k["name"], "value" => str_replace("&#xA;", "\n", $xmp_item)));
    }
    return ($xmp_parsed);
}

Revision: 25120
at March 20, 2010 21:10 by frankyfish


Initial Code
function getImageInfo($img) {
	$ret = array("title"=>"","description"=>"");
	if(!file_exists($img)) return $ret;
	$size = getimagesize($img, $info);
	if(!isset($info['APP13'])) return $ret;
	$iptc = iptcparse($info['APP13']);
	if($iptc["2#005"][0] != null) $ret["title"] = $iptc["2#005"][0];
	if($iptc["2#105"][0] != null) $ret["title"] = $iptc["2#005"][0];
	if($iptc["2#120"][0] != null) $ret["description"] = $iptc["2#120"][0];
	
	$xmp = getImageXMP($img);
	if($xmp[2]["value"] != "") $ret["title"] = $xmp[2]["value"];
	if($xmp[3]["value"] != "") $ret["description"] = $xmp[3]["value"];
	return $ret;
}

function getImageXMP($filename) {
    $file = fopen($filename, 'r');
		$source = fread($file, filesize($filename));
    	$xmpdata_start = strpos($source,"<x:xmpmeta");
    	$xmpdata_end = strpos($source,"</x:xmpmeta>");
    	$xmplenght = $xmpdata_end-$xmpdata_start;
    	$xmpdata = substr($source,$xmpdata_start,$xmplenght+12);
	fclose($file);
    $xmp_parsed = array();
    $regexps = array(
		array("name" => "rights", "regexp" => "/<dc:rights>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:rights>/"),
	    array("name" => "creator", "regexp" => "/<dc:creator>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/dc:creator>/"),
		array("name" => "title", "regexp" => "/<dc:title>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:title>/"),
		array("name" => "description", "regexp" => "/<dc:description>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:description>/"),
	    array("name" => "camera model", "regexp" => "/tiff:Model=\"(.[^\"]+)\"/"),
	    array("name" => "maker", "regexp" => "/tiff:Make=\"(.[^\"]+)\"/"),
		array("name" => "width", "regexp" => "/tiff:ImageWidth=\"(.[^\"]+)\"/"),
		array("name" => "height", "regexp" => "/tiff:ImageLength=\"(.[^\"]+)\"/"),
	    array("name" => "exposure time", "regexp" => "/exif:ExposureTime=\"(.[^\"]+)\"/"),
	    array("name" => "f number", "regexp" => "/exif:FNumber=\"(.[^\"]+)\"/"),
	    array("name" => "iso", "regexp" => "/<exif:ISOSpeedRatings>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/exif:ISOSpeedRatings>/"),
	    array("name" => "focal lenght", "regexp" => "/exif:FocalLength=\"(.[^\"]+)\"/"),
		array("name" => "user comment", "regexp" => "/<exif:UserComment>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/exif:UserComment>/"),
		array("name" => "datetime original", "regexp" => "/xmp:CreateDate=\"(.[^\"]+)\"/"),
	    array("name" => "lens", "regexp" => "/aux:Lens=\"(.[^\"]+)\"/")
    );
    foreach ($regexps as $key => $k) {
		unset($r);
		preg_match ($k["regexp"], $xmpdata, $r);
		$xmp_item = @$r[1];
		if(in_array($k["name"], array("f number", "focal lenght"))) eval("\$xmp_item = ".$xmp_item.";");
		array_push($xmp_parsed,array("item" => $k["name"], "value" => str_replace("&#xA;", "\n", $xmp_item)));
    }
	return ($xmp_parsed);
}

Initial URL

                                

Initial Description
Currenty getImagInfo() also call getImageXMP() so you only need the first function to get your infos.

Initial Title
Image Metadata Information EXIF AND XMP

Initial Tags
image

Initial Language
PHP