Can we draw images using PHP?

Using gd we will first learn how to draw line. We will learn some basic functions used in creating and managing images.

$im = @ImageCreate (100, 200)
The above line creates and image identifier of width 100 and height 200. The top left coordinate is set as 0, 0

Drawing lines and patterns using different thickness and colours using imageline() in PHP GD

$background_color = ImageColorAllocate ($im, 234, 234, 234);
Background color is set here for the image with the identifier $im. Here we have set it to a light gray background. The color of the background can be changed by changing the values of red, green, blue ( now all are set to 234 to create light gray background)

Same way let us set a text color for the image

$text_color = ImageColorAllocate ($im, 233, 14, 91);
imageline ($im,$x1,$y1,$x2,$y2,$text_color);
The above line will show the line from where to where it is to be drawn. Staring from $x1,$x2 to $x2, $y2. Note that top left is at the coordinate 0,0. The lines below will give how the line will appear with different values of the coordinates used in imageline function.

Now with this image, let us draw

ImagePng ($im);
With the above knowledge, you can see that the command imageline ($im,$x1,$y1,$x2,$y2,$text_color) draws lines between two points defined with two coordinates. To learn the different images we get ( or different lines ) with the change in value of coordinates ( x and y ) we will list a set of values and corresponding image link. Click the link right of the coordinates to check what image will be drawn with the value given at left. All images will be opened in a new window.

imageline ($im,0,0,50,100,$text_color)Displayimageline ($im,0,0,100,100,$text_color)Displayimageline ($im,100,0,0,200,$text_color)Displayimageline ($im,0,0,100,200,$text_color)Display
With above example you can see how the coordinates affect the line inside the image. Now let us dray two lines in the same image one after the other.

imageline ($im,0,0,100,200,$text_color);
imageline ($im,100,0,0,200,$text_color);Display

Above examples must have given you some idea on how to draw lines using gd support in PHP. You can use the forum if you have any doubt. Now here you can see the code we used to draw the lines in small window. Note that this code has two imageline functions to draw two lines. Change the data and see how the line changes.

Here is a function for making antialiased lines:

function imagesmoothline ( $image , $x1 , $y1 , $x2 , $y2 , $color )
{
  $colors = imagecolorsforindex ( $image , $color );
  if ( $x1 == $x2 )
  {
   imageline ( $image , $x1 , $y1 , $x2 , $y2 , $color ); // Vertical line
  }
  else
  {
   $m = ( $y2 - $y1 ) / ( $x2 - $x1 );
   $b = $y1 - $m * $x1;
   if ( abs ( $m ) <= 1 )
   {
    $x = min ( $x1 , $x2 );
    $endx = max ( $x1 , $x2 );
    while ( $x <= $endx )
    {
     $y = $m * $x + $b;
     $y == floor ( $y ) ? $ya = 1 : $ya = $y - floor ( $y );
     $yb = ceil ( $y ) - $y;
     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , floor ( $y ) ) );
     $tempcolors['red'] = $tempcolors['red'] * $ya + $colors['red'] * $yb;
     $tempcolors['green'] = $tempcolors['green'] * $ya + $colors['green'] * $yb;
     $tempcolors['blue'] = $tempcolors['blue'] * $ya + $colors['blue'] * $yb;
     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
     imagesetpixel ( $image , $x , floor ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , $x , ceil ( $y ) ) );
     $tempcolors['red'] = $tempcolors['red'] * $yb + $colors['red'] * $ya;
      $tempcolors['green'] = $tempcolors['green'] * $yb + $colors['green'] * $ya;
     $tempcolors['blue'] = $tempcolors['blue'] * $yb + $colors['blue'] * $ya;
     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
     imagesetpixel ( $image , $x , ceil ( $y ) , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
     $x ++;
    }
   }
   else
   {
    $y = min ( $y1 , $y2 );
    $endy = max ( $y1 , $y2 );
    while ( $y <= $endy )
    {
     $x = ( $y - $b ) / $m;
     $x == floor ( $x ) ? $xa = 1 : $xa = $x - floor ( $x );
     $xb = ceil ( $x ) - $x;
     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , floor ( $x ) , $y ) );
     $tempcolors['red'] = $tempcolors['red'] * $xa + $colors['red'] * $xb;
     $tempcolors['green'] = $tempcolors['green'] * $xa + $colors['green'] * $xb;
     $tempcolors['blue'] = $tempcolors['blue'] * $xa + $colors['blue'] * $xb;
     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
     imagesetpixel ( $image , floor ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
     $tempcolors = imagecolorsforindex ( $image , imagecolorat ( $image , ceil ( $x ) , $y ) );
     $tempcolors['red'] = $tempcolors['red'] * $xb + $colors['red'] * $xa;
     $tempcolors['green'] = $tempcolors['green'] * $xb + $colors['green'] * $xa;
     $tempcolors['blue'] = $tempcolors['blue'] * $xb + $colors['blue'] * $xa;
     if ( imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) == -1 ) imagecolorallocate ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] );
     imagesetpixel ( $image , ceil ( $x ) , $y , imagecolorexact ( $image , $tempcolors['red'] , $tempcolors['green'] , $tempcolors['blue'] ) );
     $y ++;
    }
   }
  }
}

EDITOR: My previous code contained bugs. Please use this one instead.

How to create an image in PHP?

The imagecreate() function is an inbuilt function in PHP which is used to create a new image. This function returns the blank image of given size. In general imagecreatetruecolor() function is used instead of imagecreate() function because imagecreatetruecolor() function creates high quality images.

Can we draw images using PHP yes since PHP 7?

First, you can draw (make images) with PHP. Second, only a sadist would try to replace a Photoshop-like tool with any programming interface to a drawing tool. Third, the GD image library for drawing in PHP has many drawbacks. Those drawbacks are pretty significant, though some of them are certainly a matter of opinion.

How to display image in PHP code?

therefore, pick the specific image name and copy image name. for example, you can see have to mention an example of image source src='image-name. png' this is a direct image path. So, if the image in directory or folder and subfolder then put this code src='folderName/SubfolderName/image-name.

How to draw polygon in PHP?

The imagepolygon() function is an inbuilt function in PHP which is used to draw a polygon. This function returns TRUE on success and returns FALSE otherwise.