当前位置: 编程技术>php
本页文章导读:
▪php之XML转数组函数的详解
如下所示: 代码如下:<?/** * xml2array() will convert the given XML text to an array in the XML structure. * Link: http://www.bin-co.com/php/scripts/xml2array/ * Arguments : $contents - The XML text * $get_.........
▪利用php绘制饼状图的实现代码
drawPieImg()函数包含8个参数,$title为饼状图的标题;$dataArr为需要显示的数据数组;$labelArr为对应数据的标签分类数组;$colorArr为对应数据的绘图颜色数组,这4个参数是必须的,对于不同的.........
▪PHP自定义大小验证码的方法详解
代码如下:<?phpfunction vCode($num=4,$size=20, $width=0,$height=0){ !$width && $width = $num*$size*4/5+5; !$height && $height = $size + 10; // 去掉了 0 1 O l 等 .........
[1]php之XML转数组函数的详解
来源: 互联网 发布时间: 2013-11-30
如下所示:
<?
/**
* xml2array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/xml2array/
* Arguments : $contents - The XML text
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
* Examples: $array = xml2array(file_get_contents('feed.xml'));
* $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
*/
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag '<tag>'
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level-1];
}
}
return($xml_array);
}
?>
函数描述及例子:
$arr = xml2array(file_get_contents("tools.xml"),1,'attribute');
代码如下:
<?
/**
* xml2array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/xml2array/
* Arguments : $contents - The XML text
* $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
* $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
* Examples: $array = xml2array(file_get_contents('feed.xml'));
* $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
*/
function xml2array($contents, $get_attributes=1, $priority = 'tag') {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; //Refference
//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}
//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag '<tag>'
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}
} elseif($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level-1];
}
}
return($xml_array);
}
?>
函数描述及例子:
代码如下:
$arr = xml2array(file_get_contents("tools.xml"),1,'attribute');
[2]利用php绘制饼状图的实现代码
来源: 互联网 发布时间: 2013-11-30
drawPieImg()函数包含8个参数,$title为饼状图的标题;$dataArr为需要显示的数据数组;$labelArr为对应数据的标签分类数组;$colorArr为对应数据的绘图颜色数组,这4个参数是必须的,对于不同的系统应用传递相应的参数即可。接下来的4个参数,负责设置要生成的饼状图的大小,如果不设置则使用系统默认值。程序按照床底数组数据的大小,从0度开始绘制,方向按照顺时针方向依次绘制对应数据占据的扇面大小。
<?php
//变量定义,画椭圆弧时的角度大小
define("ANGLELENGTH",3);
/**
* 绘制图片
* @param $title 3D图的标题
* @param $dataArr 显示的数据数组
* @param $labelArr 对应数据的标签分类数组
* @param $colorArr 对应绘图颜色的数组
* @param $a 画布的基准宽度
* @param $b 画布的基准高度
* @param $v 3D柱的高度
* @param $font 字体大小
* @return 绘制成功的图片访问路径
*/
function drawPieImg($title, $dataArr, $labelArr, $colorArr, $a=250, $b=120, $v=20, $font=10){
$ox = 5+$a;
$oy = 5+$b;
$fw = imagefontwidth($font);
$fh = imagefontheight($font);
$n = count($dataArr);//计算数组长度
$w = 10+$a*2;
$h = 10+$b*2+$v+($fh+2)*$n;
//创建画板
$img = imagecreate($w, $h);
//转RGB为索引色
for($i=0; $i<$n; $i++)
$colorArr[$i] = drawIndexColor($img,$colorArr[$i]);//为图像$img分配颜色
$clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);
$clrt = imagecolorallocate($img, 0x00, 0x00, 0x00);
//填充背景色
imagefill($img, 0, 0, $clrbk);
//求和
$tot = 0;
for($i=0; $i<$n; $i++)
$tot += $dataArr[$i];
//每个分类的起始角度大小
$sd = 0;
//每个分类所占据的角度大小
$ed = 0;
$ly = 10+$b*2+$v;
for($i=0; $i<$n; $i++){
$sd = $ed;
$ed += $dataArr[$i]/$tot*360;
//画3d扇面
draw3DSector($img, $ox, $oy+20, $a, $b, $v, $sd, $ed, $colorArr[$i]);
//画标签
imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $colorArr[$i]);
imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt);
//中文转码
$str = iconv("GB2312", "UTF-8", $labelArr[$i]);
imagettftext($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "D:/wamp/www/source/font/simhei.ttf", $str.":".$dataArr[$i]."(".(round(10000*($dataArr[$i]/$tot))/100)."%)");
$ly += $fh+2;
}
//绘制图片标题
imagettftext($img, 15, 0, 5, 15, $clrt, "D:/wamp/www/source/font/simhei.ttf", iconv("GB2312", "UTF-8",$title));
//输出图形
header("Content-type: image/png");
//输出生成的图片
$imgFileName = "./".time().".png";
imagepng($img,$imgFileName);
return $imgFileName;
}
/**
* 绘制3d扇面
*/
function draw3DSector($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) {
drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);
if($sd<180){
list($red, $green, $blue) = drawDarkColor($img, $clr);
//为图像分配颜色
$clr=imagecolorallocate($img, $red, $green, $blue);
if($ed>180)
$ed = 180;
list($sx, $sy) = getExy($a,$b,$sd);
$sx += $ox;
$sy += $oy;
list($ex, $ey) = getExy($a, $b, $ed);
$ex += $ox;
$ey += $oy;
imageline($img, $sx, $sy, $sx, $sy+$v, $clr);
imageline($img, $ex, $ey, $ex, $ey+$v, $clr);
drawArc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr);
list($sx, $sy) = getExy($a, $b, ($sd+$ed)/2);
$sy += $oy+$v/2;
$sx += $ox;
imagefill($img, $sx, $sy, $clr);
}
}
/**
* 绘制椭圆弧
*/
function drawArc($img,$ox,$oy,$a,$b,$sd,$ed,$clr){
$n = ANGLELENGTH >0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;
$d = $sd;
list($x0,$y0) = getExy($a,$b,$d);
for($i=0; $i<$n; $i++){
$d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);
list($x, $y) = getExy($a, $b, $d);
imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
$x0 = $x;
$y0 = $y;
}
}
/**
* 绘制扇面
*/
function drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) {
$n = ANGLELENGTH > 0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;
$d = $sd;
list($x0,$y0) = getExy($a, $b, $d);
imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
for($i=0; $i<$n; $i++) {
$d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);
list($x, $y) = getExy($a, $b, $d);
imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
$x0 = $x;
$y0 = $y;
}
imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
list($x, $y) = getExy($a/2, $b/2, ($d+$sd)/2);
imagefill($img, $x+$ox, $y+$oy, $clr);
}
/**
* 根据$clr颜色获取对应的柱的阴影色
* @param $img 图像
* @param $clr 颜色
* @return rgb颜色数组
*/
function drawDarkColor($img,$clr){
$rgb = imagecolorsforindex($img,$clr);
return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
}
/**
* 求角度$d对应的椭圆上的点坐标
*
* @param $a 横坐标
* @param $b 纵坐标
* @param $d 角度
* @return 对应椭圆点坐标
*/
function getExy($a, $b, $d){
$d = deg2rad($d);
return array(round($a*cos($d)), round($b*sin($d)));
}
/**
* 为图像分配RGB索引色
*/
function drawIndexColor($img, $clr){
$red = ($clr>>16) & 0xff;
$green = ($clr>>8)& 0xff;
$blue = ($clr) & 0xff;
return imagecolorallocate($img, $red, $green, $blue);
}
//测试示例
$title = "动物园动物种类分布情况";
$dataArr = array(20, 10, 20, 20, 10, 20, 30, 10); //测试数据数组
$labelArr = array("大象", "长颈鹿", "鳄鱼", "鸵鸟", "老虎", "狮子", "猴子", "斑马");//标签
$colorArr = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); //对应颜色数组
$result = drawPieImg($title, $dataArr,$labelArr,$colorArr);
echo "<img src="/blog_article/.$result" mce_src="/blog_article/.$result">";
?>
代码如下:
<?php
//变量定义,画椭圆弧时的角度大小
define("ANGLELENGTH",3);
/**
* 绘制图片
* @param $title 3D图的标题
* @param $dataArr 显示的数据数组
* @param $labelArr 对应数据的标签分类数组
* @param $colorArr 对应绘图颜色的数组
* @param $a 画布的基准宽度
* @param $b 画布的基准高度
* @param $v 3D柱的高度
* @param $font 字体大小
* @return 绘制成功的图片访问路径
*/
function drawPieImg($title, $dataArr, $labelArr, $colorArr, $a=250, $b=120, $v=20, $font=10){
$ox = 5+$a;
$oy = 5+$b;
$fw = imagefontwidth($font);
$fh = imagefontheight($font);
$n = count($dataArr);//计算数组长度
$w = 10+$a*2;
$h = 10+$b*2+$v+($fh+2)*$n;
//创建画板
$img = imagecreate($w, $h);
//转RGB为索引色
for($i=0; $i<$n; $i++)
$colorArr[$i] = drawIndexColor($img,$colorArr[$i]);//为图像$img分配颜色
$clrbk = imagecolorallocate($img, 0xff, 0xff, 0xff);
$clrt = imagecolorallocate($img, 0x00, 0x00, 0x00);
//填充背景色
imagefill($img, 0, 0, $clrbk);
//求和
$tot = 0;
for($i=0; $i<$n; $i++)
$tot += $dataArr[$i];
//每个分类的起始角度大小
$sd = 0;
//每个分类所占据的角度大小
$ed = 0;
$ly = 10+$b*2+$v;
for($i=0; $i<$n; $i++){
$sd = $ed;
$ed += $dataArr[$i]/$tot*360;
//画3d扇面
draw3DSector($img, $ox, $oy+20, $a, $b, $v, $sd, $ed, $colorArr[$i]);
//画标签
imagefilledrectangle($img, 5, $ly, 5+$fw, $ly+$fh, $colorArr[$i]);
imagerectangle($img, 5, $ly, 5+$fw, $ly+$fh, $clrt);
//中文转码
$str = iconv("GB2312", "UTF-8", $labelArr[$i]);
imagettftext($img, $font, 0, 5+2*$fw, $ly+13, $clrt, "D:/wamp/www/source/font/simhei.ttf", $str.":".$dataArr[$i]."(".(round(10000*($dataArr[$i]/$tot))/100)."%)");
$ly += $fh+2;
}
//绘制图片标题
imagettftext($img, 15, 0, 5, 15, $clrt, "D:/wamp/www/source/font/simhei.ttf", iconv("GB2312", "UTF-8",$title));
//输出图形
header("Content-type: image/png");
//输出生成的图片
$imgFileName = "./".time().".png";
imagepng($img,$imgFileName);
return $imgFileName;
}
/**
* 绘制3d扇面
*/
function draw3DSector($img, $ox, $oy, $a, $b, $v, $sd, $ed, $clr) {
drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr);
if($sd<180){
list($red, $green, $blue) = drawDarkColor($img, $clr);
//为图像分配颜色
$clr=imagecolorallocate($img, $red, $green, $blue);
if($ed>180)
$ed = 180;
list($sx, $sy) = getExy($a,$b,$sd);
$sx += $ox;
$sy += $oy;
list($ex, $ey) = getExy($a, $b, $ed);
$ex += $ox;
$ey += $oy;
imageline($img, $sx, $sy, $sx, $sy+$v, $clr);
imageline($img, $ex, $ey, $ex, $ey+$v, $clr);
drawArc($img, $ox, $oy+$v, $a, $b, $sd, $ed, $clr);
list($sx, $sy) = getExy($a, $b, ($sd+$ed)/2);
$sy += $oy+$v/2;
$sx += $ox;
imagefill($img, $sx, $sy, $clr);
}
}
/**
* 绘制椭圆弧
*/
function drawArc($img,$ox,$oy,$a,$b,$sd,$ed,$clr){
$n = ANGLELENGTH >0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;
$d = $sd;
list($x0,$y0) = getExy($a,$b,$d);
for($i=0; $i<$n; $i++){
$d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);
list($x, $y) = getExy($a, $b, $d);
imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
$x0 = $x;
$y0 = $y;
}
}
/**
* 绘制扇面
*/
function drawSector($img, $ox, $oy, $a, $b, $sd, $ed, $clr) {
$n = ANGLELENGTH > 0 ? ceil(($ed-$sd)/ANGLELENGTH) : -1;
$d = $sd;
list($x0,$y0) = getExy($a, $b, $d);
imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
for($i=0; $i<$n; $i++) {
$d = ($d+ANGLELENGTH)>$ed?$ed:($d+ANGLELENGTH);
list($x, $y) = getExy($a, $b, $d);
imageline($img, $x0+$ox, $y0+$oy, $x+$ox, $y+$oy, $clr);
$x0 = $x;
$y0 = $y;
}
imageline($img, $x0+$ox, $y0+$oy, $ox, $oy, $clr);
list($x, $y) = getExy($a/2, $b/2, ($d+$sd)/2);
imagefill($img, $x+$ox, $y+$oy, $clr);
}
/**
* 根据$clr颜色获取对应的柱的阴影色
* @param $img 图像
* @param $clr 颜色
* @return rgb颜色数组
*/
function drawDarkColor($img,$clr){
$rgb = imagecolorsforindex($img,$clr);
return array($rgb["red"]/2,$rgb["green"]/2,$rgb["blue"]/2);
}
/**
* 求角度$d对应的椭圆上的点坐标
*
* @param $a 横坐标
* @param $b 纵坐标
* @param $d 角度
* @return 对应椭圆点坐标
*/
function getExy($a, $b, $d){
$d = deg2rad($d);
return array(round($a*cos($d)), round($b*sin($d)));
}
/**
* 为图像分配RGB索引色
*/
function drawIndexColor($img, $clr){
$red = ($clr>>16) & 0xff;
$green = ($clr>>8)& 0xff;
$blue = ($clr) & 0xff;
return imagecolorallocate($img, $red, $green, $blue);
}
//测试示例
$title = "动物园动物种类分布情况";
$dataArr = array(20, 10, 20, 20, 10, 20, 30, 10); //测试数据数组
$labelArr = array("大象", "长颈鹿", "鳄鱼", "鸵鸟", "老虎", "狮子", "猴子", "斑马");//标签
$colorArr = array(0x99ff00, 0xff6666, 0x0099ff, 0xff99ff, 0xffff99, 0x99ffff, 0xff3333, 0x009999); //对应颜色数组
$result = drawPieImg($title, $dataArr,$labelArr,$colorArr);
echo "<img src="/blog_article/.$result" mce_src="/blog_article/.$result">";
?>
[3]PHP自定义大小验证码的方法详解
来源: 互联网 发布时间: 2013-11-30
代码如下:
<?php
function vCode($num=4,$size=20, $width=0,$height=0){
!$width && $width = $num*$size*4/5+5;
!$height && $height = $size + 10;
// 去掉了 0 1 O l 等
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
$code = '';
for ($i=0; $i<$num; $i++){
$code.= $str[mt_rand(0, strlen($str)-1)];
}
// 画图像
$im = imagecreatetruecolor($width,$height);
// 定义要用到的颜色
$back_color = imagecolorallocate($im, 235, 236, 237);
$boer_color = imagecolorallocate($im, 118, 151, 199);
$text_color = imagecolorallocate($im, mt_rand(0,200), mt_rand(0,120), mt_rand(0,120));
// 画背景
imagefilledrectangle($im,0,0,$width,$height,$back_color);
// 画边框
imagerectangle($im,0,0,$width-1,$height-1,$boer_color);
// 画干扰线
for($i=0;$i<5;$i++){
$font_color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
imagearc($im,mt_rand(-$width,$width),mt_rand(-$height,$height),mt_rand(30,$width*2),mt_rand(20,$height*2),mt_rand(0,360),mt_rand(0,360),$font_color);
}
// 画干扰点
for($i=0;$i<50;$i++){
$font_color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$font_color);
}
// 画验证码
@imagefttext($im, $size , 0, 5, $size+3, $text_color, 'c://WINDOWS//Fonts//simsun.ttc',$code);
header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate");
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
}
?>
函数描述及例子:
<?
// 4个字符,大小为20
vCode(4,20);
?>
最新技术文章: