繁体   English   中英

无法使用PHP在html中显示png图像[关闭]

[英]Cannot display png image in html with PHP [closed]

尽管尝试了很多尝试来使它起作用,但我还是很沮丧。 我在许多其他文章中都曾解释说,我对PHP还是很陌生,并不熟悉其强大功能,并且知道我几乎可以使它起作用,但似乎无法实现。 我已经有一个非常乐于助人的人试图为我做些愚蠢的事[一个链接] https://stackoverflow.com/a/13872071/1880796,但现在我只需要有人纠正我的代码中的错误即可工作。 我认为网址末尾有问题,因为当我尝试查看图像并查看网址时,所有内容都弄乱了,所以我不知道自己在做什么错。 我已经仔细检查了所有的数学运算,因为它是如此令人困惑,但这是result.php:

<?php
    $filename = "Results.txt";
    $lines = file($filename);

    $q1 = $_POST['q1']; // stores checked button value
    $q2 = $_POST['q2'];
    $q3 = $_POST['q3'];
    $q4 = $_POST['q4'];
    $q5 = $_POST['q5'];
    $q6 = $_POST['q6'];
    $q7 = $_POST['q7'];
    $q8 = $_POST['q8'];

    $qN = 1;  //question number

    $newLines = '';

    $total = array();

        foreach($lines as $line) {

            $line = trim($line);  //remove excess newlines etc.
            $lineArr = explode(',',$line);  //split line into array by commas
            $index = ${'q'.$qN}-1; //zero based
        if (isset($lineArr[$index])){
            $lineArr[$index]++;  //add to position by one vote.
            $total = array_sum($lineArr); //number of clicks 

            $newLines .= implode(',',$lineArr) . "\r\n"; //newLines contains numbers then also
        $qN++;
        }
    }
//write contents back to file.
file_put_contents($filename, $newLines);

//frequencies for each line without strings
$l[1] = explode(',',$lines[0]);
$l[2] = explode(',',$lines[1]);
$l[3] = explode(',',$lines[2]);
$l[4] = explode(',',$lines[3]);
$l[5] = explode(',',$lines[4]);
$l[6] = explode(',',$lines[5]);
$l[7] = explode(',',$lines[6]);
$l[8] = explode(',',$lines[7]);

//multiply each 
$result = array();
$values = array(1,2,3,4,5);
for($i=0;$i<count($l);$i++){
    for($j=0;$j<count($values);$j++){
    $result[$i+1][$j] = $l[$i+1][$j] * $values[$j];
    }
}
//calculate percentages

$p1 = (explode(',',$lines[0]));
$p2 = (explode(',',$lines[1]));
$p3 = (explode(',',$lines[2]));
$p4 = (explode(',',$lines[3]));
$p5 = (explode(',',$lines[4]));
$p6 = (explode(',',$lines[5]));
$p7 = (explode(',',$lines[6]));
$p8 = (explode(',',$lines[7]));

// calculate averages *****For some reason most recent frequency item updated is off by one, possibly bc not up to date yet???? figure out later!!!
$f1avg = round((array_sum($result[1])/($total-1)),2);
$f2avg = round((array_sum($result[2])/($total-1)),2);
$f3avg = round((array_sum($result[3])/($total-1)),2);
$f4avg = round((array_sum($result[4])/($total-1)),2);
$f5avg = round((array_sum($result[5])/($total-1)),2);
$f6avg = round((array_sum($result[6])/($total-1)),2);
$f7avg = round((array_sum($result[7])/($total-1)),2);
$f8avg = round((array_sum($result[8])/($total-1)),2);
//pie chart question 1 information
$p1f0 = round((($p1[0]/($total-1))*360),0);
$p1f1 = round((($p1[1]/($total-1))*360),0);
$p1f2 = round((($p1[2]/($total-1))*360),0);
$p1f3 = round((($p1[3]/($total-1))*360),0);
$p1f4 = round((($p1[4]/($total-1))*360),0);
//_____________$a0________$a1______________$a2___________________$a3__________________________$a4_______________
$pC1 = array(($p1f0),($p1f0+$p1f1),($p1f0+$p1f1+$p1f2),($p1f0+$p1f1+$p1f2+$p1f3),($p1f0+$p1f1+$p1f2+$p1f3+$p1f4));
print_r ($pC1);

这是饼图脚本:

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

//create pie charts
$image=imagecreatetruecolor(51,51);
//_____________________colors____________________________
$my_colorA=imagecolorallocate($image,51,51,255);
$my_colorB=imagecolorallocate($image,100,150,215);
$my_colorC=imagecolorallocate($image,20,20,151);
$my_colorX=imagecolorallocate($image,216,216,255);


$red = imagecolorallocate($image, 255, 0, 0);
$orange = imagecolorallocate($image, 191, 64, 0);
$dark_yellow = imagecolorallocate($image, 128, 128, 0);
$dark_green = imagecolorallocate($image, 64, 191, 0);
$green = imagecolorallocate($image, 0, 255, 0);
//_______________________________________________________
imagefill($image,0,0,$my_colorX);

//__________________ , center,  w , h , st, end, clr  ,    type______
imagefilledarc($image,25,25, 50, 50,  0,  $a0, $red, IMG_ARC_PIE);
imagefilledarc($image,25,25, 50, 50, $a0,  $a1, $orange, IMG_ARC_PIE);
imagefilledarc($image,25,25, 50, 50, $a1, $a2, $dark_yellow , IMG_ARC_PIE);
imagefilledarc($image,25,25, 50, 50, $a2,  $a3, $dark_green, IMG_ARC_PIE);
imagefilledarc($image,25,25, 50, 50, $a3,  $a4, $green, IMG_ARC_PIE);
//___________________________________________________________________

imagepng($image);
imagedestroy($image);

我不知道为什么它不起作用,但是当我单击查看图像时我得到了:PieChart.php?a0 = 33&a1 = 66&a2 = 230&a3 = 263&a4 = 361 /%3E%3C / td%3E%3Ctd%20style =

所以是的,我不知道我在做什么错! 请帮忙!

哦,最后这就是我要放入HTML中的内容。

img src = \\“ PieChart.php?a0 =”。$ pC1 [0]。“&a1 =”。$ pC1 [1]。 “&a2 =”。$ pC1 [2]。 “&a3 =”。$ pC1 [3]。“&a4 =”。$ pC1 [4]。“ /”

是的,我知道它需要img标签和php并回显我只是无法将其发布在表单中。

因此,解决方案碰巧是某种形式的语法错误,但对于任何希望对上述代码有深入了解的人来说...

1)通过对输入值进行硬编码并查看结果来检查脚本。
2)检查从调用函数传递的参数。

之后,以上代码示例与php手册中的代码示例极为相似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM