PHP图像处理
GD2
Jpgraph
创建一个画布:
<?php
header('content-type:image/gif');
//echo "你好";
$im = imagecreate(200,60);
$white = imagecolorallocate($im ,225 ,66 ,159);
imagegif($im);
?>
显示一张图片
<?php
function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefromjpeg($imgname);
/* See if it failed */
if(!$im){
echo "aaa";
/* Create a blank image */
$im = imagecreatetruecolor(150,40);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 40, $bgc);
/* Output an error message */
imagestring($im, 1, 5, 5, 'bb ' . $imgname, $tc);
}
return $im;
}
header('Content-Type: image/jpeg');
$img = LoadPNG('p.jpg');
imagepng($img);
imagedestroy($img);
?>
随机生成验证码:
<?php
session_start();
header('Content-Type: image/png');
$image_width = 70;
$image_height = 18;
srand(microtime()*100000);
$new_number = '';
for($i = 0 ;$i < 4 ;$i ++){
$new_number.=dechex(rand(0 ,15));
}
$_SESSION['check_checks'] = $new_number;
$num_image = imagecreate($image_width ,$image_height);
imagecolorallocate($num_image ,255 ,255 ,255);
for($i = 0 ;$i < strlen($_SESSION['check_checks']) ;$i ++){
$font = mt_rand(3 ,5);
$x = mt_rand(1 ,8) + $image_width * $i / 4;
$y = mt_rand(1 ,$image_height / 4);
$color = imagecolorallocate($num_image ,mt_rand(0,100) ,mt_rand(0 ,150) ,mt_rand(0 ,200));
imagestring($num_image ,$font ,$x ,$y,$_SESSION['check_checks'][$i] ,$color);
}
imagepng($num_image);
imagedestroy($num_image);
?>
创建一个柱形图
<?php
include ("src/jpgraph.php");
include ("src/jpgraph_bar.php");
$datay = array(160 ,180 ,203 ,289 ,405 ,488 ,489 ,408 ,299 ,166 ,187 ,205);
$graph = new Graph(600 ,300 ,"auto");
$graph -> SetScale("textlin");
$graph -> yaxis -> scale -> SetGrace(20);
$graph -> SetShadow();
$graph -> img -> SetMargin(40 ,30 ,30 ,40);
$bplot = new BarPlot($datay);
$bplot -> SetFillColor('orange');
$bplot -> value -> Show();
$bplot -> value -> SetFormat('%d');
$graph -> Add($bplot);
$graph -> SetMarginColor("lightblue");
$graph -> title -> Set("tit");
$a = array(1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12);
$graph -> xaxis -> SetTickLabels($a);
$graph -> title -> SetFont(FF_SIMSUN);
$graph -> xaxis -> SetFont(FF_SIMSUN);
$graph -> Stroke();
?>
绘制折线图
<?php
include ("src/jpgraph.php");
include ("src/jpgraph_line.php");
$datay = array(160 ,180 ,203 ,289 ,405 ,488 ,489 ,408 ,299 ,166 ,187 ,500);
$graph = new Graph(600 ,300 ,"auto");
$graph -> img -> SetMargin(50 ,40 ,30 ,40);
$graph -> img -> SetAntiAliasing();
$graph -> SetScale("textlin");
$graph -> SetShadow();
$graph -> title -> Set("tit");
$graph -> title -> SetFont(FF_SIMSUN ,FS_BOLD);
$graph -> SetMarginColor("lightblue");
$graph -> yaxis ->title -> SetFont(FF_SIMSUN);
$graph -> xaxis -> SetPos("min");
$graph -> yaxis -> HideZeroLabel();
$graph -> ygrid -> SetFill(true ,'#EFEFEF@0.5' ,'#BBCCFF@0.5');
$a = array(1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12);
$graph -> xaxis -> SetTickLabels($a);
$graph -> xaxis -> SetFont(FF_SIMSUN);
$graph -> yscale -> SetGrace(20);
$p1 = new LinePlot($datay);
$p1 -> mark -> SetType(MARK_FILLEDCIRCLE);
$p1 -> mark ->SetFillColor('red');
$p1 -> mark ->SetWidth(4);
$p1 -> SetColor('blue');
$p1 -> SetCenter();
$graph -> Add($p1);
$graph -> Stroke();
?>
3D饼状图
<?php
include ("src/jpgraph.php");
include ("src/jpgraph_pie.php");
include ("src/jpgraph_pie3d.php");
$data = array(160 ,180 ,203 ,289 ,405 ,500 );
$graph = new PieGraph(540 ,260 ,"auto");
$graph -> SetShadow();
$graph -> title -> Set("tit");
$graph -> title -> SetFont(FF_SIMSUN ,FS_BOLD);
$graph -> legend -> SetFont(FF_SIMSUN ,FS_NORMAL);
$p1 = new PiePlot3D($data);
$p1 -> SetLegends(array('11','22','33','44','55','66'));
$targ = array("pie3d_csimex1.php?v=1","pie3d_csimex1.php?v=2","pie3d_csimex1.php?v=3","pie3d_csimex1.php?v=4","pie3d_csimex1.php?v=5","pie3d_csimex1.php?v=6");
$alts = array("val=%d" ,"val=%d" ,"val=%d" ,"val=%d" ,"val=%d" ,"val=%d");
$p1 -> SetCSIMTargets($targ ,$alts);
$p1 -> SetCenter(0.4 ,0.5);
$graph -> Add($p1);
$graph -> StrokeCSIM();
?>