Jump to content

[SOLVED] PHP "crawling" an image or generating an image from random variables


kittrellbj

Recommended Posts

Hiya!

 

Here is my predicament:  I am attempting to design a browser-based strategy game involving space conquest.  I can deal with all the game mechanics themselves with great ease, but what I am having problems with planning out is the way the mapping system will work.

 

In essence, there are two primary choices.  1) I draw each map out in photoshop, and design a PHP script to crawl over the images and generate data into the database (basically, where it finds this pixel color in the map, it counts that as a point, and generates data for that point.  2) I design a PHP script to generate a random map, draw it based on its own findings, and generate the database information itself.

 

I would rather go with option #1, as I would have alot more control over how the "stars" are placed around the map, being able to make it feel more realistic (without have to teach PHP how to draw a starmap), as well as making sure the distances between stars are reasonable, and possibly adding in some "strategic locations" around the map that players would want to keep control over.

 

I have read a few tutorials on the GD library, but I am a bit lost as to how to implement this sort of activity.  If anyone can point me to some tutorials, that would be a big help.  I am not exactly sure how to begin looking for this kind of implementation, as searching for it in relation to game design doesn't come up with much (most game makers keep this kind of thing secret).  I am comfortable discussing this further if anyone has any ideas of other ways of going about it, of course.

Link to comment
Share on other sites

Can you not just make an image map ?

 

Or do i not understand what you are trying to achieve?

 

Well, yes, an image map will be part of it, but what I am trying to achieve is the PHP script to crawl over the image and generate solar systems whenever it encounters a pixel color (or some other control device).  So, it would generate a random solar system whenever it encountered the pure white pixel, use that coordinate set for the database key, and keep moving to the next one.

 

I wanted this part of it to be automated since the star maps will be rather (using the word lightly) large.

 

If you know how big the map image is, you already know the coordinates.

 

Top left would be 0, 0

 

Bottom right would be width, height

 

Or if you plan to tile it just multiply it by the number of tiles horizontally and vertically

 

Yes, but what I'm stuck on is the script that generates the map itself in the beginning.  The maps will be of a set size and dimension, but I am unsure as how to get PHP to crawl through a map and execute the commands when it encounters a star.

Link to comment
Share on other sites

In the GD function library you can use the imagecolorat() function to retrieve the color of a pixel. http://us.php.net/manual/en/function.imagecolorat.php

 

You could use a simple nested for() loop to go through each pixel row (or column, whatever) searching for a specific pixel color. Of course this will be a slow operation, but thankfully it's likely to only be run a few times to set everything up.

 

Edit, To elaborate:

 

<?php
$imageWidth = 800;
$imageHeight = 600;

for($x = 0; $x < $imageWidth; ++$x) {
    for($y = 0; $y < $imageHeight; ++$y) {
        $color = imagecolorat($image, $x, $y);
        // Do SOMETHING
    }
}

Link to comment
Share on other sites

Ok, you will need these two functions first of all

 

http://uk3.php.net/manual/en/function.imagecolorat.php

http://uk3.php.net/manual/en/function.getimagesize.php

second one only if you don't know the image size

 

And then what i would do is loop using a for loop, and have it go pixel by pixel ( across the top) and when it reaches the edge of the image get it to go down one, etc.

 

$image_x = 200;
$image_y = 200;
$y = 0;
for($x = 0;$x <= $image_x;$x++
{
if($x == $image_x)
{
	$x = 0;
	$y += 1;
}
if($y == $image_y)
	$y = 0;

$color = imagecolorat  ($image,$x,$y);

$y ++;
}

Slightly improved

Link to comment
Share on other sites

Ah, very interesting indeed.  Yes, I just spent a few moments reading about the function for finding pixel color and started thinking along the same lines as what you two posted.

 

This will be very interesting indeed. :)

 

The only thing I'm thinking is that Blade's might turn into an infinite loop unless another control is placed inside the function, such as a total count for loop, taking into account the total size of the image.  (Unless $x is not returned to 0 for the for() loop when it gets back to the top).

 

i.e.

$image_total = 40000;
$image_x = 200;
$image_y = 200;
$y = 0;
for($n = 0;$n <= $image_total;$n++
{
if($x == $image_x)
{
$x = 0;
$y += 1;
}
if($y == $image_y)
$y = 0;

$color = imagecolorat  ($image,$x,$y);
}

 

That way it will break out when it has gone through every pixel (by count) in the image.  If this is not the case, feel free to correct.

 

And looking at generic's, I believe it will go through them all without an infinite loop.

Link to comment
Share on other sites

How are stars defined on your map? Are they simple 1x1px squares or are they larger more complex patterns?

 

You could build an array containing the relative coordinates to the center of each star then throw on a padding so coming to within say 10px of a star results in a collision.

Link to comment
Share on other sites

<?php
/* 
   function to get all x,y coords of specified rgb color
   4 arguments: path/to/image, and red, green, blue values
   of target pixel to search for.  function assumes $pic
   to be a .jpg type image.  You will have to change the
   code if you are using a different image type, or else 
   setup some conditions to determine which image type
   $pic is, and use the proper GD functions for it.
   $r, $g, $b each takes a 0-255 dec format value.  
*/	 
function getCoords ($pic, $r, $g, $b) {
   // put rgb args into array for comparison in loop   
   $argrgb = array('red' => $r, 'green' => $g, 'blue' => $b);

   // create image resource of picture 
   $image = imagecreatefromjpeg($pic);

   // get width and height of image
   list($width, $height) = getimagesize($pic);

   // loop through each column
   for ($y = 0; $y <= $height; $y++) {
      // loop through each pixel of the current row
      for ($x = 0; $x <= $width; $x++) {
         // get index of color at specific x,y coord
         $color_index = imagecolorat($image, $x, $y);
         // returns pixel color as array of red,green,blue,alpha
         $rgb = imagecolorsforindex($image, $color_index);		  
         // pop the alpha value off the end of the array
         array_pop($rgb); 
         // compare pixel arrays. if they are the same, empty array is returned
         $result = array_diff($rgb, $argrgb);
         // if empty array is returned...         
         if (empty($result))
            // add the coordinate to our list
            $dotCoords[] = array('x' => $x, 'y' => $y);
      } // end $x
   } // end $y
   // return list of coords
   return $dotCoords;
} // end getCoords

/*** example ***/
// target picture
$pic = "somepic.jpg";

// get all coords of pure white pixels
$whiteCoords = getCoords($pic, 255, 255, 255);
// output results
echo "<pre>"; print_r($whiteCoords); echo "</pre>";	 
?>

Link to comment
Share on other sites

:P

 

:o

 

Lol, anyways is it working for you?

 

I am actually still in the design stage of that part of the software, I was considering possibilities on how I would go about it exactly when I got to the actual coding stage of development.  That's why I went ahead and started talking about it now.  Implementation of it will probably be in a few weeks from now, but I wanted to make sure I knew what I was trying to do was doable and not severely, out-of-this-world complicated. :)

 

How are stars defined on your map? Are they simple 1x1px squares or are they larger more complex patterns?

 

You could build an array containing the relative coordinates to the center of each star then throw on a padding so coming to within say 10px of a star results in a collision.

 

Yes, the system will be designed to find the 1px by 1px white dots on the image.  The stars will, graphically, be more significant and visually pleasing, of course, but the program will find the white dots to decide the exact points of the stars for generation of universe data.  I could design a collision pattern system, but it will probably be more complicated and troublesome than it would be worth in the end.  So long as the program can find the center of the star image and define the coordinate for it, I am just fine with that.

 

Of course, it will have to be larger than 1px x 1px for the image map, so that users do not have to squint to click on a star system. :)

 

And, thanks Crayon Violent, I like the commenting and the example, exactly the implementation I will be using there that you have.  Thanks to everyone for their input on this subject, it was all extremely helpful.

Link to comment
Share on other sites

  • 2 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.