php基于GD库,绘制PNG图像Demo

| |
[不指定 2016/03/10 18:21 | by 刘新修 ]
PHP代码
  1. <?php  
  2.     //创建画布,返回一个资源类型的变量$image,并在内存中开辟一个临时区域  
  3.     $image = imagecreatetruecolor(400,400);                //创建画布大小为400x400  
  4.   
  5.     //设置图像中所需的颜色,相当于在画画时准备的染料盒  
  6.     $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);          //为图像分配颜色为白色  
  7.     $gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);           //为图像分配颜色为灰色  
  8.     $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);       //为图像分配颜色为暗灰色  
  9.     $navy = imagecolorallocate($image, 0x00, 0x00, 0x80);           //为图像分配颜色为深蓝色  
  10.     $darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);       //为图像分配颜色为暗深蓝色  
  11.     $red = imagecolorallocate($image, 0xFF, 0x00, 0x00);           //为图像分配颜色为红色  
  12.     $darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);       //为图像分配颜色为暗红色  
  13.   
  14.     imagefill($image, 0, 0, $white);            //为画布背景填充背景颜色  
  15.     //动态制作3D效果  
  16.     for ($i=220;$i>200;$i--){                //循环10次画出立体效果  
  17.         imagefilledarc($image, 200, $i, 400, 200, -160, 40, $darknavy, IMG_ARC_PIE);  
  18.         imagefilledarc($image, 200, $i, 400, 200, 40, 75, $darkgray, IMG_ARC_PIE);  
  19.         imagefilledarc($image, 200, $i, 400, 200, 75, 200, $darkred, IMG_ARC_PIE);  
  20.     }  
  21.   
  22.     imagefilledarc($image, 200, 200, 400, 200, -160, 40, $navy, IMG_ARC_PIE);      //画一椭圆弧且填充  
  23.     imagefilledarc($image, 200, 200, 400, 200, 40 , 75, $gray, IMG_ARC_PIE);      //画一椭圆弧且填充  
  24.     imagefilledarc($image, 200, 200, 400, 200, 75, 200, $red, IMG_ARC_PIE);      //画一椭圆弧且填充  
  25.   
  26.     imagestring($image, 20, 100, 230, '34.7%', $white);                //水平地画一行字符串,依次是F/X/Y/--数值越大向右、向下  
  27.     imagestring($image, 20, 200, 150, '55.5%', $white);                //水平地画一行字符串,依次是F/X/Y/--数值越大向右、向下  
  28.   
  29.     //向浏览器中输出一个GIF格式的图片  
  30.     header('Content-type:image/png');       //使用头函数告诉浏览器以图像方式处理以下输出  
  31.     imagepng($image);                       //向浏览器输出  
  32.     imagedestroy($image);                   //销毁图像释放资源  
  33.   
  34.     //header("Content-type: text/html; charset=utf-8");  
  35.     //$str="<div>'$image'</div>";  
  36.     //echo $str;  
  37. ?>  

gd-png.php

PHP代码
  1. <?php  
  2.     //创建画布,返回一个资源类型的变量$image,并在内存中开辟一个临时区域  
  3.     $image = imagecreatetruecolor(375,375);                //创建画布大小为375x375 iPhone6 375X627  
  4.   
  5.     //设置图像中所需的颜色,相当于在画画时准备的染料盒  
  6.     $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);          //为图像分配颜色为白色  
  7.     $gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);           //为图像分配颜色为灰色  
  8.     $darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);       //为图像分配颜色为暗灰色  
  9.     $navy = imagecolorallocate($image, 0x00, 0x00, 0x80);           //为图像分配颜色为深蓝色  
  10.     $darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);       //为图像分配颜色为暗深蓝色  
  11.     $red = imagecolorallocate($image, 0xFF, 0x00, 0x00);           //为图像分配颜色为红色  
  12.     $darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);       //为图像分配颜色为暗红色  
  13.   
  14.     imagefill($image, 0, 0, $white);            //为画布背景填充背景颜色  
  15.     //动态制作3D效果  
  16.     for ($i=200;$i>180;$i--){                //循环10次画出立体效果  
  17.         imagefilledarc($image, 187, $i, 360, 180, -160, 40, $darknavy, IMG_ARC_PIE);   //  
  18.         imagefilledarc($image, 187, $i, 360, 180, 40, 75, $darkgray, IMG_ARC_PIE);  
  19.         imagefilledarc($image, 187, $i, 360, 180, 75, 200, $darkred, IMG_ARC_PIE);  
  20.     }  
  21.   
  22.     imagefilledarc($image, 187, 180, 360, 180, -160, 40, $navy, IMG_ARC_PIE);      //画一椭圆弧且填充,第一个187-180 是错开7像素  
  23.     imagefilledarc($image, 187, 180, 360, 180, 40 , 75, $gray, IMG_ARC_PIE);      //画一椭圆弧且填充  
  24.     imagefilledarc($image, 187, 180, 360, 180, 75, 200, $red, IMG_ARC_PIE);      //画一椭圆弧且填充  
  25.   
  26.     imagestring($image, 20, 180, 130, '55.5%'$white);                //水平地画一行字符串,依次是F/X/Y/--数值越大向右、向下  
  27.     imagestring($image, 20, 100, 200, '34.7%'$white);                //水平地画一行字符串,依次是F/X/Y/--数值越大向右、向下  
  28.   
  29.   
  30.     //向浏览器中输出一个GIF格式的图片  
  31.     //header("Content-type:text/html;charset=utf-8"); // 设置页面的编码风格  
  32.     header('Content-type:image/png');       //使用头函数告诉浏览器以图像方式处理以下输出--通知浏览器输出的是图像  
  33.     imagepng($image);                       //向浏览器输出  
  34.     imagedestroy($image);                   //销毁图像释放资源  
  35.   
  36.     //header("Content-type: text/html; charset=utf-8");  
  37.     //$str="<div>'$image'</div>";  
  38.     //echo $str;  
  39. ?>  
PHP/Java | 评论(0) | 引用(0) | 阅读(3585)