How to use php to draw a positive cosine curve on an image

Source: Internet
Author: User
This article is a detailed analysis of the method of using php to draw the positive cosine curve on the image. if you need a friend, refer to the previous method of writing a dynamic trigonometric curve using actionscript, in fact, php outputs the trigonometric curve quite easily.

The code is as follows:


Define ("MAX_WIDTH_PIXEL", 600 );
Define ("MAX_HEIGHT_PIXEL", 240 );

// Send the header information
Header ("Content-type: image/gif ");

// Create an image
$ Img = imageCreate (MAX_WIDTH_PIXEL, MAX_HEIGHT_PIXEL );

// Set the color
$ Bgcolor = imageColorAllocate ($ img, 0xff, 0xe9, 0xe9 );
$ Red = imageColorAllocate ($ img, 255, 0, 0 );
$ Blue = imageColorAllocate ($ img, 0, 0,255 );
$ Brown = imageColorAllocate ($ img, 100, 0, 0 );
$ Black = imageColorAllocate ($ img, 0, 0, 0 );

$ Width = MAX_WIDTH_PIXEL/2; // width
$ Height = MAX_HEIGHT_PIXEL/2; // height

// Create an axis
ImageLine ($ img, $ width, 0, $ width, MAX_HEIGHT_PIXEL, $ black); // Y axis
ImageLine ($ img, 0, $ height, MAX_WIDTH_PIXEL, $ height, $ black); // x axis

// Use loops to depict function images
For ($ I = 0; $ I <= MAX_WIDTH_PIXEL; $ I ++)
{
$ Y1 = 100 * sin ($ I/100 * M_PI );
ImageSetPixel ($ img, $ I, $ height + $ y1, $ blue );

$ Y2 = 100 * sin ($ I/300 * M_PI );
ImageSetPixel ($ img, $ I, $ height + $ y2, $ red );

$ Y3 = 100 * sin ($ I/300 * M_PI );
ImageSetPixel ($ img, $ I, $ height-$ y3, $ brown );
}

// Display the image
ImageGif ($ img );

// Release resources
ImageDestroy ($ img );
/* = = */
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.