Web Info and IT Lessons Blog...

Friday, 17 April 2015

Drawing image patterns using imagesetpixel() in PHP

The previous lesson of PHP Lessons Series was about creating an image and in this lesson we will learn to draw different image patterns using imagesetpixel() function in PHP.

Drawing Image Patterns in PHP

imagesetpixel() function is used to set the colour of a pixel of an image at the specific co-ordinates. The basic syntax of imagesetpixel() is given below:


imagesetpixel (image, x co-ordinate, y co-ordinate, color(r,g,b));

As it is clear from the above syntax, we need to pass the image resource, x and y co-ordinates of the pixel and rgb value of colour in the imagesetpixel() function to set a pixel's colour.

Now let us draw our first image pattern using imagesetpixel() function:

PHP Image Pattern1


<?php
header('Content-type: image/jpeg');
$imageWidth=300;
$imageHeight=300;
$image=imagecreate($imageWidth,$imageHeight);
$blackColour = imagecolorallocate($image, 0, 0, 0); 
$whiteColour = imagecolorallocate($image, 255, 255, 255);
$count = 0;
for($i=0;$i<300;$i++){
 for($j=0;$j<300;$j++){
  $count++;
  if($count%6 == 1){
   $assignColour = $blackColour;
  }
  else if($count%6 == 0){
   $assignColour = $whiteColour;
  }
  imagesetpixel($image,$i,$j,$assignColour);
 }
}
imagejpeg($image);
?>

In the above php code we have created an image using our previous knowledge of image creation. We have used only two colours in our pattern i.e Black and White. After the image is created and colours are allocated, we have used a nested for loop to traverse every pixel of the 300X300 image and set it's color based on the modulus operation i.e ($count%6). The modulus operation is used to assign every sixth pixel a white colour and the other five pixels a black colour.

Now let us draw another image pattern using imagesetpixel():

PHP Image Pattern2


<?php
header('Content-type: image/jpeg');
$imageWidth=300;
$imageHeight=300;
$image=imagecreate($imageWidth,$imageHeight);
$blackColour = imagecolorallocate($image, 0, 0, 0); 
$whiteColour = imagecolorallocate($image, 255, 255, 255);
$count = 0;
$blocksize = 30;
$curblock = 'Black';
for($i=0;$i<300;$i++){
 for($j=0;$j<300;$j++){
  $count++;
  if($count < $blocksize){
   if($curblock == 'Black')
    $assignColour = $blackColour;
   else
    $assignColour = $whiteColour;
  }
  else if($count == $blocksize){
   if($curblock == 'Black'){
    $assignColour = $blackColour;
    $curblock = 'White';
   }else{
    $assignColour = $whiteColour;
    $curblock = 'Black';
   }
   $count = 0;
  }
  imagesetpixel($image,$i,$j,$assignColour);
 }
}
imagejpeg($image);
?>

In the above php code we have used a variable ($blocksize = 30) to keep the colour same for a block of pixels and change the colour of the next block of (30) pixels. We change the value of variable ($curblock) after every 30 pixels which is used to determine the colour to be assigned to the pixels.

In the last pattern we will learn to draw a circular image pattern using imagesetpixel() function:

PHP Image Pattern3


<?php
header('Content-type: image/jpeg');
$imageWidth=300;
$imageHeight=300;
$image=imagecreate($imageWidth,$imageHeight);
$blackColour = imagecolorallocate($image, 0, 0, 0); 
$whiteColour = imagecolorallocate($image, 255, 255, 255);
$count = 0;
$radius = 10;
$centerX = 150;
$centerY = 150;
for($i=0;$i<300;$i++){
 for($j=0;$j<300;$j++){
  $assignColour = $blackColour;
  $d = sqrt(($i-150)*($i-150)+($j-150)*($j-150));
  $d = floor($d);
  if($d%$radius == 0){
   imagesetpixel($image,$i,$j,$blackColour);
  }else{
   imagesetpixel($image,$i,$j,$whiteColour);
  }
 }
}
imagejpeg($image);
?>

In the above pattern we calculate the distance of each pixel from the center pixel of the circle whose co-ordinates are (150,150) using this distance formula:

$d = sqrt(($i-150)*($i-150)+($j-150)*($j-150));

Then we have used php floor function to convert the float value of distance to an integer value. After getting the integer value of distance, we take it's mod with the radius value we have chosen to decide whether to draw a black pixel or a white pixel. If the distance is fully divisible by the radius (i.e modulus is zero), we draw a black pixel else we keep it white.

For more lessons about PHP subscribe on InformationBitz.com

Related Posts
How to create an image in PHP?
Convert a string to an image in PHP
Convert an image to a string in PHP
Write text to image in php using imagettftext()

No comments:

Post a Comment