生成圖片文字混合圖片通常用于驗證碼了,下面來看一個關于php生成圖片文字混合圖片的例子,希望對大家會有所幫助.
例子一:圖片合成文字:
- <?php
- $im = imagecreatetruecolor(400, 30); //創建400 30像素大小的畫布
- $white = imagecolorallocate($im, 255, 255, 255);
- $grey = imagecolorallocate($im, 128, 128, 128);
- $black = imagecolorallocate($im, 0, 0, 0);
- imagefilledrectangle($im, 0, 0, 399, 29, $white); //輸出一個使用白色填充的矩形作為背景
- //如果有中文輸出,需要將其轉碼,轉換為UTF-8的字符串才可以直接傳遞
- //$text = iconv("GB2312", "UTF-8", "回憶經典");
- $text = "中文aflasjflasjflasjf";
- //設定字體,將系統中與simsun.ttc對應的字體復制到當前目錄下
- $font = 'SIMSUN.TTC';
- //Vevb.com
- imagettftext($im, 20, 0, 12, 21, $grey, $font, $text); //輸出一個灰色的字符串作為陰影
- imagettftext($im, 20, 0, 10, 20, $black, $font, $text); //在陰影上輸出一個黑色的字符串
- header("Content-type: image/png");
- imagepng($im);
- imagedestroy($im);
- exit;
- ?>
例子二:
- <?php
- //字體大小
- $size = 30;
- //字體類型,本例為宋體
- $font ="c:/windows/fonts/simsun.ttc";
- //顯示的文字
- $text = "www.49028c.com";
- //創建一個長為500高為80的空白圖片
- $img = imagecreate(500, 80);
- //給圖片分配顏色
- imagecolorallocate($img, 0xff, 0xcc, 0xcc);
- //設置字體顏色
- $black = imagecolorallocate($img, 0, 0, 0);
- //將ttf文字寫到圖片中
- imagettftext($img, $size, 0, 100, 50, $black, $font, $text);
- //發送頭信息
- header('Content-Type: image/gif');
- //輸出圖片
- imagegif($img);
- ?>
注意:
php中GD2擴展庫是優秀的圖片處理庫,可以創建各種格式圖片,功能及其強大!
開啟GD2:找到php的配置文件php.ini搜索extension=php_gd2.dll,去掉其前面的";
新聞熱點
疑難解答