tags:

views:

141

answers:

1

I have file A2.txt with coordinate x1,y1,x2,y2 in every line like below :

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  

in my php file i have that code. This code should take every line and draw line with coordinate from line. But something was wrong cause nothing was draw :/:

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

If I use imagejpeg and imagedestroy in while its only first line draw. What to do to draw every line ?? Please help :)

+4  A: 

Unstructured, no cleanup or error checking example:

<?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:

result of script

Artefacto
just great solution ;) Big thanks!
netmajor