xref: /PHP-7.0/ext/exif/test.txt (revision 3c47fb91)
1<?php
2
3/* Test script for PHP module ext/exif
4 *
5 * (c) Marcus Boerger, 2002
6 *
7 * $Id$
8 *
9 * Rename the file to test.php and read the instructions. If the
10 * script cannot be executed or does not generate any output check
11 * you error log. In most cases this would mean you found an error
12 * if the rest of your php environment works fine.
13 *
14 * The original version of module exif has many errors and mostly
15 * fails on executing this script.
16 */
17
18$file = array_key_exists('thumbnail',$_REQUEST) ? $_REQUEST['thumbnail'] : '';
19//$file = '/t/temp/kodak-dc4800.tif';
20//$file = '/t/temp/canon-ixus.jpg';
21//$file = '/t/temp/test2.jpg';
22if ( $file) {
23	$image = exif_thumbnail($file);
24	if ( $image!==false) {
25    	@Header("content-type: image/jpeg");
26		echo $image;
27	} else {
28		echo "<html><body><table>\n";
29		echo "Thumbnail could not be extracted.\n";
30		echo "</table></body></html>";
31	}
32	die();
33}
34
35if ( !defined('IMAGETYPE_GIF'))     define('IMAGETYPE_GIF',1);
36if ( !defined('IMAGETYPE_JPEG'))    define('IMAGETYPE_JPEG',2);
37if ( !defined('IMAGETYPE_TIFF_II')) define('IMAGETYPE_TIFF_II',7);
38if ( !defined('IMAGETYPE_TIFF_MM')) define('IMAGETYPE_TIFF_MM',8);
39
40$possible = array();
41
42/****************************************************************************/
43// message function is used for debugging purpose: just to se what happens
44function message($msg) {
45	error_log($msg,0);
46	echo "$msg\n";
47}
48
49function error_msg() {
50	$ret = '<b style="color:green">O.K.</b>';
51	if (array_key_exists('php_errormsg',$GLOBALS) && strlen($GLOBALS['php_errormsg'])) {
52		$ret = '<b style="color:red">'.$GLOBALS['php_errormsg'].'</b>';
53		$GLOBALS['php_errormsg'] = '';
54	}
55	return $ret;
56}
57
58/****************************************************************************/
59// private to function search_file()
60function _search_file($root,&$possible,$path='') {
61	$sub = array();
62	$cnt = 0;
63	$type= false;
64
65	//error_log("search_file($root,$path)",0);
66	if ($dir = @opendir($root.$path.'/')) {
67		while (($found = @readdir($dir)) !== false) {
68			$type = @filetype($root.$path.'/'.$found);
69			//error_log("search_file($root$path):$type=$found",0);
70			switch( $type) {
71				case 'file':
72					$pos  = strrpos($found,'.');
73					if ( function_exists('exif_imagetype')) {
74						$type = exif_imagetype($root.$path.'/'.$found);
75					} else {
76						if ( $pos!==false) {
77							$type = GetImageSize($root.$path.'/'.$found);
78							if ( is_array($type)) {
79								$type = $type[2];
80							} else {
81								$type = false;
82							}
83						} else $type = false;
84					}
85					if ( $type!==false)
86					{
87						$possible[] = array('file'=>$root.$path.'/'.$found, 'type'=>$type);
88						//error_log("search_file($root$path) add:$path/$found",0);
89						if ( ($cnt=count($possible)) % 100 == 0) {
90							error_log("exif test page - counting files: $cnt",0);
91						}
92					}
93					break;
94				case 'dir':
95					if ( $found!='.' && $found!='..') {
96						$sub[count($sub)] = $found;
97					}
98					break;
99			}
100		}
101		@closedir($dir);
102		foreach( $sub as $idx => $found) {
103			_search_file($root,$possible,$path.'/'.$found);
104		}
105	}
106}
107
108/****************************************************************************/
109// function: search_file($file,$ext)
110//
111// Searches for $file in document tree. The path is ignored.
112//
113function search_file() {
114	global $argc, $argv;
115	$possible = array();
116
117	if ( $argc > 1) {
118		$path = $argv[1];
119	} else if ( array_key_exists('SCRIPT_FILENAME',$_SERVER)) {
120		$path = $_SERVER['SCRIPT_FILENAME'];
121		//error_log("SCRIPT_FILENAME($path)",0);
122	} else {
123		$path = $argv[0];
124		//error_log("argv($path)",0);
125	}
126	if ( ($p=strpos($path,'?')) !== false) $path = substr($path,0,$p);
127	if ( ($p=strrpos($path,'/')) /*< strlen($path)-1*/) $path = substr($path,0,$p);
128	error_log("exif test page - counting files in $path");
129	_search_file($path,$possible);
130	error_log("exif test page - counting files: ".count($possible)." done.",0);
131	return $possible;
132}
133
134/****************************************************************************/
135// function: search_file($file,$ext)
136//
137// Searches for $file in document tree. The path is ignored.
138//
139function AddInfo($Name,$Value,$highlight=0) {
140	if (is_array($Value)) $Value = 'Array: ('.join(',',$Value).')';
141	$Value = nl2br($Value);
142	if ( $highlight) {
143		$Name = "<th>$Name</th>";
144	} else {
145		$Name = "<td>$Name</td>";
146	}
147	return "<tr>$Name<td>$Value&nbsp;</td></tr>\n";
148}
149
150$possible = search_file();
151
152$title = "PHP module exif test page";
153
154?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional">
155<html>
156<head>
157<title><?=$title ?></title>
158<style type="text/css">
159body {
160	font-size: 12pt;
161}
162h1 {
163	font-size: 20pt;
164	font-weight:bold;
165}
166h2 {
167	font-size: 16pt;
168	font-weight:bold;
169}
170th {
171	text-align: left;
172}
173ul {
174	margin-bottom: 6pt;
175}
176</style>
177</head>
178<body>
179<h1><?=$title ?></h1>
180<h2>(c) Marcus B&ouml;rger, 2002</h2>
181</p>
182<p>
183Images taken from <a href="http://www.exif.org">www.exif.org</a>,
184<a href="http://marcus-boerger.de">marcus-boerger.de</a>
185all rights reserved by their authors and artists, see exif headers.
186The files can be downloaded <a href="http://marcus-boerger.de/php/ext/exif/test/">here</a>.
187To start the test you simple have to put all images into the same directory as this script.
188The test will work with all files in that directory and all subdirectories. To test private
189images just put them into that directory.
190</p>
191<p>
192Youmay take a look at the test <a href="http://marcus-boerger.de/php/ext/exif/test.txt">source here</a>.
193</p>
194<p>
195This test just prooves that some exif headers can be scanned.
196If all files produce a header in output the module might be o.k.
197</p>
198<p>
199What to look for in detail:
200</p>
201<ul>
202<li>kodak-dc4800-plus-acdsee.jpg
203	<ul>
204	<li>should provide a <b>long</b> comment 'by marcus b&ouml;rger&lt;%04i&gt;'*n</li>
205	<li>this file returns an array but it also produces an errormessage because ACDSee destroys
206	    the integrity of IFD directory (size of directory and offsets of entries following any
207	    edited entry maybe wrong).
208	</li>
209	</ul>
210</li>
211<li>hp-photosmart.jpg
212	<ul>
213	<li>should provide a <b>two line</b> copyright notice</li>
214	</ul>
215</li>
216<li>olympus-d320l
217	<ul>
218	<li>should provide an <b>APP12</b> infoset</li>
219	</ul>
220</li>
221<li>unknown.jpg
222	<ul>
223	<li>should provide an <b>empty</b> comment, this is a comment section and not an IFD0, EXIF or GPS section</li>
224	</ul>
225</li>
226<li>some images
227	<ul>
228	<li>have empty fields, that is the tag is present but no data is stored</li>
229	</ul>
230</li>
231</ul>
232<h2>function exif_tagname</h2>
233<table border='1' cellspacing='0' cellpadding='3' summary="EXIF headernames">
234<?php
235if (function_exists('exif_tagname')) {
236?>
237<tr><td>ImageWidth</td><td><?=@exif_tagname(0x0100)?></td><td><?=error_msg()?></td></tr>
238<tr><td>JPEGProc</td><td><?=@exif_tagname(0x0200)?></td><td><?=error_msg()?></td></tr>
239<tr><td>SceneType</td><td><?=@exif_tagname(0xA301)?></td><td><?=error_msg()?></td></tr>
240<tr><td>false</td><td><?=@exif_tagname(0x0000)===false?'false':'value'?></td><td><?=error_msg()?></td></tr>
241<?php
242} else {
243	echo "<tr><td>function exif_tagname is not supported</td></tr>\n";
244}
245?>
246</table>
247<br clear="all">
248<h2>function exif_read_data for <?=count($possible)?> images</h2>
249
250<?php
251$check_getimagesize   = false;
252$check_exif_thumbnail = true;
253$check_exif_read_data = false;
254$fast_output = false;
255if (function_exists('exif_read_data')) {
256	$num  = 0;
257	echo "<table border='1' cellspacing='0' cellpadding='3' summary='function results'>\n";
258	$tab2 = "";//"<table border='1' cellspacing='0' cellpadding='3' summary='EXIF information'>\n";
259	$types = array('','GIF','JPEG','PNG','SWF','PSD','BMP','TIFF_II','TIFF_MM','JPC','JP2','JPX','JB2');
260	foreach($possible as $idx => $file) {
261		$type = $file['type'];
262		$file = $file['file'];
263		if ( !((++$num)%100)) error_log("exif test page - checking files: $num",0);
264		$error = '';
265		$len   = 2;
266		$rows  = 1
267		       + ($check_getimagesize ? 1 : 0)
268		       + ($check_exif_thumbnail ? 1 : 0)
269		       + ($check_exif_read_data ? 1 : 0);
270		if ( !$fast_output) echo "<tr><td rowspan='$rows' valign='top'>$num</td><th colspan='2'>$file</th></tr>\n";
271		if ($check_getimagesize) {
272			$len++;
273			$size  = GetImageSize($file);
274			$error = error_msg();// clear message
275			if ( $size === false) {
276				$error = '<b style="color:red">GetImageSize returned false</b><br>'.$error;
277			    $res_getimagesize = $error;
278			} else {
279				$res_getimagesize = '('.join($size,',').')';
280			}
281			if ( !$fast_output) echo AddInfo("GetImageSize",$error,1);
282		}
283		if ( $check_exif_thumbnail) {
284			$len++;
285			if ($type!=IMAGETYPE_JPEG) {// && $type!=IMAGETYPE_TIFF_II && $type!=IMAGETYPE_TIFF_MM) {
286				$error = "<b style='color: green'>filetype not supported: $types[$type]</b>";
287			    $res_exif_thumbnail = $error;
288			} else {
289				$t_width  = 0;
290				$t_height = 0;
291				$result = exif_thumbnail($file, $t_width, $t_height);
292				$error = error_msg();// clear message
293				if ( $result === false) {
294					$error = '<b style="color:red">exif_thumbnail returned false</b><br>'.$error;
295					if ( $t_width && $t_height) {
296						$error = "<b style='color:green'>$t_width x $t_height</b><br>$error";
297					}
298				    $res_exif_thumbnail = $error;
299				} else {
300					$res_exif_thumbnail = $t_width . " x " . $t_height;
301				}
302			}
303			if ( !$fast_output) echo AddInfo("exif_thumbnail",$error,1);
304		}
305		if ($check_exif_read_data) {
306			$len++;
307			if ($type!=IMAGETYPE_JPEG && $type!=IMAGETYPE_TIFF_II && $type!=IMAGETYPE_TIFF_MM) {
308				$res_exif_read_data = "<b style='color: green'>filetype not supported: $types[$type]</b>";
309				if ( !$fast_output) echo AddInfo("exif_read_data",$res_exif_read_data);
310				$res = '';
311			} else {
312				$image = exif_read_data($file,'COMMENT,IFD0,EXIF,APP12',true);
313				$error = error_msg();// clear message
314				if ( !$fast_output) echo AddInfo("exif_read_data",$error,1);
315				$res   = '';
316				if ( $image === false) {
317				    $res_exif_read_data = "<b style='color:red'>exif_read_data returned false</b><br>$error";
318				} else {
319					$res_exif_read_data = $error;
320					// ah no!$error = error_msg(); // force o.k.
321					foreach($image as $Name => $Value) {
322						if ( $Name!='Thumbnail') {
323							if ( is_array($Value)) {
324								$len++;
325								$res .= AddInfo($Name,'Array('.count($Value).')');
326								foreach( $Value as $idx => $Entry) {
327									if ($idx==='Thumbnail') $Entry = '&lt;data&gt;';
328									$len++;
329									$res .= AddInfo($Name.':'.$idx,$Entry);
330								}
331							} else {
332								$len++;
333								$res .= AddInfo($Name,$Value);
334							}
335						}
336					}
337				}
338			}
339		}
340		$tab2 .= "<tr><td rowspan='$len' valign='top'>$num</td></tr>\n";
341		$tab2 .= "<tr><th colspan='2'>$file</th></tr>\n";
342		if ($check_getimagesize) {
343			$tab2 .= "<tr><th>GetImageSize</th><td>$res_getimagesize</td></tr>\n";
344		}
345		if ($check_exif_thumbnail) {
346			$tab2 .= "<tr><th>exif_thumbnail</th><td>$res_exif_thumbnail</td></tr>\n";
347		}
348		if ($check_exif_read_data) {
349		$tab2 .= "<tr><th>exif_read_data</th><td>$res_exif_read_data</td></tr>\n";
350		$tab2 .= $res;
351		}
352		if ( $fast_output) {
353			echo $tab2;
354			$tab2 = '';
355		}
356	}
357	error_log("exif test page - checking files: ".count($possible)." done.",0);
358	echo $tab2;
359	echo "</table>\n";
360} else {
361	echo "<h1 style='color:red'>function exif_read_data is not supported</h1>\n";
362}
363?>
364</body>
365</html>