畫布的默認背景色是黑色,背景與文字邊界像素的顏色是兩種顏色融合後的,所以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';
}
*/
這是效果