linux 无法生成图片大小,window可以生成图片linux不行

欢迎关注个人公众号零零糖,偶尔发发生活的牢骚~

一个文字转图片的小功能~

在做文字转图片的时候,发现再window下能成功转换,在linux下无法生成图片,经排查报错信息是

imagettftext(): any2eucjp(): invalid code in input string

搜索相关资料,得到解决办法。记录之。

linux下执行

whereis php

找到php路径后执行

/opt/php55/bin/php -i |grep configure

如下图红色方框所示,确定是php编译问题,导致linux无法生成图片。

9078fdf4c657b18ed7e414a5690924c8.png

解决办法

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

码农转型写公众号中,欢迎关注,交流深夜的姿势,不要总是阅读毒鸡汤文~

零零糖公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值