以前寫程式上傳圖檔,由於瀏覽器具有快取功能,都要標註「如無看到最新圖片,請重新整理!」的字樣。而隨著程式語言團隊的努力,現在常見的用法<img src="xxx.php">,不但能顯示最新圖片,也能隱藏圖片來源位址。

各網路程式語言jsp,php,asp等,都有類似的函式可以達到此功能,以下imagecreatefromjpeg範例改自php官網:

<?php
function LoadJpeg($imgname)
{
    /* 開啟來源圖片 */
    $im = @imagecreatefromjpeg($imgname);

    /* 如果無法開啟圖片,設定輸出錯誤圖片 */
    if(!$im)
    {
        /* 建立一個黑色底圖 */
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);

        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

        /* 在黑色底圖上寫入文字 */
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    return $im;
}

header('Content-Type: image/jpeg');
//設定來源圖片位址(實體路徑)
$img = LoadJpeg('./post/p151a.jpg');

imagejpeg($img);
imagedestroy($img);
?>

輸出圖片:
<img src="http://andy.diimii.com/wp-temp/loadjpeg_demo.php" alt="imagecreatefromjpeg範例" />
imagecreatefromjpeg範例

註:另外還有imagecreatefromgif | imagecreatefrompng可以處理其它圖片類型

 
  • Hemidemi
  • MyShare
  • Udn
  • funP
  • Furl