使用坐标PHP从txt文件

使用坐标PHP从txt文件

问题描述:

拉绳我有文件A2.txt与坐标X1,Y1,X2,Y2中的每一行象下面这样:使用坐标PHP从txt文件

204 13 225 59 
225 59 226 84 
226 84 219 111 
219 111 244 192 
244 192 236 209 
236 209 254 223 
254 223 276 258 
276 258 237 337 

在我的PHP文件,我有一个代码。此代码应占用每一行,并从行中绘制坐标。但是,什么是错的原因是什么抽奖:/:

<?php 
$plik = fopen("A2.txt", 'r') or die("blad otarcia"); 
while(!feof($plik)) 
{ 
    $l = fgets($plik,20); 
    $k = explode(' ',$l); 

    imageline ($mapa , $k[0] , $k[1] , $k[2] , $k[3] , $kolor); 
} 
imagejpeg($mapa); 
imagedestroy($mapa); 
fclose($plik) ; 
?> 

如果我使用imagejpeg和imagedestroy中,而其唯一的一线平局。怎么做绘制每一行? 请帮助:)

+1

如何'$ mapa'产生的? – Matt 2010-06-05 02:18:10

+0

这是什么意思“在......中”? while循环的内部? – 2010-06-05 02:18:54

非结构化,不清理或错误校验的例子:

<?php 
$plik = <<<EOD 
204 13 225 59 
225 59 226 84 
226 84 219 111 
219 111 244 192 
244 192 236 209 
236 209 254 223 
254 223 276 258 
276 258 237 337 
EOD; 

$plik = preg_replace('/\r\n?/', "\n", $plik); 

$arr = explode("\n", $plik); 
array_walk($arr, 
    function (&$value, $key) { 
     $value = explode(' ', $value); 
    } 
); 

$minwidth = array_reduce($arr, 
    function ($res, $val) { return min($res, $val[0], $val[2]); }, 
    PHP_INT_MAX); 
$maxwidth = array_reduce($arr, 
    function ($res, $val) { return max($res, $val[0], $val[2]); }, 
    (PHP_INT_MAX * -1) - 1); 
$minheight = array_reduce($arr, 
    function ($res, $val) { return min($res, $val[1], $val[3]); }, 
    PHP_INT_MAX); 
$maxheight = array_reduce($arr, 
    function ($res, $val) { return max($res, $val[1], $val[3]); }, 
    (PHP_INT_MAX * -1) - 1); 


/* note: The image does not reflect the "+ 1"'s I added in a subsequent edit */ 
$mapa = imagecreatetruecolor($maxwidth - $minwidth + 1, $maxheight - $minheight + 1); 
$kolor = imagecolorallocate($mapa, 100, 200, 50); 

foreach ($arr as $k) { 
    imageline($mapa, 
     $k[0] - $minwidth, 
     $k[1] - $minheight, 
     $k[2] - $minwidth, 
     $k[3] - $minheight, $kolor); 
} 
header("Content-type: image/png"); 
imagepng($mapa); 

结果:

result of script

+0

只是很好的解决方案;)非常感谢! – netmajor 2010-06-05 12:14:39