簡體   English   中英

使用txt文件中的坐標用php畫線

[英]draw line with php using coordinates from txt file

我在每行中都有坐標x1,y1,x2,y2的文件A2.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。 怎樣繪制每條線? 請幫忙 :)

非結構化,無清除或錯誤檢查的示例:

<?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);

結果:

腳本結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM