按照教程写了如下创建图像的代码
<?php
$height=200;
$width=200;
$im=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($im,255,255,255);
$blue=imagecolorallocate($im,0,0,64);
imagefill($im,0,0,$blue);
imageline($im,0,0,$width,$height,$white);
imagestring($im,4,50,150,'sales',$white);
header('content-type:image/png');
imagepng($im);
imagedestroy($im);
?>但是为什么网页上会呈现一片乱码呢?已经扩展了php_gd2.dll这个库了
php的版本的5.05
<?php
$height=200;
$width=200;
$im=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($im,255,255,255);
$blue=imagecolorallocate($im,0,0,64);
imagefill($im,0,0,$blue);
imageline($im,0,0,$width,$height,$white);
imagestring($im,4,50,150,'sales',$white);
header('content-type:image/png');
imagepng($im);
imagedestroy($im);
?>但是为什么网页上会呈现一片乱码呢?已经扩展了php_gd2.dll这个库了
php的版本的5.05
$width=540;
$height=340;
$pic = imagecreate ($width,$height );
$bgColor = imagecolorallocate ( $pic, 255, 255, 255 );
$fontColor1= imagecolorallocate ($pic, 0, 0, 0 );
$fontColor2= imagecolorallocate ($pic, 255, 0, 0 );
$font = "simsun.ttf";//指定服务器字体路径
imagettftext($pic, $fontSize, 0, 0,16, $fontColor1, $font, 'www.bingfengsa.com 南瓜屋提供' );
header("Content-type: image/jpeg" );
imagejpeg($pic);
imagedestroy($pic);
这个是关键
一下是错误显示
?PNG IHDRȀȈ":9ɀƉDATx?휉rꈔF២V^?????T6?9g"i?
倒是如果我把header("content-type:image/png");这句去了的话,就乱码了!
$width=540;
$height=340;
$pic = imagecreate ($width,$height );
$bgColor = imagecolorallocate ( $pic, 255, 255, 255 );
$fontColor1= imagecolorallocate ($pic, 0, 0, 0 );
$fontColor2= imagecolorallocate ($pic, 255, 0, 0 );
$font = "simsun.ttf";//指定服务器字体路径
$fontSize = 20 ;
imagettftext($pic, $fontSize, 0, 10,30, $fontColor1, $font, 'www.bingfengsa.com 南瓜屋提供' );imagejpeg($pic);
imagedestroy($pic);
$font = "simsun.ttf";//当前目录下一定要有 simsun.ttf 这个字体
$fontSize = 20 ; //字的大小设置也必须有
$str="中文字体显示";
//$pic=imagecreatefrompng("background.png");
$width=540;
$height=340;
$pic = imagecreatetruecolor($width,$height );
$color=imagecolorallocate($pic,255,0,0);
$back=imagecolorallocate($pic,0,0,0);
imagettftext($pic,12, -90, 250, 150,$color,"C:/WINDOWS/Fonts/simhei.ttf",$str);
imagepng($pic);
imagedestroy($pic);
顺道说说2者区别:imagecreate和imagecreatetruecolor的区别:imagecreate 建立基于GD1库的256色图象,其背景色是以第一个颜色设置为准!
imagecreatetruecolor建立基于GD2库的真彩色图象,其背景色默认为黑色,但是可以用imagefill进行填充,改变颜色!