INTRODUCTION

PHP Web Home
Server Setup
nav.php
code.php
Download Tutorial

LESSONS

01 Hello World!
02 Text I/O
03 Arithmetic
04 Date and Time
05 Image Creation
06 Pie Chart
07 Array and Records
08 Delete a Record
09 Shopping
10 Shop Checkout
11 MM Bingo

MORE PHP

PHP Console Apps
PHP and MySQL

05 - Creating Graphics Using PHP

Homework: Create your own simple graphic.

This is the HTML code to display an image.


<!-- Here is the page source code for 05image.php -->

<?php
  include "code.php";    // Contains the function used to display the source code of web pages.
?>

<html>
<head>
<title>Creating Graphics Using PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table border="1" cellpadding="4" cellspacing="0">
<tr valign="top">
<td><?php include "nav.php"; ?></td>
<td>
      <h1>05 - Creating Graphics Using PHP</h1>
      
      <p><img src="05imageCreate.php"></p>
      
      <p><strong><font color="#FF0000">Homework:</font></strong> Create
      your own simple graphic.</p>

      <p><strong>This is the HTML code to display an image.</strong></p>
      <?php dump_page("05image.php"); ?>
	  <p><strong>This is the PHP code used to create the image.</strong></p>
      <?php dump_page("05imageCreate.php"); ?>
</td>
</tr>
</table>

</body>
</html>

This is the PHP code used to create the image.


<!-- Here is the page source code for 05imageCreate.php -->

<?php
  // TELL OUTPUT STREAM TO EXPECT PNG IMAGE DATA
  header( "Content-type: image/png");
  
  // CREATE THE IMAGE
  $image = imagecreate(400, 200);                           // Create a new 400x200 image
  
  // SET UP SOME COLOURS
  $yellow = imagecolorallocate($image, 255, 255,   0);      // create color R=255, G=255, B=0
  $cyan   = imagecolorallocate($image,   0, 255, 255);      // create color R=0, G=255, B=255
  $red    = imagecolorallocate($image, 255,   0,   0);      // create color red
  $blue   = imagecolorallocate($image,   0,   0, 255);      // create color blue
  
  // DRAW SOME SHAPES
  imagefilledrectangle($image,  0,  0, 400, 200, $cyan);    // create cyan background
  imagefilledrectangle($image, 10, 80, 390, 190, $yellow);  // create inner yellow rectangle 
  imagerectangle      ($image,  5,  5, 395, 195, $red);     // create red frame
  
  // PLOT SOME CONTENTS
  imageline($image, 10, 135, 390, 135, $red);               // a line
  
  // PLOT A SINE WAVE
  for ($i = 0; $i < 3800; $i++)
  {
    $y = 135 - 50 * sin($i / 100);                          // Calculate y value
    imagesetpixel($image, 10 + $i / 10, $y, $red);          // Plot a red pixel
  }
  
  // PLOT A COSINE WAVE
  for ($i = 0; $i < 3800; $i++)
  {
    $y = 135 - 50 * cos($i / 100);                          // Calculate y value
    imagesetpixel($image, 10 + $i / 10, $y, $blue);         // Plot a blue pixel
  }
  
  // DISPLAY SOME TEXT
  for ($i = 1; $i <= 5; $i++)
  {
    // display 5 built-in fonts in red
    //          image  font  x  y         display-this-text                     colour
    imagestring($image, $i, 15, $i * 12, 'Sine wave (red)  Cosine wave (blue)', $red);
  } 

  imagepng($image);                                         // Stream the image output as it it came from a file 
			       
  imagedestroy($image);        // Cleanup memory  
?>