首页 > Web开发 > 详细

php绘图(一)

时间:2015-05-22 23:56:50      阅读:1808      评论:0      收藏:0      [点我收藏+]
绘图要单独有个php文件,图画好后,HTML里面引入
image src



gd_info()查找gd库是否开启
    1. //print_r(gd_info());
    2. if(function_exists(gd_info)){
    3. echo ‘<pre>‘;
    4. print_r(gd_info());
    5. }else{
    6. echo ‘没有开启gd库‘;
    7. }
如果没有开启进入phpini文件把
extension=php_gd2.dll 前面的分号可以去掉
imagecreate()创建画布
imagecolorllocate()创建文字及画布颜色
imagestring()在画布上输出文字
imagepng()浏览器输出图像
imagedestroy() 销毁图像在内存中的占用
header("content-type:image/png")定义图片格式
imagecreatetruecolor(200,300);//创建真彩图像
imagefilledellipse()画圆
imagecreatefromjpeg(‘m.jpg‘);创建图片画布
imagecolorallocatealpha()  创建透明色
imagesetpixel($i,$j,50,$rdd);画点
imagesetthickness($i,5);//控制线条的粗细
imagettftext(字号,旋转角度,x,y,颜色,字体名,要加入的汉字)
imagefilledellipse($i,50,50,100,100,$c);实心圆
imageellipse($i,150,50,100,100,$c);空心圆
imagecopyresized 拷贝部分图像并调整大小
    1. //ox 16进制换算十进制oxff,ox33,ox99=#ff3399
    2. header("content-type:image/png");
    3. $m=imagecreate(200,90);//创建画布
    4. gd($m);//一般第一个颜色默认为画布的颜色
    5. $y=gd($m);//获取一个随机的颜色
    6. imagestring($m,5,35,40,hello china,$y);//在画布上输出文字,5代表文字大小,35x轴,40y轴,$y为文字的颜色
    7. imagepng($m);//浏览器输出图像
    8. imagepng($m,c:/‘.uniqid().‘.png‘);//将画布图像保存到硬盘上
    9. imagedestroy($m);
    10. //定义一个随机颜色函数
    11. function gd($i){
    12. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagecreatetruecolor(400,400)定义真彩图像,默认画布背景为黑色
imagcreate()建立画布的时候第一个颜色为背景
必须要用imagefill($i,0,0,$c);来添加背景颜色$c,整个黄布就为这种颜色,png的真彩质量比jpeg好
技术分享
imagecreatefromjpeg(‘m.jpg‘);创建图片画布
    1. header(content-type:image/jpg);
    2. $i=imagecreatefromjpeg(m.jpg);
    3. imagestring($i,5,20,120,hellojkkjkj,show($i));
    4. imagejpeg($i);
    5. imagedestroy($i);
    6. function show($m){
    7. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    8. }
技术分享

imagecolorallocatealpha()透明不是所有的图像都支持透明色,目前png画布透明做的最好
int imagecolorallocatealpha ( resource $image , int $red , int $green , int $blue , int $alpha )
imagefilledellipse()画圆
bool imagefilledellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color )
    1. //imagecolorallocatealpha(),png透明
    2. header(content-type:image/png);
    3. //imagcreate()建立画布的时候第一个颜色为背景
    4. $i=imagecreatetruecolor(400,400);//真彩图像,默认画布背景为黑色
    5. $rdd=imagecolorallocatealpha($i,255,0,0,100);//0-127
    6. $green=imagecolorallocatealpha($i,0,255,0,100);//0-127
    7. $b=imagecolorallocatealpha($i,0,255,255,100);//0-127
    8. imagefilledellipse($i,100,150,200,200,$rdd);
    9. imagefilledellipse($i,200,150,200,200,$green);
    10. imagefilledellipse($i,150,250,200,200,$b);//bool imagefilledellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color )画圆
    11. imagepng($i);
    12. imagedestroy($i);
技术分享
imagesetpixel($i,$j,50,$rdd);画点
bool imagesetpixel ( resource $image , int $x , int $y , int $color )
    1. header(content-type:image/png);
    2. $i=imagecreatetruecolor(400,400);//真彩图像,默认画布背景为黑色
    3. $rdd=imagecolorallocatealpha($i,255,0,0,0);
    4. $c=gc($i);
    5. imagesetpixel($i,50,60,$c);
    6. imagepng($i);
    7. imagedestroy($i);
    8. function gc($i){
    9. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    10. }
技术分享
用for循环让点成线
    1. header(content-type:image/png);
    2. $i=imagecreatetruecolor(400,400);//真彩图像,默认画布背景为黑色
    3. $rdd=imagecolorallocatealpha($i,255,0,0,0);
    4. $c=gc($i);
    5. for($j=0;$j<400;$j++){ //$j小于底板的宽度
    6. imagesetpixel($i,$j,200,$c);//画点
    7. }
    8. imagepng($i);
    9. imagedestroy($i);
    10. function gc($i){
    11. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    12. }
技术分享
for循环出现n多线条
    1. header(content-type:image/png);
    2. $i=imagecreatetruecolor(300,300);//创建真彩图像
    3. //$im=imagecolorallocatealpha($i,255,0,255,0);
    4. for($h=1;$h<=300;$h+=10){
    5. $g=show($i);
    6. for($j=1;$j<=300;$j++){
    7. imagesetpixel($i,$j,$h,$g);
    8. }
    9. }
    10. imagepng($i);
    11. imagedestroy($i);
    12. function show($m){
    13. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    14. }
技术分享
创建方格
    1. header(content-type:image/png);
    2. $i=imagecreatetruecolor(300,300);//创建真彩图像
    3. for($h=1;$h<=300;$h+=35){
    4. $g=show($i);
    5. for($j=1;$j<=300;$j++){
    6. imagesetpixel($i,$j,$h,$g);
    7. }
    8. for($j=1;$j<=300;$j++){
    9. imagesetpixel($i,$h,$j,$g);
    10. }
    11. }
    12. imagepng($i);
    13. imagedestroy($i);
    14. function show($m){
    15. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    16. }
技术分享
创建图片方格
    1. header(content-type:image/png);
    2. //$i=imagecreatetruecolor(300,300);//创建真彩图像
    3. $i=imagecreatefromjpeg(m.jpg);
    4. for($h=1;$h<=300;$h+=35){
    5. $g=show($i);
    6. for($j=1;$j<=300;$j++){
    7. imagesetpixel($i,$j,$h,$g);
    8. }
    9. for($j=1;$j<=300;$j++){
    10. imagesetpixel($i,$h,$j,$g);
    11. }
    12. }
    13. imagepng($i);
    14. imagedestroy($i);
    15. function show($m){
    16. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    17. }
技术分享
定义一个随机透明度的随机颜色
    1. function gc($m){
    2. return imagecolorallocatealpha($m,rand(0,255),rand(0,255),rand(0,255),rand(30,150));
    3. }
    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(300,300);//创建真彩图像
    4. //$i=imagecreatefromjpeg(‘m.jpg‘);
    5. for($h=1;$h<=300;$h+=35){
    6. $g=show($i);
    7. $a=gc($i);
    8. for($j=1;$j<=300;$j++){
    9. imagesetpixel($i,$j,$h,$a);
    10. }
    11. for($j=1;$j<=300;$j++){
    12. imagesetpixel($i,$h,$j,$a);
    13. }
    14. }
    15. imagepng($i);
    16. imagedestroy($i);
    17. function show($m){
    18. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    19. }
    20. function gc($m){
    21. return imagecolorallocatealpha($m,rand(0,255),rand(0,255),rand(0,255),rand(30,150));
    22. }
技术分享
imageline(); 画线imageline — 画一条线段
bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
画出米字格
    1. header(content-type:image/png);
    2. $i=imagecreatetruecolor(400,400);//创建真彩图像
    3. imageline($i,0,200,400,200,gc($i));
    4. imageline($i,200,0,200,400,gc($i));
    5. imageline($i,0,0,400,400,gc($i));
    6. imageline($i,400,0,0,400,gc($i));
    7. imagepng($i);
    8. imagedestroy($i);
    9. function show($m){
    10. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    11. }
    12. function gc($m){
    13. return imagecolorallocatealpha($m,rand(0,255),rand(0,255),rand(0,255),rand(30,150));
    14. }
技术分享
输出混乱的线条

    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. for($j=0;$j<=100;$j++){
    5. imageline($i,mt_rand(0,400),mt_rand(0,400),mt_rand(0,400),mt_rand(0,400),show($i));
    6. }
    7. imagepng($i);
    8. imagedestroy($i);
    9. function show($m){
    10. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    11. }
    12. function gc($m){
    13. return imagecolorallocatealpha($m,rand(0,255),rand(0,255),rand(0,255),rand(30,150));
    14. }

技术分享
    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. imagesetthickness($i,5);//控制线条的粗细
    5. for($j=0;$j<=100;$j++){
    6. imageline($i,mt_rand(0,400),mt_rand(0,400),mt_rand(0,400),mt_rand(0,400),show($i));
    7. }
    8. imagepng($i);
    9. imagedestroy($i);
    10. function show($m){
    11. return imagecolorallocate($m,rand(0,255),rand(0,255),rand(0,255));
    12. }
    13. function gc($m){
    14. return imagecolorallocatealpha($m,rand(0,255),rand(0,255),rand(0,255),rand(30,150));
    15. }

技术分享
imagettftext(字号,旋转角度,x,y,颜色,字体名,要加入的汉字)
array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
    1. <?php
    2. //imagecopyesampled image文件存盘使用gbk字符集时需要对中文文字进行转码 icnov(‘gbk‘,‘utf-8‘,‘中国人‘)
    3. header(content-type:image/png);
    4. $w=800;
    5. $h=280;
    6. $i=imagecreatetruecolor($w,$h);
    7. imagefill($i,0,0,imagecolorallocate($i,255,0,0));//00坐标在画布范围内
    8. $cc=imagecolorallocate($i,0,0,0);
    9. imagettftext($i,40,5,10,181,$cc,f.ttf,‘中国人-(China)‘);//第一个是字号,
    10. $c=imagecolorallocate($i,255,255,0);
    11. imagettftext($i,40,5,8,178,$c,f.ttf,‘中国人-(China)‘);
    12. imagepng($i);
    13. imagedestroy($i);
技术分享
imagefilledellipse($i,50,50,100,100,$c);实心圆($i,x,y,w,h,$color)
imageellipse($i,150,50,100,100,$c);
    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. $c=imagecolorallocate($i,255,0,0);
    5. imagefilledellipse($i,50,50,100,100,$c);
    6. imageellipse($i,150,50,100,100,$c);
    7. imagepng($i);
    8. imagedestroy($i);
技术分享
    1. header(content-type:image/png);
    2. $i=imagecreatetruecolor(400,400);//创建真彩图像
    3. $c=imagecolorallocate($i,255,0,0);
    4. for($j=0;$j<=50;$j++){
    5. imagefilledellipse($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    6. }
    7. imageellipse($i,150,50,5,5,$c);
    8. imagepng($i);
    9. imagedestroy($i);
技术分享
生成缩略图imagecopyresampled($new,$old,0,0,0,0, $w,$h,$ww,$hh);  缩略图的坐标都是0000,如果是截取0,0,
imagecopyresized(文件比较大)=imagecopyresampled()
推荐使用imagecopyresampled()
    1. <?php
    2. //imagecopyresampled()
    3. //原图生成缩略图
    4. $old=imagecreatefromjpeg(images/a.jpg);
    5. $ww=imagesx($old);//返回图片的宽
    6. $hh=imagesy($old);//返回图片的高
    7. //目标缩略图
    8. $w=100;
    9. $h=$w/$ww*$hh;
    10. $new=imagecreatetruecolor($w,$h);
    11. imagecopyresampled($new,$old,0,0,0,0, $w,$h,$ww,$hh);
    12. imagejpeg($new,images/s_a.jpg);
    13. imagedestroy($old);
    14. imagedestroy($new);
限制图片的宽度最高不超过600
    1. <?php
    2. //imagecopyresampled()
    3. //原图调整原图效果,限制图片的宽度最高为600
    4. $fn=images/02.jpg;
    5. $old=imagecreatefromjpeg($fn);
    6. $ww=imagesx($old);//返回图片的宽
    7. if($ww>600){
    8. $hh=imagesy($old);//返回图片的高
    9. //目标缩略图
    10. $w=600;
    11. $h=$w/$ww*$hh;
    12. $new=imagecreatetruecolor($w,$h);
    13. imagecopyresampled($new,$old,0,0,0,0, $w,$h,$ww,$hh);
    14. imagejpeg($new,$fn);
    15. imagedestroy($old);
    16. imagedestroy($new);
    17. }
截取图片中的某一部分
    1. <?php
    2. //imagecopyresampled()
    3. //原图调整原图效果
    4. header(content-type:image/jpg);
    5. $fn=images/office.jpg;
    6. $old=imagecreatefromjpeg($fn);
    7. $w=300;
    8. $h=300;
    9. $new=imagecreatetruecolor($w,$h);
    10. imagecopyresampled($new,$old,0,0,577,58, $w,$h,$w,$h);//效果好推荐使用
    11. imagejpeg($new);
    12. imagedestroy($old);
    13. imagedestroy($new);
header(‘content-type:image/jpg‘);//不用直接显示输出可以不要,直接显示必须要
    1. <?php
    2. header(content-type:image/jpg);//不用直接显示输出可以不要,直接显示必须要
    3. $w=200;
    4. $h=200;
    5. $i=imagecreatetruecolor($w,$h);
    6. imagejpeg($i);
    7. imagedestroy($i);
技术分享
getimagesize($i)取得图片的宽高比imagesx() 和 imagesy()好返回的是个数组
    1. <?php
    2. $i=images/a.jpg;
    3. $info=getimagesize($i);
    4. echo ‘<pre>‘;
    5. print_r($info);
    6. echo $info[0].‘*‘.$info[1];
技术分享
显示空心的各种圆形
    1. header(content-type:image/png);
    2. $i=imagecreatetruecolor(400,400);//创建真彩图像
    3. for($j=0;$j<=150;$j++){
    4. $c=gc($i);
    5. //imagefilledellipse($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    6. imageellipse($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    7. }
    8. imageellipse($i,150,50,5,5,$c);
    9. imagepng($i);
    10. imagedestroy($i);
    11. function gc($i){
    12. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    13. }
技术分享
    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. for($j=0;$j<=150;$j++){
    5. $c=gc($i);
    6. imagefilledellipse($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    7. //imageellipse($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    8. }
    9. imageellipse($i,150,50,5,5,$c);
    10. imagepng($i);
    11. imagedestroy($i);
    12. function gc($i){
    13. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    14. }
技术分享
    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. for($j=0;$j<=150;$j++){
    5. $c=gc($i);
    6. imagefilledellipse($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    7. imageellipse($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    8. }
    9. imageellipse($i,150,50,5,5,$c);
    10. imagepng($i);
    11. imagedestroy($i);
    12. function gc($i){
    13. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    14. }
技术分享
    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. for($j=0;$j<=150;$j++){
    5. $c=gc($i);
    6. imagefilledrectangle($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    7. imagerectangle($i,mt_rand(0,400),mt_rand(0,400),mt_rand(5,20),mt_rand(5,20),$c);
    8. }
    9. imageellipse($i,150,50,5,5,$c);
    10. imagepng($i);
    11. imagedestroy($i);
    12. function gc($i){
    13. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    14. }
技术分享
    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. for($j=0;$j<=150;$j++){
    5. $x=mt_rand(0,400);
    6. $y=mt_rand(0,400);
    7. imagefilledrectangle($i,$x,$y,$x+10,$y+10,gc($i));
    8. }
    9. imagepng($i);
    10. imagedestroy($i);
    11. function gc($i){
    12. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    13. }
技术分享
imagefilledrectangle($i,$x,$y,$x+$s,$y+$s,gc($i));画矩形
bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )

imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1y1,右下角坐标为 x2y2。0, 0 是图像的最左上角。

    1. <?php
    2. header(content-type:image/png);
    3. $i=imagecreatetruecolor(400,400);//创建真彩图像
    4. for($j=0;$j<=150;$j++){
    5. $x=mt_rand(0,400);
    6. $y=mt_rand(0,400);
    7. $s=mt_rand(5,20);
    8. imagefilledrectangle($i,$x,$y,$x+$s,$y+$s,gc($i));
    9. }
    10. imagepng($i);
    11. imagedestroy($i);
    12. function gc($i){
    13. return imagecolorallocate($i,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    14. }
技术分享
imagettftext($i,40,0,240,300,$c,‘f.ttf‘,‘教育机构‘);添加中文文字
    1. <?php
    2. header(content-type:image/jpg);
    3. $i=imagecreatefromjpeg(images/02.jpg);
    4. $w=imagesx($i);
    5. $h=imagesy($i);
    6. $c=imagecolorallocatealpha($i,255,255,0,100);
    7. imagettftext($i,40,0,240,300,$c,f.ttf,‘教育机构‘);
    8. imagejpeg($i);
    9. imagedestroy($i);
技术分享
批量修改本地电脑文件的图片水印
    1. <?php
    2. $path = f:/a/;
    3. $imgs = scandir($path);
    4. $logo = imagecreatefrompng(images/m.png);
    5. //$logo=imagerotate($logo,45,imagecolorallocatealpha($logo,0,0,0,127));
    6. $w = imagesx($logo);
    7. $h = imagesy($logo);
    8. foreach($imgs as $v){
    9. if($v==‘.‘ || $v==‘..‘){
    10. continue;
    11. }
    12. if(strtolower(substr($v,strrpos($v,‘.‘)+1)) == jpg){
    13. $i = imagecreatefromjpeg($path.$v);
    14. $ww = imagesx($i);
    15. $hh = imagesy($i);
    16. // 水银logo效果
    17. imagecopy($i,$logo,$ww-$w-20,$hh-$h,0,0,$w,$h);
    18. imagejpeg($i,$path.logo_.$v);
    19. imagedestroy($i);
    20. }
    21. }
    22. imagedestroy($logo);
技术分享
本机目录下添加水印
    1. <?php
    2. $path = ‘./images/‘;
    3. $imgs = scandir($path);
    4. $logo = imagecreatefrompng(images/m.png);
    5. //$logo=imagerotate($logo,45,imagecolorallocatealpha($logo,0,0,0,127)); //水印的角度旋转
    6. $w = imagesx($logo);
    7. $h = imagesy($logo);
    8. foreach($imgs as $v){
    9. if($v==‘.‘ || $v==‘..‘){
    10. continue;
    11. }
    12. if(strtolower(substr($v,strrpos($v,‘.‘)+1)) == jpg){
    13. $i = imagecreatefromjpeg($path.$v);
    14. $ww = imagesx($i);
    15. $hh = imagesy($i);
    16. // 水银logo效果
    17. imagecopy($i,$logo,$ww-$w-20,$hh-$h,0,0,$w,$h);
    18. imagejpeg($i,$path.logo_.$v);
    19. imagedestroy($i);
    20. }
    21. }
    22. imagedestroy($logo);
imagerotate($logo,45,imagecolorallocatealpha($logo,0,0,0,127));透明logo旋转
imagearc 画圆弧
bool imagearc ( resource $image , int $cx , int $cy , int $w , int $h , int $s , int $e , int $color )
imagearc() 以 cxcy(图像左上角为 0, 0)为中心在 image 所代表的图像中画一个椭圆弧。w 和 h 分别指定了椭圆的宽度和高度,起始和结束点以 s 和 e 参数以角度指定。0°位于三点钟位置,以顺时针方向绘画。
    1. <?php
    2. // 创建一个 200X200 的图像
    3. $img = imagecreatetruecolor(200, 200);
    4. // 分配颜色
    5. $white = imagecolorallocate($img, 255, 255, 255);
    6. $black = imagecolorallocate($img, 0, 0, 0);
    7. imagefill($img,0,0,$white);
    8. // 画一个黑色的圆
    9. imagearc($img, 100, 100, 150, 150, 0, 360, $black);
    10. // 将图像输出到浏览器
    11. header("Content-type: image/png");
    12. imagepng($img);
    13. // 释放内存
    14. imagedestroy($img);
技术分享






























php绘图(一)

原文:http://www.cnblogs.com/lsr111/p/4523476.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!