如何使用 PHP 中的 imgesetthickness() 函数设置线条绘制的图像粗细?

phpserver side programmingprogramming

imagesetthickness() 是 PHP 中的一个内置函数,用于设置线条绘制的粗细。

语法

bool imagesetthickness($image, $thickness)

参数

imagesetthickness() 接受两个参数:$image 和 $thickness。

  • $image 此参数由图像创建函数(例如 imagecreatetruecolor())返回。它用于创建图像的大小。

  • $thickness − 此参数设置图像的厚度(以像素为单位)。

返回值

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

示例 1

<?php
   // 创建指定大小的图像
   $img = imagecreatetruecolor(700, 300);
   $gray = imagecolorallocate($img, 0, 0, 255);
   $white = imagecolorallocate($img, 0xff, 0xff, 0xff);

   // 设置灰色背景颜色
   imagefilledrectangle($img, 0, 0, 700, 300, $gray);

   // 将线条粗细设置为 10
   imagesetthickness($img, 10);

   // 绘制矩形
   imagerectangle($img, 30, 30, 200, 150, $white);
   
   // 将图像输出到浏览器
   header('Content-Type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出

示例 2

<?php
   // 使用 imagecreatetruecolor() 函数创建指定大小的图像
   $img = imagecreatetruecolor(700, 300);
   $blue = imagecolorallocate($img, 0, 0, 255);
   $white = imagecolorallocate($img, 0xff, 0xff, 0xff);

   // 设置白色背景颜色
   imagefilledrectangle($img, 0, 0, 300, 200, $blue);

   // 将线条粗细设置为 50
   imagesetthickness($img, 50);

   // 绘制白线
   imageline($img, 50, 50, 250, 50, $white);

   // 将图像输出到浏览器
   header('Content-Type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

输出


相关文章