GD PHP

Drawing with Anti-Aliasing

This PHP program demonstrates how to draw with anti-aliasing with the Graphics Draw, GD, library. Anti-aliasing is not available for all drawing. Currently, it only appears to work for straight lines, such as those in lines and polygons, but not for the dashed lines.

DrawAntiAliased.php

<?php
  // Setup the main anti-aliased image and a normal image
  $qMain = ImageCreateTrueColor(400, 100);
  $qNorm = ImageCreateTrueColor(200, 100);

  // Turn anti-aliasing on for the main image
  ImageAntialias($qMain, true);

  // Allocate colors
  $qRedMain = ImageColorAllocate($qMain, 255, 0, 0);
  $qRedNorm = ImageColorAllocate($qNorm, 255, 0, 0);

  // Color the background white
  $qWhiteMain = ImageColorAllocate($qMain, 255, 255, 255);
  $qWhiteNorm = ImageColorAllocate($qNorm, 255, 255, 255);
  ImageFill($qMain, 0, 0, $qWhiteMain);
  ImageFill($qNorm, 0, 0, $qWhiteNorm);

  // Draw two set of figures, one with anti-aliasing enabled
  // Format: (Image, X1, Y1, X2, Y2, Color)
  ImageLine($qMain, 0, 39, 200, 61, $qRedMain);
  ImageLine($qNorm, 0, 39, 200, 61, $qRedNorm);
  // Format: (Image, X1, Y1, X2, Y2, Color)
  ImageDashedLine($qMain, 0, 0, 159, 100, $qRedMain);
  ImageDashedLine($qNorm, 0, 0, 159, 100, $qRedNorm);
  // Format: (Image, CenterX, CenterY, RadiusX, RadiusY, Color)
  ImageEllipse($qMain, 100, 50, 190, 90, $qRedMain);
  ImageEllipse($qNorm, 100, 50, 190, 90, $qRedNorm);
  // Format: (Image, Font, x, y, Text, Color)
  ImageString($qMain, 4, 100, 25, "Hello", $qRedMain);
  ImageString($qNorm, 4, 100, 25, "Hello", $qRedNorm);
  $qPolygon = array(0, 95, 61, 0, 140, 87);
  // Format: (Image, Vertices, VertexCount, Color)
  ImagePolygon($qMain, $qPolygon, 3, $qRedMain);
  ImagePolygon($qNorm, $qPolygon, 3, $qRedNorm);
  // Format: (Image, CenterX, CenterY, RadiusX, RadiusY, AngleStart, AngleEnd, Color)
  ImageArc($qMain, 100, 60, 170, 90, 180, 360, $qRedMain);
  ImageArc($qNorm, 100, 60, 170, 90, 180, 360, $qRedNorm);

  // Merge the images with the antialiased drawing on the left
  ImageCopyMerge($qMain, $qNorm, 200, 0, 0, 0, 200, 100, 100);

  // Output image
  Header('Content-type: image/png');

  ImagePNG($qMain);
  ImageDestroy($qMain);
  ImageDestroy($qNorm);
?>
 

Output

 
 

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