當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imagesetpixel()用法及代碼示例

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



相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | imagesetpixel() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。