本文目錄一覽:
- 1、php 如何把文字轉成圖片顯示(象驗證碼那樣)
- 2、PHP圖片生成
- 3、php生成圖片顯示文字
- 4、怎麼用php生成圖像,生成驗證碼
- 5、用php代碼怎麼以背景圖片加上文字生成新的圖片,然後在標題處絕對調用該圖片?
- 6、PHP圖片中文驗證碼生成問題
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圖片生成
給你一個php 圖像處理類,完全能實現你的功能,你自己研究一下吧
?php
class image
{
var $w_pct = 50; //透明度
var $w_quality = 80; //質量
var $w_minwidth = 300; //最小寬
var $w_minheight = 300; //最小高
var $thumb_enable; //是否生成縮略圖
var $watermark_enable; //是否生水印
var $interlace = 0; //圖像是否為隔行掃描的
var $fontfile; //字體文件
var $w_img ; //默認水印圖
function __construct()
{
global $SITE_CONFING;
$this-thumb_enable = $SITE_CONFING[‘thumb_enable’];
$this-watermark_enable = $SITE_CONFING[‘watermark_enable’];
$this-set($SITE_CONFING[‘watermark_minwidth’], $SITE_CONFING[‘watermark_minheight’], $SITE_CONFING[‘watermark_quality’], $SITE_CONFING[‘watermark_pct’], $SITE_CONFING[‘watermark_fontfile’],$SITE_CONFING[‘watermark_img’]);
}
function image()
{
$this-__construct();
}
function set($w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100,$fontfile,$w_img)
{
$this-w_minwidth = $w_minwidth;
$this-w_minheight = $w_minheight;
$this-w_quality = $w_quality;
$this-w_pct = $w_pct;
$this-fontfile = $fontfile;
$this-w_img = $w_img;
}
function info($img)
{
$imageinfo = getimagesize($img); //返回圖像信息數組 0=寬的像素 1=高的像素 2=是圖像類型的標記 3 =是文本字符串,內容為“height=”yyy” width=”xxx””,
if($imageinfo === false) return false;
$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //獲取圖像文件類型 $imageinfo[2]是圖像類型的標記
$imagesize = filesize($img); //圖像大小
$info = array(
‘width’=$imageinfo[0],
‘height’=$imageinfo[1],
‘type’=$imagetype,
‘size’=$imagesize,
‘mime’=$imageinfo[‘mime’]
);
return $info;
}
function thumb($image, $filename = ”, $maxwidth = 200, $maxheight = 50, $suffix=’_thumb’, $autocut = 0)
{
if(!$this-thumb_enable || !$this-check($image)) return false;
$info = $this-info($image); //獲取圖片信息
if($info === false) return false;
$srcwidth = $info[‘width’]; //源圖寬
$srcheight = $info[‘height’]; //源圖高
$pathinfo = pathinfo($image);
$type = $pathinfo[‘extension’]; //取得擴展名
if(!$type) $type = $info[‘type’]; //如果沒有取到,用$info[‘type’]
$type = strtolower($type);
unset($info);
$scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //獲取縮略比例
//獲取按照源圖的比列
$createwidth = $width = (int)($srcwidth*$scale); //取得縮略寬
$createheight = $height = (int)($srcheight*$scale); //取得縮略高
$psrc_x = $psrc_y = 0;
if($autocut) //按照縮略圖的比例來獲取
{
if($maxwidth/$maxheight$srcwidth/$srcheight $maxheight=$height) //如果縮略圖按比列比源圖窄的話
{
$width = $maxheight/$height*$width; //寬按照相應比例做處理
$height = $maxheight; //高不變
}
elseif($maxwidth/$maxheight$srcwidth/$srcheight $maxwidth=$width)//如果縮略圖按比列比源圖寬的話
{
$height = $maxwidth/$width*$height;
$width = $maxwidth;
}
$createwidth = $maxwidth;
$createheight = $maxheight;
}
$createfun = ‘imagecreatefrom’.($type==’jpg’ ? ‘jpeg’ : $type); //找到不同的圖像處理函數
$srcimg = $createfun($image); //新建圖像
if($type != ‘gif’ function_exists(‘imagecreatetruecolor’))
$thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像
else
$thumbimg = imagecreate($width, $height); //新建一個基於調色板的圖像
if(function_exists(‘imagecopyresampled’)) //重採樣拷貝部分圖像並調整大小,真彩
//imagecopyresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)
imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
else //拷貝部分圖像並調整大小,調色板
imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
if($type==’gif’ || $type==’png’)
{
//imagecolorallocate 為一幅圖像分配顏色
$background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 給基於調色板的圖像填充背景色, 指派一個綠色
// imagecolortransparent 將某個顏色定義為透明色
imagecolortransparent($thumbimg, $background_color); // 設置為透明色,若注釋掉該行則輸出綠色的圖
}
// imageinterlace 激活或禁止隔行掃描
if($type==’jpg’ || $type==’jpeg’) imageinterlace($thumbimg, $this-interlace);
$imagefun = ‘image’.($type==’jpg’ ? ‘jpeg’ : $type);
//imagejpeg imagegif imagepng
if(empty($filename)) $filename = substr($image, 0, strrpos($image, ‘.’)).$suffix.’.’.$type; //獲取文件名
//aaa.gif aaa_thumb.gif
$imagefun($thumbimg, $filename); //新建圖像
imagedestroy($thumbimg); //銷毀縮略圖
imagedestroy($srcimg); //銷毀源圖
return $filename;
}
//watermark(源圖,生成文件,生成位置,水印文件,水印文本,背景色)
function watermark($source, $target = ”, $w_pos = 0, $w_img = ”, $w_text = ”, $w_font = 12, $w_color = ‘#cccccc’)
{
if(!$this-watermark_enable || !$this-check($source)) return false;
if(!$target) $target = $source;
if ($w_img == ” $w_text == ”)
$w_img = $this-w_img;
$source_info = getimagesize($source);
$source_w = $source_info[0]; //獲取寬
$source_h = $source_info[1]; //獲取高
if($source_w $this-w_minwidth || $source_h $this-w_minheight) return false; //寬和高達不到要求直接返回
switch($source_info[2]) //新建圖片
{
case 1 :
$source_img = imagecreatefromgif($source);
break;
case 2 :
$source_img = imagecreatefromjpeg($source);
break;
case 3 :
$source_img = imagecreatefrompng($source);
break;
default :
return false;
}
if(!empty($w_img) file_exists($w_img)) //水印文件
{
$ifwaterimage = 1; //是否水印圖
$water_info = getimagesize($w_img); //水印信息
$width = $water_info[0];
$height = $water_info[1];
switch($water_info[2])
{
case 1 :
$water_img = imagecreatefromgif($w_img);
break;
case 2 :
$water_img = imagecreatefromjpeg($w_img);
break;
case 3 :
$water_img = imagecreatefrompng($w_img);
break;
default :
return;
}
}
else
{
$ifwaterimage = 0;
//imagettfbbox 本函數計算並返回一個包圍着 TrueType 文本範圍的虛擬方框的像素大小。
//imagettfbbox ( 字體大小, 字體角度, 字體文件,文件 )
$temp = imagettfbbox(ceil($w_font*1.2), 0, $this-fontfile, $w_text);//取得使用 truetype 字體的文本的範圍
$width = $temp[4] – $temp[6]; //右上角 X 位置 – 左上角 X 位置
$height = $temp[3] – $temp[5]; //右下角 Y 位置- 右上角 Y 位置
unset($temp);
}
switch($w_pos)
{
case 0: //隨機位置
$wx = rand(0,($source_w – $width));
$wy = rand(0,($source_h – $height));
break;
case 1: //左上角
$wx = 5;
$wy = 5;
break;
case 2: //上面中間位置
$wx = ($source_w – $width) / 2;
$wy = 0;
break;
case 3: //右上角
$wx = $source_w – $width;
$wy = 0;
break;
case 4: //左面中間位置
$wx = 0;
$wy = ($source_h – $height) / 2;
break;
case 5: //中間位置
$wx = ($source_w – $width) / 2;
$wy = ($source_h – $height) / 2;
break;
case 6: //底部中間位置
$wx = ($source_w – $width) / 2;
$wy = $source_h – $height;
break;
case 7: //左下角
$wx = 0;
$wy = $source_h – $height;
break;
case 8: //右面中間位置
$wx = $source_w – $width;
$wy = ($source_h – $height) /2;
break;
case 9: //右下角
$wx = $source_w – $width;
$wy = $source_h – $height ;
break;
default: //隨機
$wx = rand(0,($source_w – $width));
$wy = rand(0,($source_h – $height));
break;
}
if($ifwaterimage) //如果有水印圖
{
//imagecopymerge 拷貝併合並圖像的一部分
//參數(源圖,水印圖,拷貝到源圖x位置,拷貝到源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)
imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this-w_pct);
}
else
{
if(!empty($w_color) (strlen($w_color)==7))
{
$r = hexdec(substr($w_color,1,2)); //獲取紅色
$g = hexdec(substr($w_color,3,2)); //獲取綠色
$b = hexdec(substr($w_color,5)); //獲取藍色
}
else
{
return;
}
//imagecolorallocate 基於調色板的圖像填充背景色
//imagestring 水平地畫一行字符串
//imagestring(源圖,字體大小,位置X,位置Y,文字,顏色)
//參數($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this-fontfile,$w_text);
//imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
}
//輸出到文件或者瀏覽器
switch($source_info[2])
{
case 1 :
imagegif($source_img, $target); //以 GIF 格式將圖像輸出到瀏覽器或文件
break;
case 2 :
imagejpeg($source_img, $target, $this-w_quality); //以 JPEG 格式將圖像輸出到瀏覽器或文件
break;
case 3 :
imagepng($source_img, $target); //以 PNG 格式將圖像輸出到瀏覽器或文件
break;
default :
return;
}
if(isset($water_info))
{
unset($water_info); //銷毀
}
if(isset($water_img))
{
imagedestroy($water_img); //銷毀
}
unset($source_info);
imagedestroy($source_img);
return true;
}
//gd庫必須存在,後綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在
function check($image)
{
return extension_loaded(‘gd’)
preg_match(“/\.(jpg|jpeg|gif|png)/i”, $image, $m)
file_exists($image)
function_exists(‘imagecreatefrom’.($m[1] == ‘jpg’ ? ‘jpeg’ : $m[1]));
//imagecreatefromjpeg
//imagecreatefromgif
//imagecreatefrompng
}
}
/**
縮略圖
1.新建一個圖像資源 通過 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
2.imagecopyresampled 拷貝圖像,並調整大小
水印:圖片水印,文字水印
1. 創建圖像
2.加水印
圖片水印:imagecopymerge 把2張圖合併在一起
文字水印:imagettftext 向圖像寫入文字
*/
?
php生成圖片顯示文字
img src=”img.php?color=redfrist=’123’second=’456′” /
這樣就可以了吧
怎麼用php生成圖像,生成驗證碼
?php
//驗證碼類
class ValidateCode {
private $charset = ‘abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789’;//隨機因子
private $code;//驗證碼
private $codelen = 4;//驗證碼長度
private $width = 90;//寬度
private $height = 40;//高度
private $img;//圖形資源句柄
private $font;//指定的字體
private $fontsize = 20;//指定字體大小
private $fontcolor;//指定字體顏色
//構造方法初始化
public function __construct() {
$this-font = dirname(__FILE__).’/font/elephant.ttf’;//注意字體路徑要寫對,否則顯示不了圖片
}
//生成隨機碼
private function createCode() {
$_len = strlen($this-charset)-1;
for ($i=0;$i$this-codelen;$i++) {
$this-code .= $this-charset[mt_rand(0,$_len)];
}
}
//生成背景
private function createBg() {
$this-img = imagecreatetruecolor($this-width, $this-height);
$color = imagecolorallocate($this-img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this-img,0,$this-height,$this-width,0,$color);
}
//生成文字
private function createFont() {
$_x = $this-width / $this-codelen;
for ($i=0;$i$this-codelen;$i++) {
$this-fontcolor = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this-img,$this-fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this-height / 1.4,$this-fontcolor,$this-font,$this-code[$i]);
}
}
//生成線條、雪花
private function createLine() {
//線條
for ($i=0;$i6;$i++) {
$color = imagecolorallocate($this-img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this-img,mt_rand(0,$this-width),mt_rand(0,$this-height),mt_rand(0,$this-width),mt_rand(0,$this-height),$color);
}
//雪花
for ($i=0;$i100;$i++) {
$color = imagecolorallocate($this-img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this-img,mt_rand(1,5),mt_rand(0,$this-width),mt_rand(0,$this-height),’*’,$color);
}
}
//輸出
private function outPut() {
header(‘Content-type:image/png’);
imagepng($this-img);
imagedestroy($this-img);
}
//對外生成
public function doimg() {
$this-createBg();
$this-createCode();
$this-createLine();
$this-createFont();
$this-outPut();
}
//獲取驗證碼
public function getCode() {
return strtolower($this-code);
}
}
用php代碼怎麼以背景圖片加上文字生成新的圖片,然後在標題處絕對調用該圖片?
?php
ob_clean(); //清除輸出緩存
header(“Content-type:image/jpeg”); //設置輸出類型
$img=”images/test.jpg”; //背景圖片名
if(isset($_GET[“img”]))$img=$_GET[“img”]; //也可以通過img參數傳入
$im=imagecreatefromjpeg($img); //讀入背景圖片
$text=”文字內容”; //要加上的文字內容
if(isset($_GET[“text”]))$text=$_GET[“text”]; //也可以通過text參數傳入
$fontFile=”xxx.ttf”; //字體文件名,必須要
$fontSize=36; //字體尺寸
$fontColor=ImageColorAllocate($im,0,0,0); //字體顏色,這裡是黑色
$textAngle=0; //文字顯示的角度,0表示水平顯示
$textLeft=20; //文字顯示的x坐標
$textTop=60; //文字顯示的y坐標
imagefttext($im,$fontSize,$textAngle,$textLeft,$textTop,$fontColor,$fontFile,$text); //把文字覆蓋到圖片上
Imagejpeg($im); //輸出圖片
ImageDestroy($im); //銷毀圖片
?
把以上文字保存為php文件,比如 img.php
然後在需要調用圖片的地方用 img src=”img.php?img=背景圖片文件路徑text=要加上的文字”/ 來調用
比如 img src=”img.php?img=images/back.jpgtext=你好”/
PHP圖片中文驗證碼生成問題
重新寫了一下,這裡不能print那個字符串..不然會出錯
因為啟動了session 之前不能有輸出
最終$_SESSION[‘yzm’]就是圖片上的驗證碼..
——————–
?php
session_start();
$im = imagecreatetruecolor(80,30);
$bg = imagecolorallocate($im,0,0,0);
$te = imagecolorallocate($im,255,255,255);
for($i=0;$i5;$i++)
{
$te2 = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,80),0,80,rand(0,30),$te2);
}
for($i=0;$i200;$i++)
{
imagesetpixel($im,rand()%100,rand()%30,$te2);
}
srand((float) microtime() * 10000000);
$input = array(“步”,”步”,”高”,”請”,”轉”,”到”,”圖”,”像”,”我”,”非”,”常”,”高”,”興”);
$rand_keys = array_rand($input, 4);
$_SESSION[‘yzm’] = $t = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]].$input[$rand_keys[3]];
imagettftext($im,12,0,10,20,$te,’stxingka.ttf’,$t);
header(“content-type: image/jpeg”);
imagejpeg($im);
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/206366.html