Core PHP

Saving an XY Graph Image

This PHP program demonstrates how to draw a graph of the parabola in the xy plane and save it as a png image file with the Graphics Draw, GD, library.

DrawAndSaveImageOfGraphXY.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
    <h2>The Graph of y = x<sup>2</sup></h2>
  </head>
  <body>
<?php
  $iGraphWidth = 400;
  $iGraphHeight = 400;

  $iCharWidth = ImageFontWidth(4);
  $iCharHeight = ImageFontHeight(4);

  $iGraphLeft = 100;
  $iGraphTop = $iCharHeight;
  $iGraphRight = $iGraphLeft + $iGraphWidth;
  $iGraphBottom = $iGraphTop + $iGraphHeight;
  $iImageWidth = $iGraphLeft + $iGraphWidth + 40;
  $iImageHeight = $iGraphTop + $iGraphHeight + $iCharHeight * 1.5;

  $iGraphIntervals = 100;
  for($i = 0; $i <= $iGraphIntervals; $i++ ) {
    $iaGraphX[$i] = $i;
    $iaGraphY[$i] = $i * $i;
  }

  $iMinX = 0;
  $iMinY = 0;
  $iMaxX = 100;
  $iMaxY = $iMaxX*$iMaxX;

  $qImage = ImageCreate($iImageWidth, $iImageHeight);
  $qBkgdColor = ImageColorAllocate($qImage, 245, 245, 245);
  $qLabelColor = ImageColorAllocate($qImage, 200, 25, 100);

  $qLightGray = ImageColorAllocate($qImage, 204, 204, 204);
  $qGraphColor = ImageColorAllocate($qImage, 100, 245, 100);
  $qDarkGray = ImageColorAllocate($qImage, 128, 128, 128);

  ImageRectangle($qImage, 0, 0, $iImageWidth - 1, $iImageHeight - 1, $qDarkGray);
  ImageRectangle($qImage, $iGraphLeft, $iGraphTop, $iGraphRight, $iGraphBottom, $qLightGray);

  // Draw the data in the graph
  $iPrevX = $iGraphLeft + ($iGraphWidth)*($iaGraphX[0]-$iMinX)/($iMaxX-$iMinX);
  $iPrevY = $iGraphBottom - ($iGraphHeight)*($iaGraphY[0]-$iMinY)/($iMaxY-$iMinY);
  for($i = 1; $i <= $iGraphIntervals; $i++ ) {
	$iCurrX = $iGraphLeft + ($iGraphWidth)*($iaGraphX[$i]-$iMinX)/($iMaxX-$iMinX);
	$iCurrY = $iGraphBottom - ($iGraphHeight)*($iaGraphY[$i]-$iMinY)/($iMaxY-$iMinY);
	ImageLine($qImage, $iPrevX, $iPrevY, $iCurrX, $iCurrY, $qGraphColor);
	$iPrevX = $iCurrX;
	$iPrevY = $iCurrY;
  }

  // Print the graph bounds
  $sMaxY = sprintf("%2.2f", $iMaxY);
  ImageString($qImage, 4, $iGraphLeft - strlen($sMaxY) * $iCharWidth - 2, $iGraphTop - $iCharWidth, $sMaxY, $qDarkGray);
  $sMinY = sprintf("%2.2f", $iMinY);
  ImageString($qImage, 4, $iGraphLeft - strlen($sMinY) * $iCharWidth - 2, $iGraphBottom - $iCharHeight, $sMinY, $qDarkGray);
  $sMinX = sprintf("%2.2f", $iMinX);
  ImageString($qImage, 4, $iGraphLeft, $iGraphBottom + 2, $sMinX, $qDarkGray);
  $sMaxX = sprintf("%2.2f", $iMaxX);
  ImageString($qImage, 4, $iGraphRight - (strlen($sMaxX) * $iCharWidth) / 2, $iGraphBottom + 2, $sMaxX, $qDarkGray);

  // Print the axis labels
  $sLabelX = 'x-axis';
  $sLabelY = 'y-axis';
  ImageString($qImage, 4, $iGraphLeft + ($iGraphWidth)/2 - (strlen($sLabelX)*$iCharWidth)/2, $iGraphBottom, $sLabelX, $qLabelColor);
  ImageString($qImage, 4, 3*$iCharWidth, ($iGraphHeight)/2, $sLabelY, $qLabelColor);

  $sFilename = sprintf("Parabola.png");
  // Save the png file
  ImagePNG($qImage, $sFilename);
  ImageDestroy($qImage);
?>
    <img src="<?php echo $sFilename ?>" />
  </body>
</html>
 

Output

 
 

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