php 画图(主要采用的是php的GD库) copy直接用

php 画图 (采用php的GD库)

一.先检验测试是否你已经安装了GD2函数库。----------查看php.ini

如果没有看到就修改一下php.ini,将extension=php_gd2.dll前的分号去掉就OK了。

//建立一幅100*30的图像
        $image = imagecreatetruecolor(200,100);
        
        //设置背景颜色
        $bgcolor = imagecolorallocate($image,0,0,0);
        
        //设置字体颜色
        $textcolor = imagecolorallocate($image,255,255,255);
        
        //把字符串写在图像左上角
        imagestring($image,20,15,10,"Hello world!",$textcolor);
        
        //输出图像
        header("Content-type: image/jpeg");
        imagejpeg($image);

二.简单实例

直接开始创建图像:
一般步骤是:

1:创建一个背景图像,之后所有的图像基于该背景。
2:在背景上绘制图形轮廓或者输入文本。
3:输出最终图形。
4:清除所有资源。

//创建一幅图像的基本步骤
        $values = array(
                                40,50,
                                20,240,
                                60,60,
                                240,20,
                                50,40,
                                10,10,
                        );                                //设置点坐标,绘制六边形用一个数组把六个点的坐标储存起来
        //创建图像      是一个变长为250的正方形      此处用到函数ImageCreateTrueColor()
        $image = imagecreatetruecolor(250,250);       
        
        //设定颜色         以后要用到
        $bgcolor = imagecolorallocate($image,200,200,200);
        $blue = imagecolorallocate($image,0,0,255);
        
        //画一个多边形       用到函数ImageFilledPolygon()   相信下面括号里的参数一看就懂吧,记住我们要画六边形
        imagefilledpolygon($image,$values,6,$blue);
        
        //输出图像
        header("Content-type:image/png");   //此函数用来发送页面的http标题^_^ 小菜我也只是了解
        imagepng($image);                   //将图像以png的格式发送到浏览器
        imagedestroy($image);               //释放资源

三.用一个图片作为背景绘图

$image = imagecreatefromjpeg("abc.jpg");  //同样我们可以用一幅图片来当图像的背景    函数名相当容易理解(只要英语好^_^)
        $values = array(
                                40,50,
                                20,240,
                                60,60,
                                240,20,
                                50,40,
                                10,10,
                        );        
        $blue = imagecolorallocate($image,0,0,255);
        imagefilledpolygon($image,$values,6,$blue);
        header("Content-type: image/jpeg");
        imagejpeg($image);
        imagedestroy($image);

记得在输出图像的时候,我们用到了imagepng(),我们只要再加一个参数就可以保存我们的图像到我们的当前目录下,具体代码如下:

//将图像保存到文件
        
        $img = imagecreatetruecolor(200,200);
        
        $white = imagecolorallocate($img,255,255,255);
        $black = imagecolorallocate($img,0,0,0);
        
        imagearc($img,100,100,150,150,0,360,$white);
        
        imagepng($img,"testpng.png");     //在我们的当前目录下就可以看到一个testpng.png图片
        
        imagedestroy($img);

四.一个RGB图的实例:

$size = 300;
        $image = imagecreatetruecolor($size,$size);
        
        //用白色背景,黑色边框画方框
        $back = imagecolorallocate($image,255,255,255);
        $border = imagecolorallocate($image,0,0,0);
        
        imagefilledrectangle($image,0,0,$size,$size,$back);                //画出白色背景
        
        imagerectangle($image,0,0,$size-1,$size-1,$border);     //画出黑色边框
        
        $yellow_x = 150;
        $yellow_y = 75;
        
        $red_x = 100;
        $red_y = 160;
        
        $blue_x = 200;
        $blue_y = 160;
        $radius = 150;
        
        $yellow = imagecolorallocatealpha($image,255,255,0,75);         //此函数将黄色的alpha值调为75,就是透明度
        $red    = imagecolorallocatealpha($image,255,0,0,75);
        $blue   = imagecolorallocatealpha($image,0,0,255,75);
        
        //画三个交叠的圆
        imagefilledellipse($image,$yellow_x,$yellow_y,$radius,$radius,$yellow);    //此函数就是我要在$image上画一个圆心($yellow_x,$yellow_y)半径为$radius/2颜色为$yellow的圆
        imagefilledellipse($image,$red_x,$red_y,$radius,$radius,$red);
        imagefilledellipse($image,$blue_x,$blue_y,$radius,$radius,$blue);
        
        //输出正确的header
        header("Content-type: image/png");
        
        //输出结果
        imagepng($image);
        imagedestroy($image);

获取像素的值:

//获取像素的颜色
        
        $image = imagecreatefromjpeg("abc.jpg");
        
        $rgb   = imagecolorat($image,100,100);
        
        $r        =        ($rgb>>16) & 0xFF;         
        $g        =        ($rgb>>8)  & 0xFF;
        $b        =        ($rgb)     & 0xFF;
        
        echo "红:$r 绿:$g 蓝:$b";

五.一个3D效果的柄状图:

$image = imagecreatetruecolor(100,100);
        
        //设定颜色以后备用,从变量名中就可看出是什么颜色了
        $white = imagecolorallocate($image,0xFF,0xFF,0xFF);
        $gray  = imagecolorallocate($image,0xC0,0xC0,0xC0);
        $darkgray = imagecolorallocate($image,0x90,0x90,0x90);
        $navy = imagecolorallocate($image,0x00,0x00,0x80);
        $darknavy = imagecolorallocate($image,0x00,0x00,0x50);
        $red = imagecolorallocate($image,0xFF,0x00,0x00);
        $darkred = imagecolorallocate($image,0x90,0x00,0x00);
        
        //制作3D效果
        for ($i = 60;$i > 50;$i--){
                imagefilledarc($image,50,$i,100,50,0,45,$darknavy,IMG_ARC_PIE);    //我要在$image上画一个圆心(50,$i)长100宽50,0(3点钟位置)——45度,颜色为darknavy的椭圆,最后一个参数不懂
                imagefilledarc($image,50,$i,100,50,45,75,$darkgray,IMG_ARC_PIE);
                imagefilledarc($image,50,$i,100,50,75,360,$darkred,IMG_ARC_PIE);
        }    //此处的for循环是将柄状图的高体现出来,百分比的体现和下面一样
        imagefilledarc($image,50,50,100,50,0,45,$navy,IMG_ARC_PIE);
        imagefilledarc($image,50,50,100,50,45,75,$gray,IMG_ARC_PIE);
        imagefilledarc($image,50,50,100,50,75,360,$red,IMG_ARC_PIE);
        
        //输出图像
        header("Content-type: image/png");
        imagepng($image);
        
        imagedestroy($image);

六.处理画布透明背景色

<?PHP
/*
 *$sourePic:原图路径
 * $smallFileName:小图名称
 * $width:小图宽
 * $heigh:小图高
 * 转载注明 www.hzhuti.com */
function pngthumb($sourePic,$smallFileName,$width,$heigh){
 $image=imagecreatefrompng($sourePic);//PNG
    imagesavealpha($image,true);//这里很重要 意思是不要丢了$sourePic图像的透明色;
    $BigWidth=imagesx($image);//大图宽度
   $BigHeigh=imagesy($image);//大图高度
   $thumb = imagecreatetruecolor($width,$heigh);
   imagealphablending($thumb,false);//这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色;
   imagesavealpha($thumb,true);//这里很重要,意思是不要丢了$thumb图像的透明色;
   if(imagecopyresampled($thumb,$image,0,0,0,0,$width,$heigh,$BigWidth,$BigHeigh)){
   imagepng($thumb,$smallFileName);}
   return $smallFileName;//返回小图路径 }
 
pngthumb("a.png", "c.png", 300, 300);//调用
?>


  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值