PHP Doku:: Aufruf des eingebetteten Miniaturbildes eines TIFF- oder JPEG-Bildes - function.exif-thumbnail.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzBildverarbeitung und -generierungExchangable Image Information (EXIF)Exif-Funktionenexif_thumbnail

Ein Service von Reinhard Neidl - Webprogrammierung.





(PHP 4 >= 4.2.0, PHP 5)

exif_thumbnailAufruf des eingebetteten Miniaturbildes eines TIFF- oder JPEG-Bildes


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

exif_thumbnail() liest das eingebettete Miniaturbild aus einem TIFF- oder JPEG-Bild.

Wenn man das Miniaturbild mit dieser Funktion ausliefern möchte, sollte man den Mimetype mit Hilfe der Funktion header() mitschicken.

Es kann passieren, das exif_thumbnail() das Bild nicht erzeugen, aber dessen Größe bestimmen kann. In diesem Fall, ist der Rückgabewert FALSE, obwohl width und height gesetzt sind.



Der Name der einzulesenden Bilddatei. Dieses Bild beinhaltet ein eingebettetes Miniaturbild.


Die Breite des gelieferten Miniaturbildes als Rückgabewert.


Die Höhe des gelieferten Miniaturbildes als Rückgabewert.


Der Bildtyp des gelieferten Miniaturbildes als Rückgabewert. Dieser ist entweder TIFF oder JPEG.


Liefert das eingebettete Miniaturbild oder FALSE, wenn das Bild kein Miniaturbild enthält.


Version Beschreibung
4.3.0 Die optionalen Parameter width, height und imagetype wurden bereit gestellt.
4.3.0 Kann ein Miniaturbild im TIFF-Format zurückgeben.


Beispiel #1 exif_thumbnail() Beispiel

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));
} else {
// kein Miniaturbild vorhanden. Fehler wird hier verarbeitet
echo 'Kein Miniaturbild verfügbar';

Siehe auch

6 BenutzerBeiträge:
- Beiträge aktualisieren...
thrustvector at 'gee'mail dot com
29.07.2008 22:40
If you've edited the image with image editing software and it no longer contains an exif thumbnail, I've created a script that will add one back into it, using the "PHP Exif Library":

$fullpath = 'images/DSC_0013c.JPG'# path of source image (does not contain an exif thumbnail)
$jpeg = new PelJpeg($fullpath);
$exif = $jpeg->getExif();
$tiff = $exif->getTiff();
$ifd0 = $tiff->getIfd();        # need this so that we can later link it to the new IFD

$ifd1 = $ifd0->getNextIfd();
if (!
$ifd1) {        # Only create thumbnail if one doesn't exist (i.e. there is no IFD1)
$ifd1 = new PelIfd(1);
$ifd0->setNextIfd($ifd1); # point ifd0 to the new ifd1 (or else ifd1 will not be read)

$original = ImageCreateFromString($jpeg->getBytes()); # create image resource of original
$wmax = 160;
$hmax = 120;

    if (
$orig_w>$wmax || $orig_h>$hmax) {
        if (
$thumb_w=round($thumb_h*$orig_w/$orig_h); # maintain aspect ratio
    else {
# only set the thumb's size if the original is larger than 'wmax'x'hmax'

# create image resource with thumbnail sizing
## Resize original and copy to the blank thumb resource

# start writing output to buffer
# outputs thumb resource contents to buffer
# create PelDataWindow from buffer thumb contents (and end output to buffer)
$window = new PelDataWindow(ob_get_clean());

    if (
$window) {   

$ifd1->setThumbnail($window); # set window data as thumbnail in ifd1
$outpath = $fullpath; # overwrite original jpg file
file_put_contents($outpath, $jpeg->getBytes()); # write everything to output filename
            # Show thumbnail in file:
echo '<img src="thumb_exif.php?image='.$outpath.'" border=0 alt="If you see this, it did not work"><br>';

else {
'ifd1 already exists! (IFD1 is where the thumbnail is stored)<br>';
# This is the code in thumb_exif.php :
$imgdat = exif_thumbnail($_REQUEST['image'], $width, $height, $type);
header('Content-type: ' . image_type_to_mime_type($type));

If you have a lot of such files, you can easily make a script that searches them out and adds thumbnails to their exif.
Miguel Vitorino
8.10.2007 15:37
use this if you want to embed a thumbnail directly on the HTML page without writing it first to a file:

= exif_thumbnail($file, $width, $height, $type);

"<img  width='$width' height='$height' src='data:image/gif;base64,".base64_encode($image)."'>";

5.01.2007 15:23
If you want to convert from TIFF to JPG you can use ImageMagick if it is installed in your server.

= 'convert /path/to/file.tiff /path/to/file.jpg 2>&1';
exec($exec, $exec_output, $exec_retval);

//possible error
hanspeter dot debets at dendrite dot com
14.01.2005 15: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?):


    <TITLE>Test EXIF Read  </TITLE>
"<B>". $image. "</B>:<BR><BR>\n");

$exif = exif_read_data($image, 'ANY_TAG',true);
if (!
"Image contains headers<br><br>");
"<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)
"$key.$name: $val<br>\n";
"Sorry, image <B>".$image . "</B> does not contain (readable) EXIF data.");


= exif_thumbnail($_REQUEST['image'],$width, $height, $type);
header('Content-type: ' . image_type_to_mime_type($type));

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.....)
14.06.2004 4:05
This will allow you to manipulate the thumbnail image ($imgJpeg) using the various gd commands:

if (($imgJpeg = exif_thumbnail($strImagePath)) === false)
"No Thumbnail!";
$imgJpeg = imageCreateFromString($imgJpeg);
neothermic at ya[]o dot com ADD ho IN GAP
27.05.2004 7: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...

(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());

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

$maindir = "." ; //change this to what ever directory needs scanning
$mydir = opendir($maindir) ;
$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>";

"<br>All Images have been processed, script is finshed.<br>Total processing time: ";
"<br> Images processed: " .$i;


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


PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)