如何使用 PHP 中的 imagesetstyle() 函数设置线条样式?

phpserver side programmingprogramming

imagesetstyle() 是 PHP 内置函数,用于设置线条样式。所有线条绘制函数(例如 imagepolygonimageline)都可以使用它。

语法

bool imagesetstyle(resource $image, array $style)

参数

imagesetstyle() 接受两个参数:$image$style

  • $image −指定要处理的图像资源。

  • $style − 指定像素颜色数组。

返回值

imagesetstyle() 成功时返回 True,失败时返回 False。

示例 1

<?php
   header("Content-type: image/jpeg");
   $img = imagecreatetruecolor(700, 300);
   $w = imagecolorallocate($img, 122, 122, 122);
   $red = imagecolorallocate($img, 255, 0, 0);

   /* 绘制一条虚线,5 个红色像素,5 个白色像素 */
   $style = array($red, $red, $red, $red, $w, $w, $w, $w, $w);
   imagesetstyle($img, $style);
   imageline($img, 0, 0, 200, 200, IMG_COLOR_STYLED);

   /* 使用 imagesetbrush() 和 imagesetstyle 绘制一排笑脸 */
   $style = array($w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $red);
   imagesetstyle($img, $style);
   $brush = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
   $w2 = imagecolorallocate($brush, 255, 255, 255);
   imagecolortransparent($brush, $w2);
   imagesetbrush($img, $brush);
   imageline($img, 200, 0, 0, 200, IMG_COLOR_STYLEDBRUSHED);

   imagejpeg($img);
   imagedestroy($img);
?>

输入图像

输出图像

示例 2

<?php
   // 使用 imagecreatefrompng() 函数加载 png 图像。
   $img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
   
   // 分配蓝色和绿色
   $blue = imagecolorallocate($img, 0, 0, 255);
   $green = imagecolorallocate($img, 0, 255, 0);

   // 绘制一条虚线,5 个蓝色像素,5 个白色像素
   $style = array($blue, $blue, $blue, $blue, $blue, $green, $green, $green, $green, $green);
   imagesetstyle($img, $style);
   imageline($img, 0, 100, 800, 100, IMG_COLOR_STYLED);
   // 将图片输出到浏览器
   header('Content-type: image/png');
   imagepng($img);
?>

输出


相关文章