php 画图问题

小鱼2012 2014-01-14 02:51:50
<?php

//定数ファイルを読み込み
require_once("../../../app/webapp/const/const.php");
//DB
require_once(PATH."/db/MysqlPdo.php");
//共通関数
require_once(WEBAPP_FUNCTION_PATH."/common.php");

header("Content-Type:image/png");

$width = 240; //定义画布宽度
$height= 190; //定义画布高度
$unit = 40; //定义刻度间隔
$right = 20; //定义坐标系距画布右侧距离
$left = 30; //定义坐标系距画布左侧距离
$top = 20; //定义坐标系距画布上侧距离
$buttom= 30; //定义坐标系距画布下侧距离
$x = array(); //定义x坐标空数组
$y = array(); //定义y坐标空数组

$max=0;

$string_CO = 'CO';
$string_2 = '2';
$name = array( '0','6','12','18','24' ); //定义横坐标的坐标标签

$image = imagecreatetruecolor($width, $height);

//控制透明
imagesavealpha($image, true);

//透明色 127全透
$whiteTransparent = imagecolorallocatealpha($image, 255, 255, 255,127); //定义真彩画布及颜色值//

//定义颜色
$white = imagecolorallocate($image, 255, 255, 255);

imagefill( $image, 0, 0, $whiteTransparent ); //用透明色填充画布

//从数据库得到数据
$datas = getDrawLineDates();

if(!empty($datas)){
//对数据进行处理;
$datas = datas_convert($datas);
$data_co = $datas['co'];
// $data_co = array ( '0', '0', '0','0', '0', '0', '0', '0', '0','0', '0', '0', '10','0','0', '0');


// print_r($data_co);exit;

for ( $i = 0; $i < count($data_co); $i++ ){ //获取$data中最大值
if( $data_co[$i] > $max ){
$max = $data_co[$i];
}
}
//计算$data数组中每个元素的坐标
for ($i = 0;$i < count($data_co) ; $i++){
$x[$i] = $left + round( $i*160/144 );
$y[$i] = $top + round( $height - $top - $buttom )*( 1 - $data_co[$i]/$max );
}


//绘制数据折线图
for ( $i=0; $i<count($data_co); $i++ ){
if ( $i+1!=count($data_co) ){
imageline($image, $x[$i], $y[$i], $x[$i+1], $y[$i+1], $white);
}
}
}

//画出横坐标
for ( $i = 0;$i<5;$i++ ){

imageline($image, $left + $i*$unit, $height - $buttom, $left + $i*$unit, $height - $buttom - 4, $white);
imagestring($image, 1, $left+$i*$unit, $height-$buttom+1, $name[$i], $white);
}
//画出纵坐标
for( $i = 0; $i<5; $i++ ){
imageline($image, $left, round($top + ($height-$top-$buttom)*$i / 5), $left+5, round($top+($height-$top-$buttom)*$i/5), $white);
imagestring($image, 1, $left/4, $top+($height-$top-$buttom)*$i/5, round($max*(5-$i)/5), $white);
}

//画横坐标
imageline( $image, $left, $height-$buttom, $width-$right, $height-$buttom, $white );
//画纵坐标
imageline( $image, $left, $buttom, $left, $height-$buttom, $white );

//画箭头
imageline( $image, $width-$right, $height-$buttom, ($width-$right)-8,$height-$buttom-3, $white );
imageline( $image, $width-$right, $height-$buttom, ($width-$right)-8,$height-$buttom+3, $white );
imageline( $image, $left, $top/4+16, $left+3, $top/4+24, $white );
imageline( $image, $left, $top/4+16, $left-3, $top/4+24, $white );

//绘制图像说明
imageline($image, $left, 180, $left+10,180, $white);
imagestring($image, 2, $left+20, 175, $string_CO, $white);
imagechar($image, 1, $left+35, 179, $string_2, $white);
//ob_clean();
//输出图形
imagepng($image);

//销毁图形资源
imagedestroy($image);
为什么在数据库中新添加一条数据之后,网页动态刷新,更新画图图片,图片就不出了了


求大神指点!!
...全文
146 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2014-01-14
  • 打赏
  • 举报
回复
注释掉与外部文件相关的代码,你的图片可以正常生成
小鱼2012 2014-01-14
  • 打赏
  • 举报
回复
//得到drawline的当天数据 function getDrawLineDates(){ $date = date("Y/m/d 00:00:00"); //$date = date("Y/m/d H:i:s", time()); $Drawline_sql = new MysqlPdo(); $sql = "SELECT temprature, watertemprature, co, current_illumination_red, current_illumination_green, current_illumination_blue, updated_at FROM setting_mst WHERE updated_at >= '$date' ORDER BY updated_at"; $result = $Drawline_sql->getAll($sql); $var = array(); if(empty($result)){ $message = DB_ERROR; }else{ if(is_array($result)){ foreach($result as $key=>$value){ foreach($value as $key2=>$value2){ $var[$key2][] = $value2; } } } $Drawline_sql->close(); return $var; } } datas_convert($datas)方法把数据库当天不同时间段的数据放在一个数组中,每十分钟数据库中有一条数据,如果没有,该方法处理为 0 ,我打印过,得到的数据时没有错的,有时在本地画图好用,有时不好用,服务器也是,页面都是定时刷新的,不知是不是缓存问题,还是画图类方法中不可以用调太多其他的方法和类。
xuzuning 2014-01-14
  • 打赏
  • 举报
回复
你的程序没有给全,不好说是哪里出了毛病 不过贴出的代码本身并没有错误 可能是 BOM 头的影响 也可能问题出在未贴出的部分
小鱼2012 2014-01-14
  • 打赏
  • 举报
回复
自己顶~~~~~~~~~~~~~~~~!

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧