File: exif.inc.php

package info (click to toggle)
zoph 0.6-2.1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 1,716 kB
  • ctags: 2,283
  • sloc: php: 8,554; perl: 1,601; sh: 760; sql: 382; python: 338; makefile: 71
file content (198 lines) | stat: -rw-r--r-- 5,906 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/*
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


/*
 * Uses PHP's read_exif_data() function to parse EXIF headers.
 *
 * I've tried to match some of the normalizations/conversions done by jhead
 * (some code borrowed from exif.c). (License from jhead explicitly allows this).
 *
 * Jason
 */
function process_exif($image) {

    $exifdata = array();

    $exif = read_exif_data($image);

    if ($exif === false) {
        echo translate("No EXIF header found.") . "<br>\n";
        return $exifdata;
    }

    if ($exif["DateTime"]) {
        $datetime = $exif["DateTime"];
    }
    else if ($exif["DateTimeOriginal"]) {
        $datetime = $exif["DateTimeOriginal"];
    }
    else if ($exif["DateTimeDigitized"]) {
        $datetime = $exif["DateTimeDigitized"];
    }

    if (!$datetime) {
        $datetime = date ("Y-m-d H:i:s", filemtime($image));
    }
    list($date, $time) = explode(' ', $datetime);
    $date = str_replace(':', '-', $date);

    $exifdata["date"] = $date;
    $exifdata["time"] = $time;

    if ($exif["Make"]) {
        $exifdata["camera_make"] = ucwords(strtolower($exif["Make"]));
    }

    if ($exif["Model"]) {
        $exifdata["camera_model"] = ucwords(strtolower($exif["Model"]));
    }

    if (isset($exif["Flash"])) {
        /*
           bug#671023 from mail2061 <AT> deys <DOT> org

           "The code in exif.inc.php that tests $exif["Flash"] in order
           to determine whether or not the flash was fired is getting
           wrong values. My FujiFilm S602 is returning '9' for 'Fired'
           and '16' for 'Not Fired(compulsory)'. I reworked the boolean
           test into a switch statement that handles this. However, I
           suspect that this field can have additional values besides
           the two I've identified."
        */
        //$exifdata["flash_used"] = $exif["Flash"] ? "Yes" : "No";

        // Revamped to handled more expressive flash indications
        $fYN="No";

        switch ($exif["Flash"]) {

        // Flash Not Fired
        case 16:
        case 0: $fYN="No"; break;

        // Flash Fired
        case 9:
        default: $fYN="Yes"; break;
        }

        $exifdata["flash_used"] = $fYN;
    }

    if ($exif["FocalLength"]) {
        list($a, $b) = explode('/', $exif["FocalLength"]);
        $exifdata["focal_length"] = sprintf("%.1fmm", $a / $b);
    }

    if ($exif["ExposureTime"]) {
        list($a, $b) = explode('/', $exif["ExposureTime"]);
        $val = $a / $b;
        $exifdata["exposure"] = sprintf("%.3f s", $val);
        if ($val <= 0.5) {
            $exifdata["exposure"] .= sprintf("  (1/%d)", (int)(0.5 + 1 / $val));
        }
    }

    if ($exif["ExposureProgram"]) {
        $ep = $exif["ExposureProgram"];
        switch ($ep) {
        case 2:
            $exifdata["exposure"] .= " [program (auto)]";
            break;
        case 3:
            $exifdata["exposure"] .= " [aperture priority (semi-auto)]";
            break;
        case 4:
            $exifdata["exposure"] .= " [shutter priority (semi-auto)]";
            break;
        }
    }

    if ($exif["FNumber"]) {
        list($a, $b) = explode('/', $exif["FNumber"]);
        $exifdata["aperture"] = sprintf("f/%.1f", $a / $b);
    }
    else if ($exif["ApertureValue"]) {
        list($a, $b) = explode('/', $exif["ApertureValue"]);
        $exifdata["aperture"] = sprintf("f/%.1f", $a / $b * log(2) * 0.5);
    }
    else if ($exif["MaxApertureValue"]) {
        list($a, $b) = explode('/', $exif["MaxApertureValue"]);
        $exifdata["aperture"] = sprintf("f/%.1f", $a / $b * log(2) * 0.5);
    }

    if ($exif["FocusDistance"]) {
        $exifdata["focus_dist"] = $exif["FocusDistance"];
    }

    if ($exif["MeteringMode"]) {
        $mm = $exif["MeteringMode"];
        switch ($mm) {
        case 2:
            $exifdata["metering_mode"] = "center weight";
            break;
        case 3:
            $exifdata["metering_mode"] = "spot";
            break;
        case 5:
            $exifdata["metering_mode"] = "matrix";
            break;
        }
    }

    if ($exif["ISOSpeedRatings"]) {
        $a = $exif["ISOSpeedRatings"];
        if ($a < 50) { $a *= 200; }
        $exifdata["iso_equiv"] = $a;
    }

    /* something is not quite right here
    if ($exif["FocalPlaneXResolution"] && $exif["FocalPlaneResolutionUnit"]) {
        $width = $exif["ExifImageWidth"];
        list($a, $b) = explode('/', $exif["FocalPlaneXResolution"]);
        $fpxr = $a / $b;
        $fpru = $exif["FocalPlaneResolutionUnit"];

        $exifdata["ccd_width"] = sprintf("%.2fmm", $width * $fpru / $fpxr);
    }
    */

    if ($exif["CompressedBitsPerPixel"]) {
        list($a, $b) = explode('/', $exif["CompressedBitsPerPixel"]);
        $val = round($a / $b);
        switch ($val) {
        case 1:
            $exifdata["compression"] = "jpeg quality: basic";
            break;
        case 2:
            $exifdata["compression"] = "jpeg quality: normal";
            break;
        case 4:
            $exifdata["compression"] = "jpeg quality: fine";
            break;
        }
    }

    if ($exif["Comment"]) {
        $exifdata["comment"] = $exif["Comment"];
    }

    return $exifdata;
}

?>