Graphing code is available in the tip section on this site and on various other php sites, just look for php and graphs.
If you want to save a dynamically created image, you only have to add the filename to the "imagepng" or "imagejpg" command:
ImagePNG($image,"my_image.png");
This is explained in the manual.