3

I wish to generate a picture with the php gd library but i have some bugs, sometimes that works sometimes it does not. The full image does not generate, only partially. Sometimes in diagonals.

I have a string such as this "color_color_color" and so on that I transform into an array to create a matrix of pixels, I then parse the array with a loop and create a picture with set pixel color. Then I generate a transparent color, save in png.

Sometimes that works but the first row and the first column are not generated.

<?php
//tell at the browser that is a picture
header("Content-Type: image/png");

$size = $_POST['size'];

//size of the picture
$image = imagecreatetruecolor($size, $size);


//color background example :  "grayOne_black_grayOne_white_" ect...
$first_array = explode("_", $_POST['field']);


 $white = imagecolorallocate($image, 255, 255, 255);
 $grayOne = imagecolorallocate($image, 225, 225, 225);
 $grayTwo = imagecolorallocate($image, 200, 200, 200);
 $grayThree = imagecolorallocate($image, 175, 175, 175);
 $grayFour = imagecolorallocate($image, 150, 150,150);
 $grayFive = imagecolorallocate($image, 125, 125, 125);
 $graySix = imagecolorallocate($image, 100, 100, 100);
 $graySeven = imagecolorallocate($image, 75, 75, 75);
 $grayHeight = imagecolorallocate($image, 50, 50, 50);
 $grayNine  = imagecolorallocate($image, 25, 25, 25);
 $black = imagecolorallocate($image, 0, 0, 0);

 // a variable to parse the array 
 $a = 0; 

 // a loop  ImageSetPixel($image, $y, $x, $color);

for($x = 0; $x < $size; $x++){  
 for($y = 0; $y < $size; $y++){  

   switch($first_array[$a]){ 
      case "white" : $color = $white; 
      break;
      case "grayOne" :  $color = $grayOne;
      break;
      case "grayTwo" : $color = $grayTwo;
      break;
      case "grayThree" : $color = $grayThree;
      break;
      case "grayFour" : $color = $grayFour;
      break;
      case "grayFive" : $color = $grayFive;
      break;
      case "graySix" : $color = $graySix;
      break;
      case "graySeven" : $color = $graySeven;
      break;
      case "grayHeight" : $color = $grayHeight;
      break;
      case "grayNine" : $color = $grayNine;
      break;
      case "black" : $color = $black ;
      break;        
    }    
    $a++;     
    ImageSetPixel($image, $y, $x, $color);        
  }  
 }

//set black as transparent
ImageColorTransparent($image, $black);

//save the picture with a number 
ImagePng($image, "../image/pictureb".$_POST['numb'].".png");

imagedestroy($image);
?>
HaveNoDisplayName
  • 7,711
  • 106
  • 32
  • 44

1 Answers1

1

I found a solution for my problem here :

http://forums.devshed.com/php-development-5/create-image-2d-rgb-array-930255-2.html