exif_thumbnail

(PHP 4 >= 4.2.0, PHP 5)

exif_thumbnail -- 取得嵌入在 TIFF 或 JPEG 图像中的缩略图

说明

string exif_thumbnail ( string filename [, int &width [, int &height [, int &imagetype]]] )

exif_thumbnail() 读取 TIFF 或 JPEG 图像中的嵌入缩略图。如果图像不包含缩略图则返回 FALSE

参数 widthheightimagetype 自 PHP 4.3.0 起可用,返回缩略图的大小和类型。有可能 exif_thumbnail() 不能创建图像但可以测定其大小,此情况下返回值是 FALSE,但是 widthheight 的值已经传回了。

如果想用此函数发送缩略图,应该用 header() 函数发送 mime 类型信息。下面的例子演示了此方法:

例子 1. exif_thumbnail() 例子

<?php
if (array_key_exists('file',$_REQUEST)) {
    
$image = exif_thumbnail($_REQUEST['file'], $width, $height, $type);
} else {
    
$image = false;
}
if (
$image!==false) {
    
header("Content-type: ".image_type_to_mime_type($type));
    echo
$image;
    exit;
} else {
    
// no thumbnail available, handle the error here
    
echo "No thumbnail available";
}
?>

自 PHP 4.3.0 起,exif_thumbnail() 函数可以以 TIFF 格式返回缩略图。

注: 本函数仅在 PHP 编译时使用了 --enable-exif 选项时可用。其功能和行为在 PHP 4.2 改变了。

注: 本函数不需要 GD 图像库。

参见 exif_read_data()image_type_to_mime_type()


add a note add a note User Contributed Notes
hanspeter dot debets at dendrite dot com
14-Jan-2005 10:35
Great that the thumbnail can be in TIFF format (f.i. Kodak cameras have embedded thumbnail in TIFF) BUT I have not been able to show TIFF as an embedded image in HTML (using the <IMG...> tag). There seems to be no function in PHP to change TIFF to, lets say, JPG. (imagecreatefromstring gives a 'unknown datatype' error for the TIFF stream. So below sample works great for JPEG embedded thumbnail, but not for TIFF embedded (but then, maybe I did something wrong?):

test_exif.php:

<HTML>
<HEAD>
   <TITLE>Test EXIF Read  </TITLE>
</HEAD>
<BODY>
<?php
$image
='P0000614.JPG';
echo(
"<B>". $image. "</B>:<BR><BR>\n");

$exif = exif_read_data($image, 'ANY_TAG',true);
if (!
$exif===false)
{
   echo(
"Image contains headers<br><br>");
   echo(
"<A href=showthumb.php?image=" . $image ."> <IMG border=0 src=showthumb.php?image=" . $image ."></A><BR><BR>");

   foreach (
$exif as $key => $section)
   {
       foreach (
$section as $name => $val)
       {
           echo
"$key.$name: $val<br>\n";
       }
   }
}
else
{
   echo(
"Sorry, image <B>".$image . "</B> does not contain (readable) EXIF data.");
}
?>
</BODY>
</HTML>

showthumb.php:

<?php
$imgdat
= exif_thumbnail($_REQUEST['image'],$width, $height, $type);
header('Content-type: ' . image_type_to_mime_type($type));
echo(
$imgdat);
?>

When clicking on the <A> opens the TIFF image in the program that windows assigned to this type, but the JPEG opens in the browser.

I am using PHP 4.3.6 on windows iis 4 (yeah, I know.....)
Eric
14-Jun-2004 10:05
This will allow you to manipulate the thumbnail image ($imgJpeg) using the various gd commands:

<?php
 
if (($imgJpeg = exif_thumbnail($strImagePath)) === false)
   print
"No Thumbnail!";
  else
  
$imgJpeg = imageCreateFromString($imgJpeg);
?>
neothermic at ya[]o dot com ADD ho IN GAP
27-May-2004 01:01
You can use the EXIF thumbnail function to extract the thumnails for use in an image gallery. I've found this to be faster than using other GD functions to convert the image to a smaller one then save it.

Here is the code I use to generate thumbnails. Remember, this is more of a utility script, although its simple to modifiy it for use...

<?PHP
set_time_limit
(0); //use this to make sure the script doesn't time out on large number of images.
//error_reporting(E_NONE); //use this to disable errors in a production script

function getmicrotime() {
  
$temparray=split(" ",microtime());
  
$returntime=$temparray[0]+$temparray[1];
   return
$returntime;
}

//lets make it slightly proper HTML. not valid, but can be changed quickly to be
echo "<html>
<head><title>Processing...</title></head>
<body>"
;

$maindir = "." ; //change this to what ever directory needs scanning
$mydir = opendir($maindir) ;
$starttime=getmicrotime();
$i = 0; //set the count of images processed to 0
while($fn = readdir($mydir)) //scan through the whole directory
  
{//open while

      
$startimagetime = getmicrotime();
      
$ext = strtolower(substr($fn,strlen($fn)-3)); //get the extension of an image
      
if ($ext == "jpg") {
          
$i++; //increase the number of images processed
          
echo $fn ." is being processed....<br>";
          
flush(); //needed to display each image progress
          

          
$image = exif_thumbnail($fn, $width, $height, $type);

           if (
$image!==false) {
                  
//if there is a good enough thumbnail to use, we have it here.
                   //extract it and put it in the file, call it [original image name].thumb.jpg
                
$handle = fopen ($fn.".thumb.jpg", 'a');
                
//write the thumbnail image
                
fwrite($handle, $image);
           } else {
                
// no thumbnail available, handle the error here
                
echo "No thumbnail available for file ".$fn."<br>";
           }
       }
}
closedir($mydir);
$endtime=getmicrotime();

echo
"<br>All Images have been processed, script is finshed.<br>Total processing time: ";
print
$endtime-$starttime;
echo
"<br> Images processed: " .$i;
echo
"</body>";
echo
"</html>"

?>

Its a bit crude, I must admit, but it can easily be adapted to suit user.

NeoThermic