php如何设置画布颜色

本篇内容介绍了“php如何设置画布颜色”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业川西大数据中心,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

设置画布颜色的方法:1、使用“imagecolorallocate(image,red,green,blue)”语句;2、使用“imagecolorallocatealpha(image,red,green,blue,alpha)”语句。

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑

方法1:imagecolorallocate() 函数

imagecolorallocate() 函数可以为一个图像资源分配颜色,如果在图像中需要设置多种颜色,只要多次调用该函数即可。函数的语法格式如下所示:

imagecolorallocate(resource $image, int $red, int $green, int $blue):

其中,$image 为要设置颜色的图像资源,imagecolorallocate() 函数会返回一个标识符,代表了由给定的 RGB 成分组成的颜色;$red,$green 和 $blue 分别是所需要的颜色的红,绿,蓝成分,取值范围是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。

提示:如果是使用 imagecreate() 函数创建的图像资源,在第一次调用 imagecolorallocate() 函数时会默认为其填充背景色。

【示例】使用 imagecolorallocate() 函数为图像设置颜色。

<?php
    $image = imagecreate(100, 100);
    $blue = imagecolorallocate($image, 0, 0, 255);
    $red = imagecolorallocate($image, 255, 0, 0);
    $green = imagecolorallocate($image, 0, 255, 0);
    header('Content-type:image/jpeg');
    imagejpeg($image);
    imagedestroy($image);
?>

运行结果如下图所示:

php如何设置画布颜色

方法2:使用imagecolorallocatealpha() 函数

imagecolorallocatealpha() 函数的作用和 imagecolorallocate() 相同,但多了一个额外的设置透明度的参数 alpha,函数的语法格式如下:

imagecolorallocatealpha(resource $image, int $red, int $green, int $blue, int $alpha)

其中,$image 为要设置颜色的图像资源;$red,$green 和 $blue 分别是所需要的颜色的红,绿,蓝成分,取值范围是 0 到 255 的整数或者十六进制的 0x00 到 0xFF;$alpha 用来设置颜色的透明的,取值范围在 0 到 127 之间,0 表示完全不透明,127 则表示完全透明。

【示例】使用 imagecolorallocatealpha() 函数为图像设置颜色。

<?php
    $size=300;
    $image=imagecreatetruecolor($size,$size);
    //用白色背景加黑色边框画个方框
    $back=imagecolorallocate($image,255,255,255);
    $border=imagecolorallocate($image,0,0,0);
    imagefilledrectangle($image,0,0,$size-1,$size-1,$back);
    imagerectangle($image,0,0,$size-1,$size-1,$border);
    $yellow_x=100;
    $yellow_y=75;
    $red_x=120;
    $red_y=165;
    $blue_x=187;
    $blue_y=125;
    $radius=150;
    //用alpha值分配一些颜色
    $yellow=imagecolorallocatealpha($image,255,255,0,75);
    $red=imagecolorallocatealpha($image,255,0,0,75);
    $blue=imagecolorallocatealpha($image,0,0,255,75);
    //画3个交迭的圆
    imagefilledellipse($image,$yellow_x,$yellow_y,$radius,$radius,$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);
?>

运行结果如下图所示:

php如何设置画布颜色

“php如何设置画布颜色”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!

文章名称:php如何设置画布颜色
链接分享:https://www.cdcxhl.com/article44/pjgjhe.html

成都网站建设公司_创新互联,为您提供外贸建站网站内链关键词优化定制开发网站设计公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发