將文本轉化成圖片

  1. 首先要肯定你的php是否開啓了gd庫擴展,只要在瀏覽器地址欄輸入htpp://你的域名/phpinfo.php,回車,只要出現gd,就表示你的php環境支持了gd擴展。若是不支持,直接在php.ini裏開啓,去掉gd2前的;再重啓apache就能夠了。如圖:php

    php文字轉圖片
    php文字轉圖片
  2.  

    php代碼以下:html

    <?php apache

    header("Content-type:text/html;charset=utf-8");瀏覽器

    header ( 'Content-type: image/png' ); 字體

    $font_size = 18; //字體大小 14pxspa

    $text = '有朋自遠方來。不亦樂呼'; htm

    $font = 'fonts/simsun.ttc'; blog

    $font  =   iconv("UTF-8","gb2312",$font);圖片

    $fontarea = imagettfbbox($font_size,0,$font,$text); //肯定會變化的字符串的位置utf-8

    $text_width = $fontarea[2]-$fontarea[0]+($font_size/3); //字符串文本框長度

    $text_height = $fontarea[1]-$fontarea[7]+($font_size/3); ////字符串文本框高度

    $im = imagecreate( $text_width , $text_height ); 

    $white = imagecolorallocate($im, 255,255,255); //定義透明色

    $red = imagecolorallocate ( $im , 255 , 0 , 0);  //文本色彩

    imagettftext ( $im , $font_size , 0 , 0, $text_height-($font_size/2.5) , $red , $font , $text ); 

    imagecolortransparent($im,$white);

    imagepng ( $im ); 

    imagedestroy ($im); 

    ?> 

     

    3.最後運行效果,文字就變爲圖片了。如圖:

    php文字轉圖片
    php文字轉圖片
相關文章
相關標籤/搜索