Advertisement
fastman92

PHP Draw sinewave with ImageGD

Nov 26th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. <?php
  2. // Draws sinewave in PHP
  3. // Author: fastman92
  4. // Site: fastman92-site.tk
  5.  
  6. // Input data  
  7. $sizeX = 600;
  8. $sizeY = 300;
  9. $period = 200;    // px
  10.  
  11. // Code
  12. $im = imagecreatetruecolor($sizeX, $sizeY);
  13. $white = imagecolorallocate($im, 255, 255, 255);
  14.  
  15. imagefilledrectangle($im, 0, 0, $sizeX, $sizeY, $white);
  16.  
  17. $color = imagecolorallocate($im, 0, 0, 0);
  18.  
  19. $previousY = 0.0;
  20.  
  21.     for($x = 0; $x < $sizeX; $x++)
  22.     {    
  23.         $y = ($sizeY / 2.0) + $sizeY/2 * sin(2*M_PI*$x/$period);
  24.         imageline($im, $x-1, $previousY, $x, $y, $color);
  25.  
  26.         $previousY = $y;
  27.     }
  28.  
  29. header('Content-Type: image/png');
  30. imagepng($im);
  31.  
  32. imagecolordeallocate($im, $color);
  33. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement