Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于csv数据的php绘图_Php_Html_Csv - Fatal编程技术网

基于csv数据的php绘图

基于csv数据的php绘图,php,html,csv,Php,Html,Csv,我要做的是从csv文件中加载一些值,并使用它们作为x,y值来绘制一些矩形 我正在加载文件,但不是显示图像,而是输出原始图像数据。我知道在html代码中我可以使用 <img scr="foo.php"></script> 索引php代码 <html> <body> <?php include("parse.php"); ?> </body> </

我要做的是从csv文件中加载一些值,并使用它们作为x,y值来绘制一些矩形

我正在加载文件,但不是显示图像,而是输出原始图像数据。我知道在html代码中我可以使用

<img scr="foo.php"></script> 
索引php代码

<html>
    <body>
        <?php
            include("parse.php");
        ?>
    </body>
</html>

解析php代码

<?php

include("draw.php");

$file = fopen("data.csv", "r");
while (!feof($file)) {
    $line = fgetcsv($file);

        drawGraph($line[0], $line[1], $line[2], $line[3]);

}
fclose($file);
?>

绘制php代码

<?php

function drawGraph($xPos, $yPos, $xxPos, $yyPos) {

//create a 200 x 200 canvas image
$canvas = imagecreatetruecolor(200, 200);

//set canvas background to white
$white = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $white);

//create colors
$pink = imagecolorallocate($canvas, 255, 105, 180);

//draw rectangles
imagerectangle($canvas, $xPos, $yPos, $xxPos, $yyPos, $pink);

//ERROR - following line displays raw data of image not the actural image
imagepng($canvas);

imagedestroy($canvas);
}

?>

您需要传递一个内容类型标题,告诉浏览器数据是图像。如果您使用drawGraph函数执行此操作,它将输出原始数据

<html>
    <body>
            <?php
                include("parse.php");
            ?>
        </body>
</html>

您需要传递一个内容类型标题,告诉浏览器数据是图像。如果您使用drawGraph函数执行此操作,它将输出原始数据

<html>
    <body>
            <?php
                include("parse.php");
            ?>
        </body>
</html>


事实上,我并不完全理解您的意思,但这可能会有所帮助:在推送原始数据之前,记得先做标题('Content-Type:image/png')。您能发布生成的HTML吗?这是一些没有标题的HTML输出‰png IHDRÈÈ“:9É=IDATxœÜÁ1950a;·t’SÈA^fæ/t’nclp¸A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$头加上一个原始数据链接显示,我不太了解,所以我不太清楚,但这可能会有帮助:在推送原始数据之前,记得做标题('Content-Type:image/png')。你能发布生成的HTML吗?这是一些没有标题的HTML输出‰png IHDRÈÈ“:9É=IDATxœÜÁ1950a;·t’SÈA^fæ/t’nclp¸A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’A’’!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,Â"!,EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$EBX$头添加原始数据显示,所以我已经尝试过使用png图像头的内容不正确,但它不起作用。你确定你已经删除了所有无关的字符吗?这是教科书上的例子,好吧,我只是再次尝试将头添加到index.php文件中。它仍然不起作用,但像以前一样,显示图像的“断开链接”图标。我敢打赌,您缺少一些尾随空格,或者您的标题格式不正确。如果您不能立即看到它,则必须对浏览器进行一些调试,以查看它接收到的内容。你不能用“”来表示文件。我不明白你为什么不能用“”来表示文件。php文件不应该能够“返回”浏览器显示的图像吗?我已经尝试了一个内容类型为image/png的标题,但它不起作用。你确定已经删除了所有无关字符吗?这是教科书上的例子,好吧,我只是再次尝试将头添加到index.php文件中。它仍然不起作用,但像以前一样,显示图像的“断开链接”图标。我敢打赌,您缺少一些尾随空格,或者您的标题格式不正确。如果您不能立即看到它,则必须对浏览器进行一些调试,以查看它接收到的内容。你不能用“”来表示文件。我不明白你为什么不能用“”来表示文件。php文件不应该能够“返回”浏览器显示的图像吗?
//create a 200 x 200 canvas image
$canvas = imagecreatetruecolor(200, 200);

//set canvas background to white
$white = imagecolorallocate($canvas, 255, 255, 255);
imagefill($canvas, 0, 0, $white);

//create colors
$pink = imagecolorallocate($canvas, 255, 105, 180);

$file = fopen("data.csv", "r");
while (!feof($file)) {
    $line = fgetcsv($file);

    //draw rectangles
    imagerectangle($canvas, $line[0], $line[1], $line[2], $line[3], $pink);

}

fclose($file);


/** finally output entire single png **/    
//ERROR - following line displays raw data of image not the actural image
imagepng($canvas);

imagedestroy($canvas);