Core PHP

Drawing Simple Rectangles into a Jpeg with GD

The GD library allows programmers to draw simple geometric primitives.

SimpleRectanglesJpeg.php

<?php
  // Create a 600 by 400 pixel image
  $qImage = imagecreatetruecolor(600, 400);

  // Allocate colors
  $qWhite = imagecolorallocate($qImage, 255, 255, 255);
  $qGreen = imagecolorallocate($qImage, 0, 255, 0);
  $qOrange = imagecolorallocate($qImage, 255, 128, 0);
  $qDarkGreen = imagecolorallocate($qImage, 75, 150, 0);

  // Draw a white rectangle with a green border
  imagefilledrectangle($qImage, 50, 50, 550, 350, $qWhite);
  imagerectangle($qImage, 50, 50, 550, 350, $qGreen);

  // Draw two rectangles inside that one
  imagerectangle($qImage, 100, 120, 300, 300, $qOrange);
  imagerectangle($qImage, 75, 180, 450, 325, $qDarkGreen);

  // Output the image and free the associated memory
  header('Content-Type: image/jpeg');

  imagejpeg($qImage);
  imagedestroy($qImage);
?>
 

Output

 
 

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