html_image

Attribute NameTypeRequiredDefaultDescription
filestringYesn/aname/path to image
heightstringNoactual image heightheight to display image
widthstringNoactual image widthwidth to display image
basedirstringnoweb server doc rootdirectory to base relative paths from
altstringno""alternative description of the image
hrefstringnon/ahref value to link the image to

html_image is a custom function that generates an HTML tag for an image. The height and width are automatically calculated from the image file if none are supplied.

html_image 是一個客制化函式,可以產生讓影像使用的 HTML 標籤,如果沒 有提供影像的 height 與 width 的話,則會自動根據所提供的影像檔案資料而得。

basedir is the base directory that relative image paths are based from. If not given, the web server document root (env variable DOCUMENT_ROOT) is used as the base. If security is enabled, the path to the image must be within a secure directory.

basedir 指的是相關影像檔的目錄路徑。如果沒有設定的話,則會使用網頁伺 服器的根目錄(env 變數 DOCUMENT_ROOT)。如果有開啟安全機制的話,則影像的目錄路徑也必 須是一個安全路徑。

href is the href value to link the image to. If link is supplied, an <a href="LINKVALUE"><a> tag is put around the image tag.

href 是影像會連結到的 href 值,如果沒有給定的話,則會使用 <a href="LINKVALUE"><a>。

All parameters that are not in the list above are printed as name/value-pairs inside the created <img>-tag.

所有在上面所列出的參數並不是每個都可以以 name/value 的方式在 <img> 標籤中列印出來的。

Technical Note: html_image requires a hit to the disk to read the image and calculate the height and width. If you don't use template caching, it is generally better to avoid html_image and leave image tags static for optimal performance.

技術提醒: html_image 需要從硬碟中讀取影像檔且計算影像檔的長與寬,如果你沒有使 用樣版快取的話,最好是避免使用 html_image 而是使用 image 靜態標籤以取得更好的效能。

Example 8-7. html_image 範例

<?php

require('Smarty.class.php');
$smarty = new Smarty;
$smarty->display('index.tpl');

?>

where index.tpl is:

其中 index.tpl 的內容為:

{html_image file="pumpkin.jpg"}
{html_image file="/path/from/docroot/pumpkin.jpg"}
{html_image file="../path/relative/to/currdir/pumpkin.jpg"}

a possible output would be:

可能的輸出結果為:

<img src="pumpkin.jpg" alt="" width="44" height="68" />
<img src="/path/from/docroot/pumpkin.jpg" alt="" width="44" height="68" />
<img src="../path/relative/to/currdir/pumpkin.jpg" alt="" width="44" height="68" />