用PHP產生動態影像圖_PHP教程

來源:互聯網
上載者:User
很多人不瞭解 PHP 可以產生非HTML的資料.這是對產生影像圖非常有用的.可以從 database 產生一個簡單的廣告橫圖或更簡單只產生一個圖形按鈕 .

我用 TTF 字型在以下的範例中
我通常取名作 'button.php3':

#######################################################
-----button.php3------
Header("Content-type: image/gif");
if(!isset($s)) $s=11;
$size = imagettfbbox($s,0,"fonts/TIMES.TTF",$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$xpad=9;
$ypad=9;
$im = imagecreate($dx+$xpad,$dy+$ypad);
$blue = ImageColorAllocate($im, 0x2c,0x6D,0xAF);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
ImageRectangle($im,0,0,$dx+$xpad-1,$dy+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$dy+$ypad,$white);
ImageTTFText($im, $s, 0, (int)($xpad/2)+1, $dy+(int)($ypad/2), $black, "fonts/TIMES.TTF", $text);
ImageTTFText($im, $s, 0, (int)($xpad/2), $dy+(int)($ypad/2)-1, $white, "fonts/TIMES.TTF", $text);
ImageGif($im);
ImageDestroy($im);
?>
#######################################################
很重要一點是你不能在這檔案中放任何HTML tags.也不能有空白行在 tag 之前或之後. 如果你用這段Script後看到一個不完整的影像, 表示你可能在PHP標籤以外誤打了字元.

以上的 script 可以由此文法在網頁中叫出來:

#######################################################
----test.php-----




New Page 1








#######################################################

test.php結果會像這樣: .

's' 參數是設定字型大小 .

這是當 s=18 時:

注I:
字型路徑 "/fonts/TIMES.TTF" 可由windows/fonts目錄下取得 TIMS.TTF 字型檔 Copy 至你網站的目錄 fonts下即可測試 至於中文的表現 尚待各位網友提供心得

注意 我先畫了一個黑色方塊區再用白色位移產生 3D 效果.也陬L法在淺色背景中看出來 但你可以把背景色改為深色看看此效果. 字型也做了同樣效果表現立體感.

你要先確定你的安裝 PHP 時有設定支援 GD 和 TTF. 可參考 PHP FAQ . 我建議可以 copy libgd.a 到 /usr/local/lib 和 gd*.h 相關檔案到 /usr/local/include 然後
'make install' for FreeTTF library.

可以在這http://rover.wiesbaden.netsurf.de/~kikita/ 找到釵httf 字型哦!

注:

以下的原始碼改進了上面的弁?可多行文字顯示:
#######################################################
--------------button.php-----------------

Header("Content-type: image/jpeg");
if(!isset($bgred)) $bgred=0;
if(!isset($bggreen)) $bggreen=51;
if(!isset($bgblue)) $bgblue=153;
if(!isset($chred)) $chred=255;
if(!isset($chgreen)) $chgreen=255;
if(!isset($chblue)) $chblue=255;
if(!isset($shadow)) $shadow="yes";
if(!isset($wrappos)) $wrappos=20;
if(!isset($crop)) $crop=2.2;
if(!isset($jpegquality)) $jpegquality=80;
if(!isset($s)) $s=11;
$savetext=$text;
$text=wordwrap($text,$wrappos," ",0);
if (!isset($font)) $fontname="/www/ttfonts/arialbd.ttf";
else
$fontname="/www/ttfonts/".$font.".ttf";
$size = imagettfbbox($s,0,$fontname,$text);
$dx = abs($size[2]-$size[0]);
$dy = abs($size[5]-$size[3]);
$upper=abs($size[5]);
$under=$size[1];
$th=$upper-$under;
$xpad=9;
if (substr_count($text,chr(13))>=1)
{
$mult=(substr_count($text,chr(13)));
$ypad=($mult*$crop*$s)+$s;
}
else $ypad=($crop-2)*$s;
$im = imagecreate($dx+$xpad,$th+$ypad);
$color = ImageColorAllocate($im, $bgred,$bggreen,$bgblue);
$black = ImageColorAllocate($im, 0,0,0);
$fontcolor = ImageColorAllocate($im, $chred,$chgreen,$chblue);
ImageRectangle($im,0,0,$dx+$xpad-1,$th+$ypad-1,$black);
ImageRectangle($im,0,0,$dx+$xpad,$th+$ypad,$white);
if ($shadow=="yes")
ImageTTFText($im, $s, 0, (int)($xpad/2)-2+1, $th+2+(int)($ypad/2)-3, $black, $fontname, $text);
ImageTTFText($im, $s, 0, (int)($xpad/2)-2, $th+2+(int)($ypad/2)-1-3, $fontcolor, $fontname, $text);
Imagejpeg($im,"",$jpegquality);
ImageDestroy($im);

?>
#######################################################
這可以下面這個 form 來產生:
#######################################################
----------test.php--------------------



New Page 1







#######################################################

或是直接像上例一樣呼叫:

#######################################################
----test.php-----



New Page 1







http://www.bkjia.com/PHPjc/315865.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/315865.htmlTechArticle很多人不瞭解PHP 可以產生非HTML的資料.這是對產生影像圖非常有用的.可以從 database 產生一個簡單的廣告橫圖或更簡單只產生一個圖形按鈕...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.