473,800 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can i make chess like pattern image using gd library

245 New Member
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 4903
Markus
6,050 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
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

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

Similar topics

9
2938
by: Michel | last post by:
I am trying to display a position, on a chessboard, corresponding to a sequence of moves recorded in a MySql database. Any clue? Michel
13
2339
by: Brian | last post by:
Hi all... This question is more for the GURUs out there. It is not a question on how to do something, but why it happens, and I am trying to figure out if there is a pattern. I am using IE, but this pattern also happens in Mozilla, but not always the same way... I am not as interested in how the browsers are different, but the big question is: Is there a pattern to what type of actions are delayed for after the scripts have finished, and...
1
2572
by: Paul Franklin | last post by:
Hi, I am writing some C++ code, and the problem is analogous to the following situation: Take a Chess Board of 8x8 (nxn) blocks and I have 8 (n) horses. Like in chess, the horses are placed at a distance of 2 and a half blocks apart from the preceding horse. For example, Horse B is at a distance of 2 and a half blocks from horse A, Horse C is at a distance of 2 and a half blocks from horse B and so on. If one of the horses is moved to...
5
3767
by: Will McGugan | last post by:
Hi folks, I've written a Python chess module that does the following. * Reads / Writes PGN files * Write FEN files * Validates moves * Lists legal moves * Detects check / mate / stalemate / 50 move rule / threefold repetition
11
2590
by: Gregc. | last post by:
G'day I am writing a chess program. Here is my code: #include <stdio.h> #include <stdlib.h> bool isInCheck (int krow, int kcol, int qrow, int qcol) { double check;
5
6915
by: Paolo Pantaleo | last post by:
Well Python is not a good language for writing a chess engine (even if a chess engine exists: http://www.kolumbus.fi/jyrki.alakuijala/pychess.html), but it could be grat for chess interfaces, for drawing boards, and similar things. I foudn out a library for these things (http://www.alcyone.com/software/chess/). Does anyone konw about more chess related modules? Thnx PAolo
63
15283
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html http://www.mailcom.com/ioccc/toledo/hint.htmBut this post is because I have discovered (asurprise for me) that it is also the worldsmallest chess program written in C language.It has a size of 3004 bytes, or 2261 bytes simplydeleting all the spacing that makes the knightfigure, cutting...
2
3121
by: CoreyWhite | last post by:
When playing games, perhaps the most simple is tic-tac-toe. The game has two simple strategies, one is defensive and the other offensive. It is not hard at first to learn how to tie games when playing an opponent. And then the next stage in development comes after you learn how to beat an opponent. You really can only employ either strategy when you get to make the first move, and your opponent will quickly learn what you are doing...
2
3493
by: eureka2050 | last post by:
Greetings everyone, I am a PHP beginner and this is my first time here. I am using the pdf2html program which generates an image of every corresponding PDF page. Every image file basically contains 3 bar graphs. Next using the GD library I slice this image into 3 individual images containing one bar graph each (please see attachment). Now, I need to check for the presence of certain symbols on the left and right hand side of every bar.. kinda...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.