Heim  >  Artikel  >  Backend-Entwicklung  >  iconv乱码求解决

iconv乱码求解决

WBOY
WBOYOriginal
2016-06-06 20:16:501288Durchsuche

想做个小工具玩玩 结果遇到了中文乱码和、背景不能透明的问题,求大神帮忙看看是咋回事。原谅我是个小白

iconv乱码求解决

<code><?php header("content-Type: text/html; charset=Utf-8");
//1、创建画布
$im = imagecreatetruecolor(800,800);//新建一个真彩色图像,默认背景是黑色,返回图像标识符。另外还有一个函数 imagecreate 已经不推荐使用。

$red = imagecolorallocate($im,255,0,0);
$str = iconv("gb2312","utf-8//IGNORE","大撒旦撒旦撒!hello,world");
imagestring($im,5,0,0,$str,$red);//参数说明:5-指文字的大小。函数 imagestring 不能写中文

//2、加载外部图片
$im_new = imagecreatefromjpeg("baidu.jpg");//返回图像标识符
$im_new_info = getimagesize("baidu.jpg");//取得图像大小,返回一个数组。该函数不需要用到gd库。
/*----
****3、将加载的图片,复制到画布上
****参数说明:
 $im:不用说,指的是画布;
 $im_new:源图片,也就是从外面加载进来的图像
 (30,30):将加载进来的图像,放在画布中的位置,左上角
 (0,0):表示加载的图片,从什么位置开始。(0,0)表示左上角起点,也可以只加载图片的一部分进来的
 (*,*):用*表示,可以为原图片宽和高,也可以小于宽高,只截取一部分,与上面坐标一起使用,表示截取的部分
******/
imagecopy($im,$im_new,30,30,0,0,$im_new_info[0],$im_new_info[1]);//返回布尔值
//3、输出图像
header("content-type: image/png");
imagepng($im);//输出到页面。如果有第二个参数[,$filename],则表示保存图像
//4、销毁图像,释放内存
imagedestroy($im);
?></code>

回复内容:

想做个小工具玩玩 结果遇到了中文乱码和、背景不能透明的问题,求大神帮忙看看是咋回事。原谅我是个小白

iconv乱码求解决

<code><?php header("content-Type: text/html; charset=Utf-8");
//1、创建画布
$im = imagecreatetruecolor(800,800);//新建一个真彩色图像,默认背景是黑色,返回图像标识符。另外还有一个函数 imagecreate 已经不推荐使用。

$red = imagecolorallocate($im,255,0,0);
$str = iconv("gb2312","utf-8//IGNORE","大撒旦撒旦撒!hello,world");
imagestring($im,5,0,0,$str,$red);//参数说明:5-指文字的大小。函数 imagestring 不能写中文

//2、加载外部图片
$im_new = imagecreatefromjpeg("baidu.jpg");//返回图像标识符
$im_new_info = getimagesize("baidu.jpg");//取得图像大小,返回一个数组。该函数不需要用到gd库。
/*----
****3、将加载的图片,复制到画布上
****参数说明:
 $im:不用说,指的是画布;
 $im_new:源图片,也就是从外面加载进来的图像
 (30,30):将加载进来的图像,放在画布中的位置,左上角
 (0,0):表示加载的图片,从什么位置开始。(0,0)表示左上角起点,也可以只加载图片的一部分进来的
 (*,*):用*表示,可以为原图片宽和高,也可以小于宽高,只截取一部分,与上面坐标一起使用,表示截取的部分
******/
imagecopy($im,$im_new,30,30,0,0,$im_new_info[0],$im_new_info[1]);//返回布尔值
//3、输出图像
header("content-type: image/png");
imagepng($im);//输出到页面。如果有第二个参数[,$filename],则表示保存图像
//4、销毁图像,释放内存
imagedestroy($im);
?></code>

乱码原因是因为 浏览器和文件字符集不一致

,你可以把最上面的header去掉,不去掉的话在浏览器将编码改成gb2312。

不能透明是因为你根本没设置,谈何透明。
透明系列函数是alpha,看下哪个是你需要

iconv("gb2312","utf-8//IGNORE","大撒旦撒旦撒!hello,world");
把这里面的gb2312换成gbk试试,gbk的范围更大点

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn