Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Exif information out of Images.

PHP and Ghraphics

Getting Exif information out of Images.

by  Karl Blessing  Posted    (Edited  )
So you have a photo gallery, and a digital camera. Alot of people are interested to know how the shots were taken or when for that matter. (data such as shutter, aperture, focal length, iso speed, date, etc are stored inside of 99% of images shot with an exif compliant digital camera)

First off have to make sure Exif support was compiled with PHP( linux/unix ), or that the php_exif.dll extension is loaded ( windows ).

The following code pulls the basic information I usally need for my photo gallery (Camera Model, Shutter, Aperture, ISO Speed, And Focal Length)

Code:
function get_exif($filename, $gallery)
{
	if(!extension_loaded('exif'))
	{
		return "Exif Support Not Loaded";
	}
	else
	{
/* In my gallery code all my images are under the gallery folder, under a specific gallery, the path below can be changed to where you images are located. */
		$imagepath = $_SERVER['DOCUMENT_ROOT']."/gallery/".$gallery."/".$filename.".jpg";
	
	/* next line obtains the exif data from the images the second parameter can be changed to 'EXIF' but seems 'IFD0' works fine for the list of information I need */
		@$exif = exif_read_data($imagepath, 'IFD0');				
/* Because manufacture vary , the exif information provided isnt always in the same name, I check to see if at least two of the exif information is not blank, if they are then I assume no valid exif information is present. */

		if (($exif['FNumber'] == "") || ($exif['FocalLength'] == ""))
			return "No EXIF Data";
		else
		{
			@$exif = exif_read_data($imagepath, 'IFD0', true);

//uncomment the next code block, if you wish to see ALL values inside of the exif information, useful for finding the exact keys you want beforehand
			/*
			foreach ($exif as $key => $section) {
				foreach ($section as $name => $val) {
					echo "<!-- $key.$name: $val -->\n";
				}
			}
			*/

//convert the raw values to understandible values
			$Fnumber = explode("/", $exif['EXIF']['FNumber']);
			$Fnumber = $Fnumber[0] / $Fnumber[1];
			$Focal = explode("/", $exif['EXIF']['FocalLength']);
			$Focal = $Focal[0] / $Focal[1];

//prepare the text for return		
			$exif_text = "Model: ".$exif['IFD0']['Model']."<br>";
			$exif_text = $exif_text."Shutter: ".$exif['EXIF']['ExposureTime']."<br>";
			$exif_text = $exif_text."Aperture: f/".$Fnumber."<br>";
			$exif_text = $exif_text."ISO Speed: ".$exif['EXIF']['ISOSpeedRatings']."<br>";
			$exif_text = $exif_text."Focal Length: ".round($Focal)."mm<br>";
			
			return $exif_text;
		}
	}
}
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top