GD PHP

Drawing a Line

This PHP program demonstrates how to draw a line with the Graphics Draw, GD, library.

DrawLine.php

<?php
  $qImage  = imagecreatetruecolor(512, 512);

  $qBkgdColor = imagecolorallocate($qImage, 200, 200, 170);
  // Color the background by setting each pixel color individually
  for ($i = 0; $i < 512; ++$i) {
    for ($j = 0; $j < 512; ++$j) {
      imagesetpixel($qImage, $i, $j, $qBkgdColor);
    }
  }

  $qLineColor = imagecolorallocate($qImage, 255, 255, 255);
  // Format: (Image, X1, Y1, X2, Y2, Color)
  imageline($qImage, 50, 300, 450, 100, $qLineColor);

  header('Content-type: image/png');
  imagepng($qImage);
  imagedestroy($qImage);
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.