imagesetpixel()函数是PHP中的内置函数,用于在指定坐标处绘制像素。
用法:
bool imagesetpixel( resource $image, int $x, int $y, int $color )
参数:该函数接受上述和以下所述的四个参数:
- $image:它指定要处理的图像资源。
 - $x:它指定像素的x坐标。
 - $y:它指定像素的y坐标。
 - $color:它指定像素的颜色。
 
返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。
下面给出的程序说明了PHP中的imagesetpixel()函数:
程序1(在图像上画一条线):
的PHP
<?php 
    
// Load the png image 
$image = imagecreatefrompng( 
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png'); 
  
// Draw a line using imagesetpixel 
$red = imagecolorallocate($image, 255, 0, 0); 
for ($i = 0; $i < 1000; $i++) { 
    imagesetpixel($image, $i, 100, $red); 
} 
  
// Output image to the browser 
header('Content-type:image/png'); 
imagepng($image); 
?>输出:

程序2(绘制图案):
的PHP
<?php 
  
  // Create a blank image 700x200 
$image = imagecreatetruecolor(700, 200); 
  
$points = [ 
    array('x' => 00, 'y' => 10), 
    array('x' => 0, 'y' => 190), 
    array('x' => 800, 'y' => 190) 
]; 
  
// Prepare the color 
$green = imagecolorallocate($image, 0, 255, 0); 
  
// Draw the pattern 
$x = 700; 
$y = 200; 
for ($i = 0; $i < 100000; $i++) { 
    imagesetpixel($image, round($x), round($y), $green); 
    $a = rand(0, 2); 
    $x = ($x + $points[$a]['x']) / 2; 
    $y = ($y + $points[$a]['y']) / 2; 
} 
  
// Show the output in browser 
header('Content-Type:image/png'); 
imagepng($image); 
?>输出:

参考: https://www.php.net/manual/en/function.imagesetpixel.php
相关用法
- d3.js d3.map.set()用法及代码示例
 - PHP Ds\Map xor()用法及代码示例
 - CSS url()用法及代码示例
 - p5.js pow()用法及代码示例
 - PHP each()用法及代码示例
 - PHP next()用法及代码示例
 - p5.js day()用法及代码示例
 - CSS var()用法及代码示例
 - PHP pow( )用法及代码示例
 - d3.js d3.set.has()用法及代码示例
 - PHP Ds\Map put()用法及代码示例
 - p5.js hue()用法及代码示例
 
注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | imagesetpixel() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
