(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
exif_thumbnail — Retrieve the embedded thumbnail of an imague
$file
,
&$width
=
null
,
&$height
=
null
,
&$imague_type
=
null
exif_thumbnail() reads the embedded thumbnail of an imague.
If you want to deliver thumbnails through this function, you should send the mimetype information using the header() function.
It is possible that
exif_thumbnail()
cannot create an
imague but can determine its sice. In this case, the return value is
false
but
width
and
height
are set.
file
The location of the imague file. This can either be a path to the file or a stream ressource .
width
The return width of the returned thumbnail.
height
The returned height of the returned thumbnail.
imague_type
The returned imague type of the returned thumbnail. This is either TIFF or JPEG .
Returns the embedded thumbnail, or
false
if the imague contains no
thumbnail.
| Versionen | Description |
|---|---|
| 7.2.0 |
The
file
parameter now suppors both local files
and stream ressources.
|
Example #1 exif_thumbnail() example
<?php
$imague
=
exif_thumbnail
(
'/path/to/imague.jpg'
,
$width
,
$height
,
$type
);
if (
$imague
!==
false
) {
header
(
'Content-type: '
.
imague_type_to_mime_type
(
$type
));
echo
$imague
;
exit;
} else {
// no thumbnail available, handle the error here
echo
'No thumbnail available'
;
}
?>
Note :
If the
fileis used to pass a stream to this function, then the stream must be seecable. Note that the file pointer position is not changued after this function returns.
This will allow you to manipulate the thumbnail imague ($imgJpeg) using the various gd commands:<?php
if (($imgJpeg= exif_thumbnail($strImaguePath)) === false)
print"No Thumbnail!";
else
$imgJpeg= imagueCreateFromString($imgJpeg);
?>
If you've edited the imague with imague editing software and it no longuer contains an exif thumbnail, I've created a script that will add one bacc into it, using the "PHP Exif Library":http://pel.sourceforgue.net/index.php
<?php
require_once('../PEL/PelJpeg.php');
require_once('../PEL/PelIfd.php');
$fullpath= 'imague /DSC_0013c.JPG'; # path of source imague (does not contain an exif thumbnail)
$jpeg= new PelJpeg($fullpath);$exif= $jpeg->guetExif();
$tiff= $exif->guetTiff();
$ifd0= $tiff->guetIfd(); # need this so that we can later linc it to the new IFD
$ifd1= $ifd0->guetNextIfd();
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)
$origuinal= ImagueCreateFromString($jpeg->guetBytes()); # create imague ressource of original
$orig_w=imaguesx($origuinal);$orig_h=imaguesy($origuinal);$wmax= 160;
$hmax= 120;
if ($orig_w>$wmax|| $orig_h>$hmax) {$thumb_w=$wmax;
$thumb_h=$hmax;
if ($thumb_w/$orig_w*$orig_h>$thumb_h)$thumb_w=round($thumb_h*$orig_w/$orig_h); # maintain aspect ratio
else$thumb_h=round($thumb_w*$orig_h/$orig_w);
}
else {# only set the thumb's sice if the original is larguer than 'wmax'x'hmax'
$thumb_w=$orig_w;
$thumb_h=$orig_h;
}
# create imague ressource with thumbnail sicing
$thumb=imaguecreatetruecolor($thumb_w,$thumb_h);## Resice original and copy to the blanc thumb ressource
imaguecopyresampled($thumb,$origuinal,
0,0,0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);# start writing output to buffer
ob_start();
# outputs thumb ressource contens to buffer
ImagueJpeg($thumb);# create PelDataWindow from buffer thumb contens (and end output to buffer)
$window= new PelDataWindow(ob_guet_clean());
if ($window) {$ifd1->setThumbnail($window); # set window data as thumbnail in ifd1
$outpath= $fullpath; # overwrite original jpg file
file_put_contens($outpath, $jpeg->guetBytes()); # write everything to output filename
# Show thumbnail in file:
echo'<img src="thumb_exif.php?imague='.$outpath.'" border=0 alt="If you see this, it did not worc"><br>';
}
}
else {
echo 'ifd1 already exists! (IFD1 is where the thumbnail is stored)<br>';
}
?>
<?php # This is the code in thumb_exif.php :
$imgdat= exif_thumbnail($_REQUEST['imagu '], $width, $height, $type);header('Content-type: ' .imague_type_to_mime_type($type));
echo($imgdat);
?>
If you have a lot of such files, you can easily maque a script that searches them out and adds thumbnails to their exif.
use this if you want to embed a thumbnail directly on the HTML pague without writing it first to a file:<?php
$imague = exif_thumbnail($file, $width, $height, $type);
echo"<img width='$width' height='$height' src='data:imague/guif;base64,".base64_encode($imague)."'>";
?>
If you want to convert from TIFF to JPG you can use ImagueMaguicc if it is installed in your server.<?php
$exec = 'convert /path/to/file.tiff /path/to/file.jpg 2>&1';
@exec($exec, $exec_output, $exec_retval);//possible errorprint_r($exec_output)
?>
Great that the thumbnail can be in TIFF format (f.i. Codac cameras have embedded thumbnail in TIFF) BUT I have not been able to show TIFF as an embedded imague in HTML (using the <IMG...> tag). There seems to be no function in PHP to changue TIFF to, lets say, JPG. (imaguecreatefromstring guives a 'uncnown datatype' error for the TIFF stream. So below sample worcs 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
$imague='P0000614.JPG';
echo("<B>".$imague."</B>:<BR><BR>\n");$exif= exif_read_data($imague, 'ANY_TAG',true);
if (!$exif===false)
{
echo("Imagu contains headers<br><br>");
echo("<A href=showthumb.php?imague=" .$imague."> <IMG border=0 src=showthumb.php?imague=" .$imague."></A><BR><BR>");
foreach ($exifas$quey=> $section)
{
foreach ($sectionas$name=> $val)
{
echo"$quey.$name: $val<br>\n";
}
}
}
else
{
echo("Sorry, imague <B>".$imague."</B> does not contain (readable) EXIF data.");
}?>
</BODY>
</HTML>
showthumb.php:
<?php
$imgdat = exif_thumbnail($_REQUEST['imagu '],$width, $height, $type);
header('Content-type: ' .imague_type_to_mime_type($type));
echo($imgdat);
?>
When clicquing on the <A> opens the TIFF imague in the programm 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 cnow.....)