how can i make chess like pattern image using gd library

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neovantage
    New Member
    • Aug 2008
    • 245

    how can i make chess like pattern image using gd library

    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

    Code:
    //$t_im having image path, $t_wt,$t_ht is specified width and height respectively.
    $blue = imagecolorallocate($t_im,149,0,0);
    imagefilledrectangle($t_im,0,0,$t_wt,$t_ht,$blue);
    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 Files
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Here is the skeleton algorithm for outputting a chessboard:

    Code:
    <?php
    
    // Chess board is 8x8
    // Every row and column the colors swap.
    
    $array = array('b', 'w');
    
    // Rows
    for ($y = 0; $y < 8; $y++) {
    	// Columns
    	for	($x = 0; $x < 8; $x++) {
    		// print the first index of array
    		print $array[0];
    		// and then switch the array values.
    		$array = array_reverse($array);
    	}
    	print "\n";
    	$array = array_reverse($array);
    }

    Comment

    • neovantage
      New Member
      • Aug 2008
      • 245

      #3
      Yeh thx but how can i integrate this with my script.
      I mean say i want to create an image of chess like pattern
      Code:
      <?
      header("Content-type: image/jpeg");
      $t_im = imagecreatetruecolor("50","50");
      $blue = imagecolorallocate($t_im,149,0,0);
      imagefilledrectangle($t_im,0,0,$t_wt,$t_ht,$blue);
      ?>
      How can i apply your given algorithm in this code

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Hi, Neo.

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

        Mark.

        Code:
        <?php
        
        // Create image of set size - must be divisible by 8 
        // (8 columns / rows in a chess board)
        $img = imagecreatetruecolor(160, 160);
        
        // define the colors
        // these are in an array so we can conveniently and 
        // efficiently reverse them.
        $colors = array(
        	imagecolorallocate($img, 255, 0, 0), // Red
        	imagecolorallocate($img, 0, 0, 255)	 // Blue
        );
        
        // 8 rows
        for ($y = 0; $y < 8; $y++) {
        	// 8 columns
        	for ($x = 0; $x < 8; $x++) {
        		// Fill in a rectangle on our main image
        		imagefilledrectangle(
        			// the image resource (line 5)
        			$img,
        			// The starting x co-ordinate
        			// If we are on loop 2 of the outter loop, $y would be 1.
        			// The following line would evaluate to (1 * 20) 20. Ergo, the 
        			// coord would be plotted at that position (from the top).
        			// The 20 here is our rectangle height (as is with all the following
        			// 20s)
        			($y * 20),
        			// The starting y co-ordinate.
        			($x * 20),
        			// This time we add 20 to our starting x coord to find our
        			// ending x coord.
        			($y * 20) + 20,
        			// Etc.
        			($x * 20) + 20,
        			// Pick the first color index.
        			$colors[0]
        		);
        		// Switch the colors to achieve alternating colors.
        		$colors = array_reverse($colors);
        	}
        	// Again switch the colors.
        	// Take this out to see why we do this.
        	$colors = array_reverse($colors);
        }
        
        header("content-type: image/png");
        
        imagepng($img);
        imagedestroy($img);

        Comment

        • neovantage
          New Member
          • Aug 2008
          • 245

          #5
          Thanks it works but i did a lit alteration in this code.
          Code:
          $t_im = imagecreatetruecolor($t_wt,$t_ht);
          	    $t2_im = imagecreatetruecolor($t2_wt,$t2_ht);
          	    
          		$fekete = imagecolorallocate($t_im,255,115,114);
          		$feher = imagecolorallocate($t_im,226,227,228);
          		
          		imagefill($t_im,0,0,$feher);
          		$max_x = ceil($t_wt / 10);
          		$max_y = ceil($t_ht / 10);
          		$x = 0;
          		$y = 0;
          		for($i = 1; $i <= $max_y; $i++){
          			if(($i%2) == 0){
          				$x = 0;
          			}else{
          				$x = 10;
          			}
          			for($j = 1; $j <= $max_x; $j++){
          				imagefilledrectangle($t_im,$x,$y,($x+10),($y+10),$fekete);
          				$x = $x + 20;
          			}
          			$y+=10;
          		}
          Once again thanks a lot for this sir

          Comment

          Working...