aboutsummaryrefslogtreecommitdiff
path: root/lib/exif.php
blob: 8ca0dde0c39cc26ce81e283d0b30e98c7b33abb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
 * Exif Processing Library
 *
 * @package TidypicsExif
 */

/**
 * Pull EXIF data from image file
 * 
 * @param TidypicsImage $image
 */
function td_get_exif($image) {

	// catch for those who don't have exif module loaded
	if (!is_callable('exif_read_data')) {
		return;
	}

	$mime = $image->mimetype;
	if ($mime != 'image/jpeg' && $mime != 'image/pjpeg') {
		return;
	}

	$filename = $image->getFilenameOnFilestore();
	$exif = exif_read_data($filename, 'IFD0,EXIF', true);
	if (is_array($exif)) {
		$data = array_merge($exif['IFD0'], $exif['EXIF']);
		foreach ($data as $key => $value) {
			if (is_string($value)) {
				// there are sometimes unicode characters that cause problems with serialize
				$data[$key] = preg_replace( '/[^[:print:]]/', '', $value);
			}
		}
		$image->tp_exif = serialize($data);
	}
}

/**
 * Grab array of EXIF data for display
 * 
 * @param TidypicsImage $image
 * @return array|false
 */
function tp_exif_formatted($image) {

	$exif = $image->tp_exif;
	if (!$exif) {
		return false;
	}

	$exif = unserialize($exif);

	$model = $exif['Model'];
	if (!$model) {
		$model = "N/A";
	}
	$exif_data['Model'] = $model;

	$exposure = $exif['ExposureTime'];
	if (!$exposure) {
		$exposure = "N/A";
	}
	$exif_data['Shutter'] = $exposure;

	//got the code snippet below from http://www.zenphoto.org/support/topic.php?id=17
	//convert the raw values to understandible values
	$Fnumber = explode("/", $exif['FNumber']);
	if ($Fnumber[1] != 0) {
		$Fnumber = $Fnumber[0] / $Fnumber[1];
	} else {
		$Fnumber = 0;
	}
	if (!$Fnumber) {
		$Fnumber = "N/A";
	} else {
		$Fnumber = "f/$Fnumber";
	}
	$exif_data['Aperture'] = $Fnumber;

	$iso = $exif['ISOSpeedRatings'];
	if (!$iso) {
		$iso = "N/A";
	}
	$exif_data['ISO Speed'] = $iso;

	$Focal = explode("/", $exif['FocalLength']);
	if ($Focal[1] != 0) {
		$Focal = $Focal[0] / $Focal[1];
	} else {
		$Focal = 0;
	}
	if (!$Focal || round($Focal) == "0") {
		$Focal = 0;
	}
	if (round($Focal) == 0) {
		$Focal = "N/A";
	} else {
		$Focal = round($Focal) . "mm";
	}
	$exif_data['Focal Length'] = $Focal;

	$captured = $exif['DateTime'];
	if (!$captured) {
		$captured = "N/A";
	}
	$exif_data['Captured'] = $captured;

	return $exif_data;
}