画布的默认背景色是黑色,背景与文字边界像素的颜色是两种颜色融合后的,所以ImageColorTransparent($im,$red)这个函数不能彻底让背景透明。
你可以先给画布填充一个与文字颜色极度相近但是不等的颜色 例如文字颜色 255 0 0 背景颜色255 2 0
然后在将背景色透明。数值差可以慢慢调太相近 文字边缘就会被吞噬(文字变细)
‘贰’ php 如何把文字转成图片显示(象验证码那样)
需要安装PHP GD2库. 基本要用到以下几个函数
//设置一个图片文件名字
$png_name=time().'.png';
//画一个200*50的方框的图片
$img=imagecreate(200,50);
//设置背景颜色(白色)
$bg_color=imagecolorallocate($img,255,255,255);
//设置字体颜色(黑色)
$txt_color=imagecolorallocate($img,0,0,0);
//给图片添上背景颜色
imagefilledrectangle($img,0,0,200,50,$bg_color);
//取一个字符,然后画在方块中,其中:
21是字体大小
$deg是字体偏斜角度(0-360°)
$x是横坐标(在图片中,这里是0-200)
$y是纵坐标(在图片中,这里是0-50)
$txt_color是字体颜色
captcha.ttf是字体库
$letter是要写入的字符
imagefttext($img,21,$deg,$x,$y,$txt_color,'captcha.ttf',$letter);
//在图片中画一个点,用以干扰,所以坐标是随机的
imagesetpixel($img,rand()%200,rand()%50,$txt_color);
//在图片中画一条线,同样用以干扰,坐标随机(两组rand()确定了线段的起点和终点)
imageline($img,rand()%200,rand()%50,rand()%200,rand()%50,$bg_color);
//将$img输出为文件.
imagepng($img,$png_name);
‘叁’ 求一段php文字生成图片的代码
把imageString()去掉,加上下面的代码试试,很多人用这做图片水印的-_-!
$font_file = "字体文件的路径";
$str = '我爱PHP!';
$str = mb_convert_encoding($str,"UTF-8","GBK");
imagettftext('图像','字体大小','角度',x,y,'颜色',$font_file,$str);
‘肆’ PHP文字生成图片
没仔细看你的程序,但是如果你的意思是想加上中文,但是中文是gbk的,然后就出现乱码了的话就对了。
假设你的中文是 $str="这是你的中文内容"
加上下面一句:
$str = iconv("gbk","utf-8",$str)
这时候再把$str加到上面就不会乱码了
‘伍’ php生成图片上生成文字时候怎么样才能首行缩进
这个是我一个多图片拼接、文字拼接的 例子。你看一下
$bgimg = "bg.jpg";
$bg_info = getimagesize($bgimg);
list($width,$height) = $bg_info;
//获取图片的后缀
$bg_type = image_type_to_extension($bg_info[2], false);
//拼接图片资源句柄函数
$func = 'imagecreatefrom' . $bg_type;
//创建图片资源句柄
$bg_image = $func($bgimg);
//图1处理
$tu1 = "tu1.jpg";
$tu1_info = getimagesize($tu1);
$tu1_type = image_type_to_extension($tu1_info[2], false);
//创建图片资源句柄
$tu1_func = 'imagecreatefrom' . $tu1_type;
$tu1_image = $tu1_func($tu1);
//合并背景图+图1
image($bg_image, $tu1_image, 230, 600, 0, 0, 220, 280); //图1 200距左边距 50距顶边距 301 299图1大小 背景图 0 0
//图2处理 二维码
$tu2 = "tu2.jpg";
$tu2_mark = getimagesize($tu2);
$tu2_type = image_type_to_extension($tu2_mark[2], false);
//创建图片资源句柄
$tu2_func = 'imagecreatefrom' . $tu2_type;
$tu2_image = $tu2_func($tu2);
//合并背景图+二维码
image($bg_image, $tu2_image, 233, 950, 0, 0, 1000, 650);
//文字
$ = imagecolorallocate($bg_image, 255, 255, 255);
$font = "msyh.ttc"; //字体文件
$content = ['戴雯羽', '9','陈晨','圣艺美术书法学校','hmzg201911301744','变色龙','金'];
$font_lposition = [650, 650,650,650,2177,2160,1800]; //距左边距
$font_tposition = [680, 740,790,845,438,580,720]; //距上边距
if (count($content) > 0) {
for ($i = 0; $i < count($content); $i++) {
imagettftext($bg_image, 30, 0, $font_lposition[$i], $font_tposition[$i], 155, $font, $content[$i]);
}
}
//图片输出函数拼接
$outFunc = 'image' . $bg_type;
//保存图片
$img_name = "new.jpg";
$outFunc($bg_image, $img_name);
echo "<img src='$img_name' style='width:80%'>";
//销毁内存
imagedestroy($bg_image);
imagedestroy($tu1_image);
‘陆’ 如何用php把文字转变成图片.也就是往网页输入文字.通过网站后台生成png图片
首先要确定你的环境支持GD库;
程序很简单:
$str = "测试一下";//输入的文字
header("Content-type: image/jpeg");
$im = imagecreate(100, 30) or die("Cannot Initialize new GD image stream");//图片大小
$str=iconv("gb2312","UTF-8",$str);
for($i=0;$i<200;$i++) //加入干扰象素
{
$clr = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($im, rand()%100 , rand()%50 , $clr);
}
//$str="sss";
$black = imagecolorallocate($im, 0, 0, 0);
$fnt = "c:\windows\fonts\simhei.ttf"; //字体文件
ImageTTFText($im, 15, 0, 10, 20, $black, $fnt, $str);
imagejpeg($im);
//imagepng($im);
imagedestroy($im);
‘柒’ 如何将文字用php转换成图片
看一些关于gd库的函数,相关链接http://manual.phpv.net/zh/function.imagejpeg.php
‘捌’ 求代码示例:php将数据库读取出来的文字转成图片显示在页面上
<?php
$Phone=18907975647;#手机号码,具体从数据库怎么读出来,你自己写代码
$im=imagecreate(300,30);#建立一个宽300,高30像素的图片对象
imagecolorallocate($im,255,255,255);#将图片背景填充为白色
$Color=imagecolorallocate($im,0,0,0);#在生成一黑色色颜色,以便写入字符串
imagestring($im,16,0,0,$Phone,$Color);#将字符串写到图片上
header('content-type:image/*');//设置文件头为图片格式
imagepng($im);//输出一个png格式的图片
imagedestroy($im);//销毁图片对象
下面写效果图:
‘玖’ php生成图片显示文字
<img src="img.php?color=red&frist='123'&second='456'" />
这样就可以了吧
‘拾’ php如何生成加粗或者斜体的文字样式图片
加粗或者斜体的文字可以用php的函数控制.我想你是想生成验证码图片是吗?
如果是想生成验证么图片有几个函数可以考虑
imagecreate($length,$height)创建图片.参数是图片的宽度和高度
imagecolorallocate($image,$r,$g,$b)设置背景色,r b g就是图片的三色rgb参数.这个可以由传入0-255的随机数决定随机的背景色.还可以生成字体色
imagettftext($_image,$fontSize,mt_rand(-40,70),$codeNX,$fontSize*1.5,$_color,$ttf,$code[$i]);写入随机的文字,这里要一个字一个字写.所以这个函数要循环调用.
网络了一下 找到了一个类...如下
<?php
/**
*安全验证码
*
*安全的验证码要:验证码文字扭曲、旋转,使用不同字体,添加干扰码。
*如果用中文做验证码(我这里不是哦,有兴趣你来改成用中文的),安全度会更好些,但验证码扭曲和旋转是王道,用了字体也算是已经给字体扭曲了,我就不再去给他添一只扭曲的足了。
*可配置的属性都是一些简单直观的变量,我就不用弄一堆的setter/getter了
*
*@author流水孟春<cmpan(at)qq.com>
*@rightNEWBSD
*@linkhttp://labs.yulans.cn/YL_Security_Secoder
*@linkhttp://wiki.yulans.cn/docs/yl/security/secoder
*/
classYL_Security_Secoder{
/**
*验证码的session的下标
*
*@varstring
*/
publicstatic$seKey='sid.sekey.ylans.cn';
publicstatic$expire=3000;//验证码过期时间(s)
/**
*验证码中使用的字符,01IO容易混淆,建议不用
*
*@varstring
*/
publicstatic$codeSet='346789ABCDEFGHJKLMNPQRTUVWXY';
publicstatic$fontSize=25;//验证码字体大小(px)
publicstatic$useCurve=true;//是否画混淆曲线
publicstatic$useNoise=true;//是否添加杂点
publicstatic$imageH=0;//验证码图片宽
publicstatic$imageL=0;//验证码图片长
publicstatic$length=4;//验证码位数
publicstatic$bg=array(243,251,254);//背景
protectedstatic$_image=null;//验证码图片实例
protectedstatic$_color=null;//验证码字体颜色
/**
*输出验证码并把验证码的值保存的session中
*验证码保存到session的格式为:$_SESSION[self::$seKey]=array('code'=>'验证码值','time'=>'验证码创建时间');
*/
publicstaticfunctionentry(){
//图片宽(px)
self::$imageL||self::$imageL=self::$length*self::$fontSize*1.5+self::$fontSize*1.5;
//图片高(px)
self::$imageH||self::$imageH=self::$fontSize*2;
//建立一幅self::$imageLxself::$imageH的图像
self::$_image=imagecreate(self::$imageL,self::$imageH);
//设置背景
imagecolorallocate(self::$_image,self::$bg[0],self::$bg[1],self::$bg[2]);
//验证码字体随机颜色
self::$_color=imagecolorallocate(self::$_image,mt_rand(1,120),mt_rand(1,120),mt_rand(1,120));
//验证码使用随机字体
$ttf=dirname(__FILE__).'/ttfs/'.mt_rand(1,20).'.ttf';
if(self::$useNoise){
//绘杂点
self::_writeNoise();
}
if(self::$useCurve){
//绘干扰线
self::_writeCurve();
}
//绘验证码
$code=array();//验证码
$codeNX=0;//验证码第N个字符的左边距
for($i=0;$i<self::$length;$i++){
$code[$i]=self::$codeSet[mt_rand(0,27)];
$codeNX+=mt_rand(self::$fontSize*1.2,self::$fontSize*1.6);
//写一个验证码字符
imagettftext(self::$_image,self::$fontSize,mt_rand(-40,70),$codeNX,self::$fontSize*1.5,self::$_color,$ttf,$code[$i]);
}
//保存验证码
isset($_SESSION)||session_start();
$_SESSION[self::$seKey]['code']=join('',$code);//把校验码保存到session
$_SESSION[self::$seKey]['time']=time();//验证码创建时间
header('Cache-Control:private,max-age=0,no-store,no-cache,must-revalidate');
header('Cache-Control:post-check=0,pre-check=0',false);
header('Pragma:no-cache');
header("content-type:image/png");
//输出图像
imagepng(self::$_image);
imagedestroy(self::$_image);
}
/**
*画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)
*
*高中的数学公式咋都忘了涅,写出来
* 正弦型函数解析式:y=Asin(ωx+φ)+b
*各常数值对函数图像的影响:
*A:决定峰值(即纵向拉伸压缩的倍数)
*b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
*φ:决定波形与X轴位置关系或横向移动距离(左加右减)
*ω:决定周期(最小正周期T=2π/∣ω∣)
*
*/
protectedstaticfunction_writeCurve(){
$A=mt_rand(1,self::$imageH/2);//振幅
$b=mt_rand(-self::$imageH/4,self::$imageH/4);//Y轴方向偏移量
$f=mt_rand(-self::$imageH/4,self::$imageH/4);//X轴方向偏移量
$T=mt_rand(self::$imageH*1.5,self::$imageL*2);//周期
$w=(2*M_PI)/$T;
$px1=0;//曲线横坐标起始位置
$px2=mt_rand(self::$imageL/2,self::$imageL*0.667);//曲线横坐标结束位置
for($px=$px1;$px<=$px2;$px=$px+0.9){
if($w!=0){
$py=$A*sin($w*$px+$f)+$b+self::$imageH/2;//y=Asin(ωx+φ)+b
$i=(int)((self::$fontSize-6)/4);
while($i>0){
imagesetpixel(self::$_image,$px+$i,$py+$i,self::$_color);//这里画像素点比imagettftext和imagestring性能要好很多
$i--;
}
}
}
$A=mt_rand(1,self::$imageH/2);//振幅
$f=mt_rand(-self::$imageH/4,self::$imageH/4);//X轴方向偏移量
$T=mt_rand(self::$imageH*1.5,self::$imageL*2);//周期
$w=(2*M_PI)/$T;
$b=$py-$A*sin($w*$px+$f)-self::$imageH/2;
$px1=$px2;
$px2=self::$imageL;
for($px=$px1;$px<=$px2;$px=$px+0.9){
if($w!=0){
$py=$A*sin($w*$px+$f)+$b+self::$imageH/2;//y=Asin(ωx+φ)+b
$i=(int)((self::$fontSize-8)/4);
while($i>0){
imagesetpixel(self::$_image,$px+$i,$py+$i,self::$_color);//这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多
$i--;
}
}
}
}
/**
*画杂点
*往图片上写不同颜色的字母或数字
*/
protectedstaticfunction_writeNoise(){
for($i=0;$i<10;$i++){
//杂点颜色
$noiseColor=imagecolorallocate(
self::$_image,
mt_rand(150,225),
mt_rand(150,225),
mt_rand(150,225)
);
for($j=0;$j<5;$j++){
//绘杂点
imagestring(
self::$_image,
5,
mt_rand(-10,self::$imageL),
mt_rand(-10,self::$imageH),
self::$codeSet[mt_rand(0,27)],//杂点文本为随机的字母或数字
$noiseColor
);
}
}
}
/**
*验证验证码是否正确
*
*@paramstring$code用户验证码
*@returnbool用户验证码是否正确
*/
publicstaticfunctioncheck($code){
isset($_SESSION)||session_start();
//验证码不能为空
if(empty($code)||empty($_SESSION[self::$seKey])){
returnfalse;
}
//session过期
if(time()-$_SESSION[self::$seKey]['time']>self::$expire){
unset($_SESSION[self::$seKey]);
returnfalse;
}
if($code==$_SESSION[self::$seKey]['code']){
returntrue;
}
returnfalse;
}
}
//useage
/*
YL_Security_Secoder::$useNoise=false;//要更安全的话改成true
YL_Security_Secoder::$useCurve=true;
YL_Security_Secoder::entry();
*/
/*
//验证验证码
if(!YL_Security_Secoder::check(@$_POST['secode'])){
print'errorsecode';
}
*/
这是效果