歡迎關注個人公衆號零零糖,偶爾發發生活的牢騷~
一個文字轉圖片的小功能~
在做文字轉圖片的時候,發現再window下能成功轉換,在linux下無法生成圖片,經排查報錯信息是
imagettftext(): any2eucjp(): invalid code in input string
搜索相關資料,得到解決辦法。記錄之。
linux下執行
whereis php
找到php路徑後執行
/opt/php55/bin/php -i |grep configure
如下圖紅色方框所示,確定是php編譯問題,導致linux無法生成圖片。
解決辦法
1.把此編譯選項去掉;
2.使用mb_convert_encoding轉換
mb_convert_encoding(‘測試’, 'HTML-ENTITIES', 'UTF-8');
如下:
/**
* 將文字轉成圖片
* @param array $text 文字 如$text=關注公衆號零零糖 必傳
* @param string $font_path 字體路徑 必傳
* @param number $font_size 字體大小 默認30
* @param array $text_color 字體顏色 默認隨機
* @param bool $disturb 是否畫干擾點和線
* @author Yoper
* @Wechat Yoperman
*/
function text_to_img($text,$font_path,$font_size=30,$text_color,$disturb=false) {
if(empty($text)){
echo 'text empty';
}
if(empty($font_path)){
echo 'font_path empty';
}
$gd_support = extension_loaded('gd');
if(!$gd_support){
echo 'gd not support';
}
$text_length=mb_strlen($text,'utf-8');
empty($text_color) && ($text_color=[rand(20, 100), rand(20, 100), rand(20, 100)]);
//單個文字寬度
$single_text_width=$font_size+10;
//圖片大小
$width=$text_length*$single_text_width;//一個字50寬度,字越多越寬
$height=$single_text_width;//高度
$image = imagecreatetruecolor($width, $height);
//背景
$background = imagecolorallocate($image, 0, 0, 0);//白色
imagecolortransparent($image,$background); // 設置爲透明色,若註釋掉該行則輸出上面設置的背景
imagefill($image, 0, 0, $background);
//是否畫干擾
if($disturb){
//干擾點
for ($i=0; $i < 300; $i++) {
$pixColor = imagecolorallocate($image, rand(150, 240), rand(150, 240), rand(150, 240));
$pixX = rand(10, 190);
$pixY = rand(5, 55);
imagesetpixel($image, $pixX, $pixY, $pixColor);
}
//4條水平線
for ($i=0; $i < 5; $i++) {
$lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
$lineX1 = 0;
$lineX2 = 300;
$lineY1 = ($i + 1) * 12;
$lineY2 = ($i + 1) * 12;
imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}
//10條垂直線
for ($i=0; $i < 30; $i++) {
$lineColor = imagecolorallocate($image, rand(50, 150), rand(50, 150), rand(50, 150));
$lineX1 = ($i + 1) * 10;
$lineX2 = ($i + 1) * 10;
$lineY1 = 0;
$lineY2 = 60;
imageline($image, $lineX1, $lineY1, $lineX2, $lineY2, $lineColor);
}
}
//文字
for ($i=0; $i < $text_length; $i++) {
$textColor = imagecolorallocate($image,$text_color[0], $text_color[1], $text_color[2]);
$textX = $i * $single_text_width;//文字X座標
$textY=$single_text_width-10;
$text_angle=0;//旋轉角度
$single_text=mb_substr($text, $i, 1,'utf-8');//獲取單個文字
$single_text=mb_convert_encoding($single_text, 'HTML-ENTITIES', 'UTF-8');//對php編譯--enable-gd-jis-conv的處理
imagettftext($image, $font_size,$text_angle, $textX, $textY, $textColor,$font_path,$single_text);
}
header("Content-Type:image/png");
imagepng($image);
imagedestroy($image);
}
//使用方法
$font_path='/fonts/STXINWEI.TTF';//請修改成自己路徑的字體!可在C:\Windows\Fonts下複製出來
text_to_img('零零糖公衆號',$font_path,30);
另外,在THINKPHP5.1中,圖片不會直接呈現,會直接顯示"亂碼",即便代碼中設置了Content-Type:image/png,在瀏覽器上觀察依然是Content-Type:text/html; charset=utf-8
在header("Content-Type:image/png");前面加ob_end_clean();即可
...
ob_end_clean();
header("Content-Type:image/png");
...
END
碼農轉型寫公衆號中,歡迎關注,交流深夜的姿勢,不要總是閱讀毒雞湯文~
零零糖公衆號