php繪圖技巧如何在圖片上寫中文與英文

來源:互聯網
上載者:User
  1. //1、建立畫布

  2. $im = imagecreatetruecolor(300,200);//建立一個真彩色映像,預設背景是黑色,返回映像標識符。另外還有一個函數 imagecreate 已經不推薦使用。
  3. $red = imagecolorallocate($im,255,0,0);

  4. //2、寫字

  5. $str = "hello,world";
  6. imagestring($im,5,30,60,$str,$red);//參數說明:5-指文字的大小。函數 imagestring 不能寫中文

  7. //3、輸出映像

  8. header("content-type: image/png"); //bbs.it-home.org
  9. imagepng($im);//輸出到頁面。如果有第二個參數[,$filename],則表示儲存映像

  10. //4、銷毀映像,釋放記憶體

  11. imagedestroy($im);
  12. ?>

複製代碼

方法2,寫中文。

  1. //1、建立畫布

  2. $im = imagecreatetruecolor(300,200);//建立一個真彩色映像,預設背景是黑色,返回映像標識符。另外還有一個函數 imagecreate 已經不推薦使用。
  3. $red = imagecolorallocate($im,255,0,0);

  4. //2、寫字

  5. $str = iconv("gb2312","utf-8","北京,你早!hello,world");//檔案格式為gbk,而這裡轉為uft-8格式,才能正常輸出,否則也為亂碼。表示不明
  6. imagettftext($im,12,rand(0,20),20,100,$red,"simhei.ttf",$str);

  7. //3、輸出映像

  8. header("content-type: image/png");
  9. imagepng($im);//輸出到頁面。如果有第二個參數[,$filename],則表示儲存映像

  10. //4、銷毀映像,釋放記憶體

  11. imagedestroy($im);
  12. ?>

複製代碼

imagettftext() 函數遠強於imagestring() 函數,主要表現:1、imagettftext() 可以輸出中文和英文,可以指定字型;imagestring() 只能輸出英文,只能使用預設字型。2、imagettftext() 字型大小可以無限大;imagestring() 字型只有1~5號大小。3、imagettftext() 輸出的字型可以變換角度;imagestring() 只能水平輸出。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.