PHP通过多维数组urlencode urldecode创建GD图片

PHP GD Image creation by passing multidimensional array urlencode urldecode

我正在尝试使用 php GD 生成图表。我正在将数据从 html 传递到控制器图形函数。整个过程在我的本地主机上运行,​​但在我的实时服务器上,图形图像没有创建。我在本地 ubuntu 14.04 机器上使用 PHP 5.5...实时服务器在 debian wheezy 上使用 Php5.4...我已经检查了两台机器 GD 库是否已安装。 .

在检查了一些之后,我怀疑我在做 url 编码和 url 解码时做错了,或者在我看来,在创建多维数组时做错了。

这是我的控制器图形函数:

public function graph1()
{
include('phpgraphlib.php');
$graph = new PHPGraphLib(600,300);

$data1 = unserialize(urldecode(stripslashes($_GET['mydata1'])));

$line1 = array_column($data1, 'line1');
$line2 = array_column($data1, 'line2');


$graph->addData($line1,$line2);
$graph->setBars(false);
//$graph->setGrid(false);
$graph->setGridColor("238, 238, 238");
$graph->setLine(true);
$graph->setDataPoints(true);
$graph->setDataPointColor('160,160,160');
$graph->setDataValues(true);
$graph->setDataValueColor('0,0,0');
$graph->setGoalLine(.0025);
$graph->setGoalLineColor('red');
$graph->setLineColor("255, 167, 78", "67, 162, 255","blue");
//$graph->setLegend(true);
//$graph->setLegendTitle("Apples", "Pears");
$graph->createGraph();
}

这是我传递数据的观点:

<?php

$data1 = array();

foreach ($line1 as $field => $label){
if (count($points) > 0 && isset($points[0]->$field)){
  $i = 0; 
  $total = count($points); 
  $total = $total > 30 ? 30 : $total;
  foreach ($points as $point){
    if ($i >= $total){
      break;
    }

    $data1 []['line1'] = $point -> $field; 
    $i++;
  }
  }
  }

foreach ($line2 as $field => $label){
 if (count($points) > 0 && isset($points[0]->$field)){
  $i = 0; 
  $total = count($points); 
  $total = $total > 30 ? 30 : $total;
  foreach ($points as $point){
    if ($i >= $total){
      break;
    }

    $data1 []['line2'] = $point -> $field; 
    $i++;
  }
}
}

<img src="<?php echo site_url('/g1/g2/graph1')."?mydata1=".urlencode(serialize($data1)); ?>" />

此代码不会在实时服务器上创建图像。如果有人可以建议我该怎么做,真的很棒吗?

这是来自我的本地主机的 imgsrc 的 url 值:

http://localhost/site/index.php/g1/g2/graph1?mydata1=a%3A12%3A%7Bi%3A0%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A2%3A%2292%22%3B%7Di%3A1%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22144%22%3B%7Di%3A2%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22110%22%3B%7Di%3A3%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22100%22%3B%7Di%3A4%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22365%22%3B%7Di%3A5%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22185%22%3B%7Di%3A6%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22176%22%3B%7Di%3A7%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22234%22%3B%7Di%3A8%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22358%22%3B%7Di%3A9%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22201%22%3B%7Di%3A10%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22144%22%3B%7Di%3A11%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22112%22%3B%7D%7D

这是来自 Live 服务器的 img src 的 url 值:

https://mysite.com/index.php/g1/g2/graph1?mydata1=a%3A12%3A%7Bi%3A0%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A2%3A%2292%22%3B%7Di%3A1%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22144%22%3B%7Di%3A2%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22110%22%3B%7Di%3A3%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22100%22%3B%7Di%3A4%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22365%22%3B%7Di%3A5%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22185%22%3B%7Di%3A6%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22176%22%3B%7Di%3A7%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22234%22%3B%7Di%3A8%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22358%22%3B%7Di%3A9%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22201%22%3B%7Di%3A10%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22144%22%3B%7Di%3A11%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22112%22%3B%7D%7D

此外,如果我设置 pdf debug tru,则实时服务器中会显示此调试错误:

mPDF error: IMAGE Error (https://mysite.com/index.php/g1/g2/graph1?mydata1=a%3A12%3A%7Bi%3A0%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A2%3A%2292%22%3B%7Di%3A1%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22144%22%3B%7Di%3A2%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22110%22%3B%7Di%3A3%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22100%22%3B%7Di%3A4%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22365%22%3B%7Di%3A5%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22185%22%3B%7Di%3A6%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22176%22%3B%7Di%3A7%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22234%22%3B%7Di%3A8%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22358%22%3B%7Di%3A9%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22201%22%3B%7Di%3A10%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22144%22%3B%7Di%3A11%3Ba%3A1%3A%7Bs%3A5%3A%22line1%22%3Bs%3A3%3A%22112%22%3B%7D%7D): Could not find image file

如您所见,PHP 函数 array_column 的生命始于 PHP 5.5。

http://php.net/manual/en/function.array-column.php (PHP 5 >= 5.5.0)

因此对于 5.4 友好代码,您可以替换:

$line1 = array_column($data1, 'line1');
$line2 = array_column($data1, 'line2');

有:

$line1 = array();
$line2 = array();

foreach ( $data1 as $line_array ) {
    if ( isset($line_array['line1']) ) { $line1[] = $line_array['line1']; }
    if ( isset($line_array['line2']) ) { $line2[] = $line_array['line2']; }
}

这里有一个演示,你可以在这里打卡:http://writecodeonline.com/php/

<?php

$data1 = array(
      array('line1' => 165)
    , array('line1' => 180)
    , array('line1' => '')
    , array('line1' => 140)
    , array('line2' => '')
    , array('line2' => 50)
    , array('line2' => 68)
    , array('line2' => 80)
);

$line1 = array();
$line2 = array();

foreach ( $data1 as $line_array ) {
    if ( isset($line_array['line1']) ) { $line1[] = $line_array['line1']; }
    if ( isset($line_array['line2']) ) { $line2[] = $line_array['line2']; }
}

print_r($line1);
print_r($line2);

?>

print_r -ing $line1 和 $line2 给我:

Array
(
    [0] => 165
    [1] => 180
    [2] => 
    [3] => 140
)
Array
(
    [0] => 
    [1] => 50
    [2] => 68
    [3] => 80
)