Please note that when resizing images with GD and most image processing scripts or applications you will loose the EXIF information. What I did as a workaround is book this information into MySQL before I re-size images.
<?php
// This function is used to determine the camera details for a specific image. It returns an array with the parameters.
function cameraUsed($imagePath) {
// Check if the variable is set and if the file itself exists before continuing
if ((isset($imagePath)) and (file_exists($imagePath))) {
// There are 2 arrays which contains the information we are after, so it's easier to state them both
$exif_ifd0 = read_exif_data($imagePath ,'IFD0' ,0);
$exif_exif = read_exif_data($imagePath ,'EXIF' ,0);
//error control
$notFound = "Unavailable";
// Make
if (@array_key_exists('Make', $exif_ifd0)) {
$camMake = $exif_ifd0['Make'];
} else { $camMake = $notFound; }
// Model
if (@array_key_exists('Model', $exif_ifd0)) {
$camModel = $exif_ifd0['Model'];
} else { $camModel = $notFound; }
// Exposure
if (@array_key_exists('ExposureTime', $exif_ifd0)) {
$camExposure = $exif_ifd0['ExposureTime'];
} else { $camExposure = $notFound; }
// Aperture
if (@array_key_exists('ApertureFNumber', $exif_ifd0['COMPUTED'])) {
$camAperture = $exif_ifd0['COMPUTED']['ApertureFNumber'];
} else { $camAperture = $notFound; }
// Date
if (@array_key_exists('DateTime', $exif_ifd0)) {
$camDate = $exif_ifd0['DateTime'];
} else { $camDate = $notFound; }
// ISO
if (@array_key_exists('ISOSpeedRatings',$exif_exif)) {
$camIso = $exif_exif['ISOSpeedRatings'];
} else { $camIso = $notFound; }
$return = array();
$return['make'] = $camMake;
$return['model'] = $camModel;
$return['exposure'] = $camExposure;
$return['aperture'] = $camAperture;
$return['date'] = $camDate;
$return['iso'] = $camIso;
return $return;
} else {
return false;
}
}
?>
An example of it's use follows:
<?php
$camera = cameraUsed("/img/myphoto.jpg");
echo "Camera Used: " . $camera['make'] . " " . $camera['model'] . "<br />";
echo "Exposure Time: " . $camera['exposure'] . "<br />";
echo "Aperture: " . $camera['aperture'] . "<br />";
echo "ISO: " . $camera['iso'] . "<br />";
echo "Date Taken: " . $camera['date'] . "<br />";
?>
Will display the following, depending on the data:
Camera Used: SONY DSC-S930
Exposure Time: 1/400
Aperture: f/4.3
ISO: 100
Date Taken: 2010:12:10 18:18:45
If the image has been re-sized and the information is no longer available then you should receive the following when echoing the same:
Camera Used: Unavailable
Exposure Time: Unavailable
Aperture: Unavailable
ISO: Unavailable
Date Taken: Unavailable
Some cameras do not capture all the information, for instance Blackberry phones do not record an aperture, or iso and you will get Unavailable for those fields.
I hope you find this helpful.
exif_read_data
(PHP 4 >= 4.2.0, PHP 5)
exif_read_data — Liest die EXIF-Header von JPEG oder TIFF aus
Beschreibung
$filename
[, string $sections
[, bool $arrays
[, bool $thumbnail
]]] )exif_read_data() liest die EXIF-Header aus einer JPEG- oder TIFF-Bilddatei aus. Auf diese Weise kann man die Metadaten auslesen, die mit Digitalkameras erzeugt wurden.
Exif-Header kommen normalerweise bei JPEG/TIFF-Bildern vor, die von Digitalkameras gemacht wurden. Leider hat jeder Kamerahersteller eine andere Vorstellung davon, wie man die Bilder beschreibt. Man kann sich also nicht darauf verlassen, das ein bestimmter Exif-Header vorhanden ist.
Height und Width werden genauso wie getimagesize() berechnet. Diese Werte dürfen also nicht Teil von irgendwelchen Headern sein, die zurückgegeben werden. Ferner ist html ein Höhen/Breiten-Textstring für den Gebrauch in normalem HTML.
Wenn ein Exif-Header einen Copyrightvermerk beinhaltet, kann dieser selbst aus zwei Werten bestehen. Da die Lösung im Exif-2.10 -tandard nicht konsistent ist, liefert der COMPUTED-Bereich die beiden Einträge Copyright.Photographer und Copyright.Editor, während der IFD0-Bereich aus einem Byte-Array besteht, der die beiden Werte duch ein NULL-Zeichen trennt. Wenn der Datentyp falsch ist, ist nur der erste Eintrag vorhanden (normales Verhalten von Exif). COMPUTED beinhaltet auch ein Copyright-Eintrag. Dieser entspricht entweder dem originalen Copyright-String oder er besteht aus einer kommaseparierten Liste von Foto- und Herausgeber-Copyright.
Der Tag UserComment besitzt das gleiche Problem, wie das Copyright-Tag. Er kann zwei Werte speichern. Als erstes die benutze Kodierung und als zweites den Wert selbst. Wenn dem so ist, enthält der IFD-Bereich die Kodierung oder ein Byte-Array. Der COMPUTED-Bereich speichert die beiden Einträge UserCommentEncoding und UserComment. Der Eintrag UserComment ist in beiden Fällen verfügbar. Er sollte also dem Eintrag im IFD0-Bereich vorgezogen werden.
exif_read_data() validiert auch EXIF-Datentags entsprechend der EXIF-Spezifikationen (» http://exif.org/Exif2-2.PDF, Seite 20).
Hinweis:
Windows ME/XP können beide beim Anschließen einer Kamera die Exif-Header verwerfen. Mehr Informationen dazu sind unter » http://www.canon.co.jp/Imaging/NOTICE/011214-e.html zu finden.
Parameter-Liste
-
filename -
Der Name der zu lesenden Bilddatei. Dieser darf keine URL sein.
-
sections -
Ist eine kommaseparierte List von Bereichen, die in der Datei vorhanden sein muss, um das Rückgabe-array zu erzeugen. Wenn keine der geforderten Bereiche gefunden werden kann, wird
FALSEzurückgegeben.FILE FileName, FileSize, FileDateTime, SectionsFound COMPUTED html, Width, Height, IsColor und andere, wenn vorhanden. Height und Width werden genauso wie getimagesize() berechnet. Diese Werte dürfen also nicht Teil von irgendwelchen Headern sein, die zurückgegeben werden. Ferner ist html ein Höhen/Breiten-Textstring für den Gebrauch in normalem HTML. ANY_TAG Irgendwelche Informationen, die ein Tag besitzen, wie z.B. IFD0, EXIF, ... IFD0 Alle IFD0-Daten mit Tag. In normalen Bilddateien beinhalten diese Bildgröße usw. THUMBNAIL Eine Datei sollte ein Miniaturbild enthalten, wenn es einen zweiten IFD besitzt. Alle Informationen mit Tags über das eingebettete Miniaturbild werden in diesem Bereich gespeichert. COMMENT Kommentarheader des JPEG-Bildes. EXIF Der EXIF-Bereich ist ein Unterbereich von IFD0. Er enthält detailliertere Information über das Bild. Die meisten dieser Einträge beziehen sich auf die Digitalkamera. -
arrays -
Definiert ob jeder Bereich ein Array wird oder nicht. Die
sectionsCOMPUTED, THUMBNAIL und COMMENT werden immer zu Arrays, da die Namen der Werte mit denen anderer Bereiche kollidieren können. -
thumbnail -
Bei
TRUEwird das Miniaturbild ausgelesen, ansonsten nur die Daten der Tags.
Rückgabewerte
Gibt ein assoziatives array zurück, bei dem der Arrayindex den
Headernamen entspricht und der Arraywert die Werte enthält, die mit diesen
Headern in Verbindung stehen. Wenn keine Daten zurückgeliefert werde können,
liefert exif_read_data() FALSE.
Changelog
| Version | Beschreibung |
|---|---|
| 4.3.0 | Kann alle eingebetteten IFD-Daten inklusive Arrays (werden auch als solche zurückgegeben) lesen. Sowohl die Größe des eingebetteten Miniaturbildes wird im Unterbereich von THUMBNAIL zurückgegeben als auch Miniaturbilder im TIFF-Format. Es gibt auch keine Längenbeschränkung mehr für die Rückgabewerte (solange das Speicherlimit nicht erreicht wird). |
| 4.3.0 | Wenn PHP mbstring unterstützt, können die Benutzerkommentare automatisch umkodiert werden. Benutzerkommentare in Unicode oder JIS werden automatisch in den entsprechenden exif-Initwert der php.ini umkodiert. |
| 4.3.0 | Wenn das Bild irgendeinen IFD0-Wert hat, enthält der Bereich COMPUTED einen Eintrag ByteOrderMotorola, welcher auf 0 für little-endian (intel) und 1 für big-endian (motorola) Bytefolge gesetzt ist. Bei einem falschen Datentyp, enthalten COMPUTED und UserComment auch nicht mehr nur den ersten Copyrighteintrag. |
Beispiele
Beispiel #1 exif_read_data()-Beispiel
<?php
echo "test1.jpg:<br />\n";
$exif = exif_read_data('tests/test1.jpg', 'IFD0');
echo $exif===false ? "Keine Headerdaten gefunden.<br />\n" : "Bild beinhaltet Header<br />\n";
$exif = exif_read_data('tests/test2.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
?>
Der erste Aufruf schlägt fehl, da das Bild keine Headerinformationen hat.
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
test1.jpg: Keine Headerdaten gefunden. test2.jpg: FILE.FileName: test2.jpg FILE.FileDateTime: 1017666176 FILE.FileSize: 1240 FILE.FileType: 2 FILE.SectionsFound: ANY_TAG, IFD0, THUMBNAIL, COMMENT COMPUTED.html: width="1" height="1" COMPUTED.Height: 1 COMPUTED.Width: 1 COMPUTED.IsColor: 1 COMPUTED.ByteOrderMotorola: 1 COMPUTED.UserComment: Exif test image. COMPUTED.UserCommentEncoding: ASCII COMPUTED.Copyright: Photo (c) M.Boerger, Edited by M.Boerger. COMPUTED.Copyright.Photographer: Photo (c) M.Boerger COMPUTED.Copyright.Editor: Edited by M.Boerger. IFD0.Copyright: Photo (c) M.Boerger IFD0.UserComment: ASCII THUMBNAIL.JPEGInterchangeFormat: 134 THUMBNAIL.JPEGInterchangeFormatLength: 523 COMMENT.0: Comment #1. COMMENT.1: Comment #2. COMMENT.2: Comment #3end THUMBNAIL.JPEGInterchangeFormat: 134 THUMBNAIL.Thumbnail.Height: 1 THUMBNAIL.Thumbnail.Height: 1
Siehe auch
- exif_thumbnail() - Aufruf des eingebetteten Miniaturbildes eines TIFF- oder JPEG-Bildes
- getimagesize() - Ermittelt die Größe einer Grafik
From - darkain at darkain dot com 's example.
If all the data is from the same image - simply
<?php
$exif_data = exif_read_data ( $_FILES['photo']
$emake =$exif_data['Make'];
$emodel = $exif_data['Model'];
$eexposuretime = $exif_data['ExposureTime'];
$efnumber = $exif_data['FNumber'];
$eiso = $exif_data['ISOSpeedRatings'];
$edate = $exif_data['DateTime'];
?>
will work, I tried using the PEL library, and while pretty cool, I can't for the life understand how to call some things, this is simpler if your system is pretty basic or if you're in a rush. If you have time, try playing with PEL. It's not maintained at the moment though..
http://pel.sourceforge.net/
Get some EXIFs fields (easy way):
<?php
$exif_make = exif_read_data ( $_FILES['photo']['tmp_name'] ,'IFD0' ,0 );
$emake = $exif_make['Make'];
$exif_model = exif_read_data ( $_FILES['photo']['tmp_name'] ,'IFD0' ,0 );
$emodel = $exif_model['Model'];
$exif_exposuretime = exif_read_data ( $_FILES['photo']['tmp_name'] ,'EXIF' ,0 );
$eexposuretime = $exif_exposuretime['ExposureTime'];
$exif_fnumber = exif_read_data ( $_FILES['photo']['tmp_name'] ,'EXIF' ,0 );
$efnumber = $exif_fnumber['FNumber'];
$exif_iso = exif_read_data ( $_FILES['photo']['tmp_name'] ,'EXIF' ,0 );
$eiso = $exif_iso['ISOSpeedRatings'];
$exif_date = exif_read_data ( $_FILES['photo']['tmp_name'] ,'IFD0' ,0 );
$edate = $exif_date['DateTime'];
?>
Using the exif methods to read WINXP data returns unexpected results unless both exif and mbstring are compiled statically. Please reference the following bug reports:
Bug #31980
Bug #23105
Specifically, the last comment on #23105:
"[8 Apr 2003 4:26pm UTC] edink@php.net
This cannot be fixed due to the fact that mbstring has been removed from PHP core (it has been 'unbundled') and the rest of core files and other extensions cannot use mbstring functionality when it is compiled as a shared library (dll).
"
If exif is compiled statically (--enable-exif) and mbstring compiled as a DSO module (--enable-mbstring=shared) then exif_read_data may only return a single character rather than the entire string.
Compiling both exif and mbstring statically (--enable-exif --enable-mbstring) resolves the issue.
When the new update came out from Apple for iOS6 it provided the ability for iPad, iPod, and iPhones to be able to upload files from the device through Safari. Obviously this will open up an array of implementations where at one point it was just not possible.
The issue comes when a photo is uploaded it will be dependent on the location of the "button" when the photo was taken. Imagine if you will that you have your iPhone turned with the button at the top and you take a photo. The photo when uploaded to your server might be "upside down".
The following code will ensure that all uploaded photos will be oriented correctly upon upload:
<?php
$image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name']));
$exif = exif_read_data($_FILES['image_upload']['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
// $image now contains a resource with the image oriented correctly
?>
What you do with the image resource from there is entirely up to you.
I hope that this helps you identify and orient any image that's uploaded from an iPad, iPhone, or iPod. Orientation for the photo is the key to knowing how to rotate it correctly.
I wanted some quick and easy functions for computing the shutter speed and f-stop. I couldn't find any anywhere, so I made some. It took some research :
<?php
function exif_get_float($value) {
$pos = strpos($value, '/');
if ($pos === false) return (float) $value;
$a = (float) substr($value, 0, $pos);
$b = (float) substr($value, $pos+1);
return ($b == 0) ? ($a) : ($a / $b);
}
function exif_get_shutter(&$exif) {
if (!isset($exif['ShutterSpeedValue'])) return false;
$apex = exif_get_float($exif['ShutterSpeedValue']);
$shutter = pow(2, -$apex);
if ($shutter == 0) return false;
if ($shutter >= 1) return round($shutter) . 's';
return '1/' . round(1 / $shutter) . 's';
}
function exif_get_fstop(&$exif) {
if (!isset($exif['ApertureValue'])) return false;
$apex = exif_get_float($exif['ApertureValue']);
$fstop = pow(2, $apex/2);
if ($fstop == 0) return false;
return 'f/' . round($fstop,1);
}
?>
This is function, resize image and don't rotates images have exif info
PHP must be enabled:
extension=php_mbstring.dll
extension=php_exif.dll
<?php
function CreateThumbnail($pic,$thumb,$thumbwidth, $quality = 100)
{
$im1=ImageCreateFromJPEG($pic);
//if(function_exists("exif_read_data")){
$exif = exif_read_data($pic);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$im1 = imagerotate($im1,90,0);
break;
case 3:
$im1 = imagerotate($im1,180,0);
break;
case 6:
$im1 = imagerotate($im1,-90,0);
break;
}
}
//}
$info = @getimagesize($pic);
$width = $info[0];
$w2=ImageSx($im1);
$h2=ImageSy($im1);
$w1 = ($thumbwidth <= $info[0]) ? $thumbwidth : $info[0] ;
$h1=floor($h2*($w1/$w2));
$im2=imagecreatetruecolor($w1,$h1);
imagecopyresampled ($im2,$im1,0,0,0,0,$w1,$h1,$w2,$h2);
$path=addslashes($thumb);
ImageJPEG($im2,$path,$quality);
ImageDestroy($im1);
ImageDestroy($im2);
}
?>
Photos processed in Picasa often contain garbage data in the "MAKERNOTE" section and under EXIF.MakerNote, (UTF8) like:
[MakerNote] => r0~Þæ"î2OÔy e §…b! ) ) EI "ÐÓ
#s&0{'Û(å-Ð`ÿÿ@ÿÿÿìEè€Ýÿÿ ÿÿÿÿÿÿx "ú»Dóÿ H ?.}BúIMG:DIGITAL IXUS 100 IS JPEGFirmware Version 1.00s›xÇØÿÿÿ–l¥ÿÿÿ ØÌÌxŒ ÿÿÌÌŸãÿÿÿ¼Ž(½(½T‹U’‹d–~Ø“¥ÿÿÿ ÀÿœªãjáÀpgaXfaWb[Te«
8ú5:Áð-3åÿÿ5»ÿ ‹;ßÊ Š €à€` ¸ddîÿîÿîÿîÿîÿîÿ
ÿÿŠ1—Ïàôɿ׬gªiï
This can't be written to Blob in MySql. The following code removes the garbage tags.
$exif = exif_read_data($process_photo, 0, 'EXIF');
if($exif['IFD0']['Software'] == "Picasa"){
foreach ($exif as $key => $section){
if($key != "MAKERNOTE"){
foreach ($section as $name => $val){
if($name != 'MakerNote'){
$exifA[$key][$name] = $val;
}
}
$exifB[$key] = $exifA[$key];
}
}
$serialized_exif = serialize ($exifB);
}else{
$serialized_exif = serialize ($exif);
}
some cameras (most higher models) have position senzor (gyroskope?) and taking-position is wrote in EXIF, here is simple script for automatic rotating images
<?php
$exif = exif_read_data($filename);
$ort = $exif['IFD0']['Orientation'];
switch($ort)
{
case 1: // nothing
break;
case 2: // horizontal flip
$image->flipImage($public,1);
break;
case 3: // 180 rotate left
$image->rotateImage($public,180);
break;
case 4: // vertical flip
$image->flipImage($public,2);
break;
case 5: // vertical flip + 90 rotate right
$image->flipImage($public, 2);
$image->rotateImage($public, -90);
break;
case 6: // 90 rotate right
$image->rotateImage($public, -90);
break;
case 7: // horizontal flip + 90 rotate right
$image->flipImage($public,1);
$image->rotateImage($public, -90);
break;
case 8: // 90 rotate left
$image->rotateImage($public, 90);
break;
}
?>
$image->rotateImage() is inspired by example of http://php.net/manual/en/function.imagerotate.php
$image->flipImage() is inspired by http://php.net/manual/en/function.imagecopy.php#42803 (thank you)
When reading EXIF information from the 'WINXP' group, you may need to change used encoding from the default "ISO-8859-15" to "UTF-8". This can be done in php.ini or in your code:
<?php
ini_set('exif.encode_unicode', 'UTF-8');
$exif = exif_read_data('TEST.JPG', 0, true);
echo $exif['WINXP']['Title'];
?>
Useful documentation about EXIF:
http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
See also comments next to XPTitle and XPAuthor.
I've written a library in pure PHP5 for editing EXIF tags. It deals with both reading and writing EXIF tags, and can be downloaded from http://pel.sourceforge.net/
Exif is very unstable under php4.1.2
If you have some problem, (the function didnt return anything, like a blocking call) try this:
$file = './image.jpg';
getimagesize ( $file , $info);
$exif = array();
if (isset($info)) {
foreach($info as $key => $val) {
if ($key != 'APP1') { $exif = read_exif_data($file); break; }
}
}
-- Sharp
For reading EXIF from XMP data embedded by Adobe Photoshop CS, see http://www.photography-on-the.net/ee/beta/cs_xmp_to_exif.php
I've just released the "PHP JPEG Metadata Toolkit" which allows reading, writing and displaying of EXIF information, and does not need the --enable-exif option in PHP4.
It has been tested on 466 different models of digital cameras!
It can decode the following EXIF makernotes:
Agfa, Canon, Casio, Contax, Epson, Fujifilm, Konica, Minolta, Kyocera, Nikon, Olympus, Panasonic, Pentax (Asahi), Ricoh and Sony
Additionaly it can decode IPTC, XMP, Photoshop IRB and many other types of JPEG metadata
Try it out, and download it at:
http://www.ozhiker.com/electronics/pjmt/index.html
