Reputation: 1288
Maybe someone could help me with this, couse I'm stuck here. So I have zem.png image which is 560x225 size. It has transparent background. I need to draw lines from x,y to x1, x2 with gd library (I need to draw 5-7 lines, but it would be perfect to get a simple example how to draw one line).
Here's how my transparent image is created:
// Kuriame paveiksleli pasinaudodami GD library keliams tarp miestu pavaizduoti
$im = imagecreatetruecolor(560, 225);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
// Issaugome paveiksleli
imagepng($im, './zem.png');
imagedestroy($im);
After creating this image I'm using it as table's or div's background image. My table ir div is divided to blocks on x - 35 blocks, on y 25.
I have 4 points or 4 blocks selected on which I place other images, here's how I'm generating those rondom blocks:
$x = rand(1, 35);
$y = rand(1, 25);
$x2 = rand(1, 35);
$y2 = rand(1, 25);
$x3 = rand(1, 35);
$y3 = rand(1, 25);
$x4 = rand(1, 35);
$y4 = rand(1, 25);
Single block size is 16x9. And I need to draw a line from every generated block to other block (as representing roads from cities), so I have to mulptiply my x (x2,x3..) by 16 and my y's by 9 to know exacly right coordinates of lines start and ending points. So I'm doing this:
// Breziame kelius
$kordinate = $x * 16;
$kordinate2 = $y * 9;
$kordinate3 = $x2 * 16;
$kordinate4 = $y2 * 9;
Okey I have coordinates for one line now. And there's the place I'm stuck. I tried a lot of examples, of people precreated funkcions and ect. but I still can't draw a line using php gd library. So maybe anyone could suggest something? To add something to imagecreating code, or simply remove it and just leave blank transparent image and open it and then draw a line...
Upvotes: 0
Views: 2252
Reputation: 1852
I always prepare a class and function when I make a table or a map image. I'm not sure if it helps but here is my sample code.You can just name this file GridTb.php and run it.
<?php
header('Content-type: image/png');
$GridTb = new GridTb();
$GridTb->pngfile(330, 700);
class GridTb {
function pngfile($width, $height) {
define("WIDTH", $width);
define("HEIGHT",$height);
$png_image = imagecreate(WIDTH, HEIGHT);
imagecolorallocate($png_image, 255, 255, 255);
imagesetthickness($png_image, 1);
$black = imagecolorallocate($png_image, 0, 0, 0);
$x = 0;
$y = 0;
$w = imagesx($png_image) - 1;
$z = imagesy($png_image) - 1;
//basic square frame
imageline($png_image, $x, $y, $x, $y + $z, $black);
imageline($png_image, $x, $y, $x + $w, $y, $black);
imageline($png_image, $x + $w, $y, $x + $w, $y + $z, $black);
imagerectangle($png_image, $x, $y + $z, $x + $w, $y + $z, $black);
$wid = 30;
// $h=40;
for ($row = 0; $row < 10; $row++) {
imageline($png_image, $wid, HEIGHT, $wid, 0, $black);
$wid+=30;
imageline($png_image, $wid, HEIGHT, $wid, 0, $black);
for ($h = 40; $h < 701; $h++) {
$h2 = array(60,200,150,150,100);
imageline($png_image, WIDTH, $h, 0, $h, $black);
$h+=60;
imageline($png_image, WIDTH, $h, 0, $h, $black);
$h+=200;
imageline($png_image, WIDTH, $h, 0, $h, $black);
$h+=150;
imageline($png_image, WIDTH, $h, 0, $h, $black);
$h+=150;
imageline($png_image, WIDTH, $h, 0, $h, $black);
$h+=100;
imageline($png_image, WIDTH, $h, 0, $h, $black);
//sum of $h = 700
}
}
imagepng($png_image);
imagedestroy($png_image);
}
}
?>
<IMG src="GridTb.php">
Upvotes: 1