exif_thumbnail

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

exif_thumbnail β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ встроСнноС ΠΏΡ€Π΅Π²ΡŒΡŽ изобраТСния

ОписаниС

function exif_thumbnail(
Β Β Β Β resource|string $file,
Β Β Β Β int &$width = null,
Β Β Β Β int &$height = null,
Β Β Β Β int &$image_type = null
): string|false

exif_thumbnail() считываСт встроСнноС ΠΏΡ€Π΅Π²ΡŒΡŽ изобраТСния.

Если Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ эскиз посрСдством этой Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ, Π²Π°ΠΌ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ ΠΎΡ‚ΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ mimetype-ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ, использовав header() Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ.

Иногда exif_thumbnail() Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ ΡΠΎΠ·Π΄Π°Ρ‚ΡŒ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅, Π½ΠΎ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Π΅Π³ΠΎ Ρ€Π°Π·ΠΌΠ΅Ρ€. Π’ Ρ‚Π°ΠΊΠΈΡ… случаях ΠΎΠ½Π° Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ false, Π½ΠΎ Π·Π°Π΄Π°Ρ‘Ρ‚ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π°ΠΌ width ΠΈ height ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹Π΅ значСния.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

file
ΠœΠ΅ΡΡ‚ΠΎΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ Ρ„Π°ΠΉΠ»Π° с ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ΠΌ. ΠœΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΠ°ΠΊ ΠΏΡƒΡ‚Ρ‘ΠΌ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ, Ρ‚Π°ΠΊ ΠΈ ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ²Ρ‹ΠΌ рСсурсом.
width
ВозвращаСмая ΡˆΠΈΡ€ΠΈΠ½Π° эскиза.
height
ВозвращаСмая высота эскиза.
image_type
Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹ΠΉ Ρ‚ΠΈΠΏ эскиза. Π­Ρ‚ΠΎ Π»ΠΈΠ±ΠΎ TIFF, Π»ΠΈΠ±ΠΎ JPEG.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ встроСнный эскиз ΠΈΠ»ΠΈ false, Ссли ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π΅ содСрТит эскиза.

Бписок измСнСний

ВСрсия ОписаниС
7.2.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ file ΠΏΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Π½ Π² stream ΠΈ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Ρ‚ΡŒ ΠΊΠ°ΠΊ Π»ΠΎΠΊΠ°Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ„Π°ΠΉΠ»Ρƒ, Ρ‚Π°ΠΊ ΠΈ ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ²Ρ‹ΠΉ рСсурс.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования exif_thumbnail()

<?php
$image
= exif_thumbnail('/path/to/image.jpg', $width, $height, $type);

if (
$image!==false) {
header('Content-type: ' .image_type_to_mime_type($type));
echo
$image;
exit;
} else {
// Π½Π΅Ρ‚ доступного ΠΏΡ€Π΅Π²ΡŒΡŽ, здСсь ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ ΠΎΡˆΠΈΠ±ΠΊΡƒ
echo 'НСт доступного эскиза';
}
?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Если ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ file использован для ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡ΠΈ ΠΏΠΎΡ‚ΠΎΠΊΠ° Π² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ, Ρ‚ΠΎ этот ΠΏΠΎΡ‚ΠΎΠΊ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±Ρ‹Ρ‚ΡŒ ΠΏΠ΅Ρ€Π΅ΠΌΠ°Ρ‚Ρ‹Π²Π°Π΅ΠΌΡ‹ΠΌ. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ Ρ„Π°ΠΉΠ»ΠΎΠ²Ρ‹ΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΎΠ½Π½Ρ‹ΠΉ ΡƒΠΊΠ°Π·Π°Ρ‚Π΅Π»ΡŒ Π½Π΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΈΠ·ΠΌΠ΅Π½Ρ‘Π½ послС Π·Π°Π²Π΅Ρ€ΡˆΠ΅Π½ΠΈΡ Ρ€Π°Π±ΠΎΡ‚Ρ‹ этой Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • exif_read_data() - Π§ΠΈΡ‚Π°Π΅Ρ‚ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ EXIF ΠΈΠ· Ρ„Π°ΠΉΠ»ΠΎΠ² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ
  • image_type_to_mime_type() - ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ Mime-Ρ‚ΠΈΠΏΠ° для Ρ‚ΠΈΠΏΠ° изобраТСния, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΠΎΠ³ΠΎ функциями getimagesize, exif_read_data, exif_thumbnail, exif_imagetype
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
7
Eric ΒΆ
21 years ago
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);
?>
up
3
thrustvector at &#39;gee&#39;mail dot com ΒΆ
17 years ago
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": http://pel.sourceforge.net/index.php

<?php
require_once('../PEL/PelJpeg.php');
require_once('../PEL/PelIfd.php');
$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
    $orig_w=imagesx($original);
    $orig_h=imagesy($original);
    $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 size if the original is larger than 'wmax'x'hmax'
        $thumb_w=$orig_w;
        $thumb_h=$orig_h;
    }

        # create image resource with thumbnail sizing
    $thumb=imagecreatetruecolor($thumb_w,$thumb_h); 
        ## Resize original and copy to the blank thumb resource
    imagecopyresampled($thumb,$original,
                               0,0,0,0,$thumb_w,$thumb_h,$orig_w,$orig_h);

        # start writing output to buffer
    ob_start();        
        # outputs thumb resource contents to buffer
    ImageJpeg($thumb);    
        # 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 {
    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['image'], $width, $height, $type);
        header('Content-type: ' . image_type_to_mime_type($type));
        echo($imgdat);
?>

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

<?php
$image = exif_thumbnail($file, $width, $height, $type);

echo "<img  width='$width' height='$height' src='data:image/gif;base64,".base64_encode($image)."'>";
?>
up
-1
Anonymous ΒΆ
19 years ago
If you want to convert from TIFF to JPG you can use ImageMagick 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 error
print_r($exec_output)
?>
up
-2
hanspeter dot debets at dendrite dot com ΒΆ
21 years ago
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.....)