搜索
首页后端开发PHP问题php怎么设置画布颜色

php怎么设置画布颜色

Jul 19, 2021 pm 08:05 PM
php

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

php怎么设置画布颜色

本教程操作环境: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(&#39;Content-type:image/jpeg&#39;);
    imagejpeg($image);
    imagedestroy($image);
?>

运行结果如下图所示:

1.png

方法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(&#39;Content-type:image/png&#39;);
    //最后输出结果
    imagepng($image);
    imagedestroy($image);
?>

运行结果如下图所示:

2.png

推荐学习:《PHP视频教程

以上是php怎么设置画布颜色的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器