469,269 Members | 1,022 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,269 developers. It's quick & easy.

how can i make chess like pattern image using gd library

245 100+
Hey geeks,
i want to draw a jpeg image of pattern chess.
I can draw solid color image using gd libraray and here is my code

Expand|Select|Wrap|Line Numbers
  1. //$t_im having image path, $t_wt,$t_ht is specified width and height respectively.
  2. $blue = imagecolorallocate($t_im,149,0,0);
  3. imagefilledrectangle($t_im,0,0,$t_wt,$t_ht,$blue);
  4.  
But i want to draw image like a chess have pattern. Please find the attahced image for reference that i am interested to draw like that
Attached Images
 
Nov 11 '09 #1
4 4455
Markus
6,050 Expert 4TB
Here is the skeleton algorithm for outputting a chessboard:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Chess board is 8x8
  4. // Every row and column the colors swap.
  5.  
  6. $array = array('b', 'w');
  7.  
  8. // Rows
  9. for ($y = 0; $y < 8; $y++) {
  10.     // Columns
  11.     for    ($x = 0; $x < 8; $x++) {
  12.         // print the first index of array
  13.         print $array[0];
  14.         // and then switch the array values.
  15.         $array = array_reverse($array);
  16.     }
  17.     print "\n";
  18.     $array = array_reverse($array);
  19. }
  20.  
Nov 11 '09 #2
neovantage
245 100+
Yeh thx but how can i integrate this with my script.
I mean say i want to create an image of chess like pattern
Expand|Select|Wrap|Line Numbers
  1. <?
  2. header("Content-type: image/jpeg");
  3. $t_im = imagecreatetruecolor("50","50");
  4. $blue = imagecolorallocate($t_im,149,0,0);
  5. imagefilledrectangle($t_im,0,0,$t_wt,$t_ht,$blue);
  6. ?>
  7.  
How can i apply your given algorithm in this code
Nov 11 '09 #3
Markus
6,050 Expert 4TB
Hi, Neo.

Please have a look at the following code - do your best to understand it and ask questions if you don't.

Mark.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Create image of set size - must be divisible by 8 
  4. // (8 columns / rows in a chess board)
  5. $img = imagecreatetruecolor(160, 160);
  6.  
  7. // define the colors
  8. // these are in an array so we can conveniently and 
  9. // efficiently reverse them.
  10. $colors = array(
  11.     imagecolorallocate($img, 255, 0, 0), // Red
  12.     imagecolorallocate($img, 0, 0, 255)     // Blue
  13. );
  14.  
  15. // 8 rows
  16. for ($y = 0; $y < 8; $y++) {
  17.     // 8 columns
  18.     for ($x = 0; $x < 8; $x++) {
  19.         // Fill in a rectangle on our main image
  20.         imagefilledrectangle(
  21.             // the image resource (line 5)
  22.             $img,
  23.             // The starting x co-ordinate
  24.             // If we are on loop 2 of the outter loop, $y would be 1.
  25.             // The following line would evaluate to (1 * 20) 20. Ergo, the 
  26.             // coord would be plotted at that position (from the top).
  27.             // The 20 here is our rectangle height (as is with all the following
  28.             // 20s)
  29.             ($y * 20),
  30.             // The starting y co-ordinate.
  31.             ($x * 20),
  32.             // This time we add 20 to our starting x coord to find our
  33.             // ending x coord.
  34.             ($y * 20) + 20,
  35.             // Etc.
  36.             ($x * 20) + 20,
  37.             // Pick the first color index.
  38.             $colors[0]
  39.         );
  40.         // Switch the colors to achieve alternating colors.
  41.         $colors = array_reverse($colors);
  42.     }
  43.     // Again switch the colors.
  44.     // Take this out to see why we do this.
  45.     $colors = array_reverse($colors);
  46. }
  47.  
  48. header("content-type: image/png");
  49.  
  50. imagepng($img);
  51. imagedestroy($img);
  52.  
Nov 11 '09 #4
neovantage
245 100+
Thanks it works but i did a lit alteration in this code.
Expand|Select|Wrap|Line Numbers
  1. $t_im = imagecreatetruecolor($t_wt,$t_ht);
  2.         $t2_im = imagecreatetruecolor($t2_wt,$t2_ht);
  3.  
  4.         $fekete = imagecolorallocate($t_im,255,115,114);
  5.         $feher = imagecolorallocate($t_im,226,227,228);
  6.  
  7.         imagefill($t_im,0,0,$feher);
  8.         $max_x = ceil($t_wt / 10);
  9.         $max_y = ceil($t_ht / 10);
  10.         $x = 0;
  11.         $y = 0;
  12.         for($i = 1; $i <= $max_y; $i++){
  13.             if(($i%2) == 0){
  14.                 $x = 0;
  15.             }else{
  16.                 $x = 10;
  17.             }
  18.             for($j = 1; $j <= $max_x; $j++){
  19.                 imagefilledrectangle($t_im,$x,$y,($x+10),($y+10),$fekete);
  20.                 $x = $x + 20;
  21.             }
  22.             $y+=10;
  23.         }
  24.  
Once again thanks a lot for this sir
Nov 12 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Michel | last post: by
13 posts views Thread by Brian | last post: by
1 post views Thread by Paul Franklin | last post: by
5 posts views Thread by Will McGugan | last post: by
11 posts views Thread by Gregc. | last post: by
5 posts views Thread by Paolo Pantaleo | last post: by
63 posts views Thread by biyubi | last post: by
2 posts views Thread by CoreyWhite | last post: by
2 posts views Thread by eureka2050 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.