PHP Conference Japan 2024

exif_thumbnail

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

exif_thumbnail擷取影像的內嵌縮圖

說明

exif_thumbnail(
    資源|字串 $file,
    整數 &$width = null,
    整數 &$height = null,
    整數 (int) &$image_type = null
): 字串 (string)|false

exif_thumbnail() 函式用於讀取影像內嵌的縮圖。

如果您想透過此函式傳送縮圖,您應該使用 header() 函式發送 MIME 類型資訊。

exif_thumbnail() 有可能無法建立影像,但可以判斷其大小。在這種情況下,返回值為 false,但 widthheight 會被設定。

參數

file

影像檔案的位置。這可以是檔案的路徑或串流 資源 (resource)

width

返回縮圖的寬度。

height

返回縮圖的高度。

image_type

返回縮圖的影像類型。這可以是 TIFFJPEG

返回值

返回內嵌的縮圖,如果影像不包含縮圖,則返回 false

更新日誌

版本 說明
7.2.0 file 參數現在同時支援本地檔案和串流資源。

範例

範例 #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 參數將串流傳遞給此函式,則該串流必須是可搜尋的。請注意,此函式返回後,檔案指標位置不會改變。

參見

新增筆記

使用者貢獻的筆記 5 則筆記

7
Eric
20 年前
這樣您就可以使用各種 gd 命令操作縮圖 ($imgJpeg)

<?php
if (($imgJpeg = exif_thumbnail($strImagePath)) === false)
print
"沒有縮圖!";
else
$imgJpeg = imageCreateFromString($imgJpeg);
?>
3
thrustvector at 'gee'mail dot com
16 年前
如果您使用影像編輯軟體編輯過影像,並且它不再包含 exif 縮圖,我已經創建了一個腳本,可以使用「PHP Exif 函式庫」將縮圖新增回去: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);
?>

如果您有很多這樣的檔案,您可以輕鬆地製作一個腳本來搜尋它們並將縮圖新增到它們的 exif 中。
3
Miguel Vitorino
17 年前
如果您想要直接將縮圖嵌入 HTML 頁面而無需先將其寫入檔案,請使用以下程式碼:

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

echo
"<img width='$width' height='$height' src='data:image/gif;base64,".base64_encode($image)."'>";
?>
-1
匿名
17 年前
如果要將 TIFF 轉換為 JPG,可以使用伺服器中已安裝的 ImageMagick。

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

//可能的錯誤
print_r($exec_output)
?>
-2
hanspeter dot debets at dendrite dot com
19 年前
縮圖可以是 TIFF 格式真是太好了(例如柯達相機內嵌的縮圖是 TIFF 格式),但我一直無法在 HTML 中以嵌入式影像顯示 TIFF(使用 <IMG...> 標籤)。PHP 中似乎沒有將 TIFF 轉換為例如 JPG 的函式。(imagecreatefromstring 對於 TIFF 資料流會產生「未知資料類型」錯誤。因此,以下範例適用於 JPEG 內嵌縮圖,但不適用於 TIFF 內嵌縮圖(但也許我做錯了什麼?))

test_exif.php

<HTML>
<HEAD>
<TITLE>測試 EXIF 讀取</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(
"圖片包含標頭<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(
"抱歉,圖片 <B>".$image . "</B> 不包含(可讀取的) EXIF 資料。");
}
?>
</BODY>
</HTML>

showthumb.php

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

點擊 <A> 時,TIFF 圖像會在 Windows 指定給此類型的程式中開啟,但 JPEG 圖像會在瀏覽器中開啟。

我正在 Windows IIS 4 上使用 PHP 4.3.6(我知道這版本很舊……)
To Top