PhpGD Library pie chart generation interface source code

Source: Internet
Author: User
The GD Library is an extension library for php image processing. the GD Library provides a series of APIs for image processing. you can use the GD library to process images or generate images, the GD Library on websites is usually used to generate thumbnails or add watermarks to images. for the latest PHP version, generating thumbnails or adding watermarks is basically not processed by a dedicated GD Library. php can directly process simple image applications, such as the processing diagram mentioned.

The GD Library is an extension library for php image processing. the GD Library provides a series of APIs for image processing. you can use the GD library to process images or generate images, the GD Library on websites is usually used to generate thumbnails or add watermarks to images. for the latest PHP version, generating thumbnails or adding watermarks is basically not processed by a dedicated GD Library. php can directly process simple image applications, such as processing image thumbnails and adding watermarks to images, image verification code, etc. Currently, the most widely used php gd diagram should be a pie chart or a statistical chart. Below is an interface source code generated by a php GD Library pie chart and runs it directly, A blue-dominated pie chart can be generated.

 

Program running preview:

PHP code
  1. // Picture width and height
  2. $ W = isset ($ _ GET ['w'])? Intval ($ _ GET ['w']): 400;
  3. $ H = isset ($ _ GET ['H'])? Intval ($ _ GET ['H']): 200;
  4. $ Title = $ _ GET ['t'];
  5. // Color Library
  6. $ Color_arr = array (
  7. Array (0, 0, 255 ),
  8. Array (255 ),
  9. Array (255 ),
  10. Array (255 ),
  11. Arrays (120,120,255 ),
  12. Arrays (150,150,255 ),
  13. Arrays (180,180,255 ),
  14. Arrays (210,210,255 ),
  15. Array (0,255, 0 ),
  16. Arrays (120,255,120 ),
  17. Arrays (150,255,150 ),
  18. Arrays (180,255,180 ),
  19. Arrays (210,255,210 ),
  20. Array (255, 0, 0 ),
  21. Array (255, 30, 30 ),
  22. Array (255, 60, 60 ),
  23. Array (255, 90, 90 ),
  24. Arrays (255,120,120 ),
  25. Arrays (255,150,150 ),
  26. Arrays (255,180,180 ),
  27. Arrays (255,210,210 ),
  28. Array (255,255, 0 ),
  29. Array (255, 0, 255 ),
  30. Array (255, 60, 255 ),
  31. Array (255, 90, 255 ),
  32. Arrays (255,120,255 ),
  33. Arrays (255,150,255 ),
  34. Arrays (255,180,255 ),
  35. Arrays (255,210,255 ),
  36. Arrays (0,255,255 ),
  37. Arrays (90,255,255 ),
  38. Arrays (120,255,255 ),
  39. Arrays (150,255,255 ),
  40. Arrays (180,255,255 ),
  41. Arrays (210,255,255 ),
  42. Array (50, 50, 50 ),
  43. Array (70, 70 ),
  44. Array (90, 90, 90 ),
  45. Arrays (110,110,110 ),
  46. Arrays (130,130,130 ),
  47. Arrays (150,150,150 ),
  48. Arrays (170,170,170 ),
  49. Arrays (190,190,190 ),
  50. );
  51. // Value
  52. $ Points = isset ($ _ GET ['Ps'])? Explode (',', $ _ GET ['Ps']): array (, 1 );
  53. // Value corresponding name
  54. $ Pts = isset ($ _ GET ['PTS '])? Explode (',', $ _ GET ['PTS ']): array ('1', '2', '3 ');
  55. $ Cnt = count ($ points );
  56. // Total value
  57. $ Total = 0;
  58. $ Tlen = 0;
  59. For ($ I = 0; $ I <$ cnt; $ I ++ ){
  60. $ Total + = $ points [$ I];
  61. $ Tlen = $ tlen> strlen ($ pts [$ I])? $ Tlen: strlen ($ pts [$ I]);
  62. }
  63. // Shortest side
  64. $ Min_wh = ($ W-2 * ($ tlen + 8) * ImageFontWidth (2)> ($ H-10 )? $ H: ($ W-2 * ($ tlen + 8) * ImageFontWidth (2 ));
  65. // Create an image
  66. $ Im = imagecreate ($ w, $ h );
  67. // The background color of the image is white.
  68. $ White = imagecolorallocate ($ im, 255,255,255 );
  69. $ Black = imagecolorallocate ($ im, 0, 0, 0 );
  70. ImageFill ($ im, 0, 0, $ white );
  71. // Center coordinate
  72. $ Cx = $ w/2;
  73. $ Cy = $ h/2 + 20;
  74. // Width and height of the circle's external rectangle
  75. $ Cw = $ min_wh-65;
  76. $ Ch = $ min_wh-65;
  77. $ Start = 0;
  78. $ End = 0;
  79. For ($ I = 0; $ I <$ cnt; $ I ++ ){
  80. // Slice color
  81. $ Color = $ I> = 43? Imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255): imagecolorallocate ($ im, $ color_arr [$ I] [0], $ color_arr [$ I] [1], $ color_arr [$ I] [2]);
  82. $ Color = imagecolorallocate ($ im, $ color_arr [$ I] [0], $ color_arr [$ I] [1], $ color_arr [$ I] [2]);
  83. // Start and end angles
  84. $ Start = $ end;
  85. $ End = $ start + $ points [$ I]/$ total * 360;
  86. Imagefilledarc ($ im, $ cx, $ cy, $ cw, $ ch, $ start, $ end, $ color, IMG_ARC_PIE );
  87. $ Notel1x = $ cx + cos (deg 2rad ($ start/2 + $ end/2) * $ cw/2;
  88. $ Notel1y = $ cy + sin (deg 2rad ($ start/2 + $ end/2) * $ cw/2;
  89. $ Notel2x = $ cx + cos (deg 2rad ($ start/2 + $ end/2) * ($ cw/2 + 10 );
  90. $ Notel2y = $ cy + sin (deg 2rad ($ start/2 + $ end/2) * ($ cw/2 + 10 );
  91. Imageline ($ im, $ notel1x, $ notel1y, $ notel2x, $ notel2y, $ black );
  92. $ Str = strlen ($ pts [$ I])? $ Pts [$ I]. "(". (substr ($ points [$ I]/$ total * 100,0, 5 )). "%)": ($ I + 1 ). "(". (substr ($ points [$ I]/$ total * 100,0, 5 )). "% )";
  93. If ($ notel2x> $ notel1x ){
  94. $ Notel3x = $ notel2x + 10;
  95. $ Notestrx = $ notel2x + 10;
  96. } Else {
  97. $ Notel3x = $ notel2x-10;
  98. $ Notestrx = $ notel2x-10-ImageFontWidth (2) * strlen ($ str );
  99. }
  100. $ Notel3y = $ notel2y;
  101. $ Notestry = $ notel2y-10;
  102. Imageline ($ im, $ notel2x, $ notel2y, $ notel3x, $ notel3y, $ black );
  103. Imagestring ($ im, 2, $ notestrx, $ notestry, $ str, $ black );
  104. }
  105. $ Wf5 = ImageFontWidth (5 );
  106. $ T_len = $ wf5 * strlen ($ title );
  107. // Echo $ cw. ''. $ ch; die ();
  108. Imagestring ($ im, 5, $ w/2-$ t_len/2, 5, $ title, $ black );
  109. // Flush image
  110. Header ('content-type: image/gif ');
  111. Imagegif ($ im );
  112. Imagedestroy ($ im );
  113. ?>


 

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.