如何在 PHP 中使用 imagesetpixel() 函式設定單個畫素?
imagesetpixel() 是 PHP 中的一個內建函式,用於設定列出座標處的單個畫素。
語法
bool imagesetpixel(resource $image, int $x, int $y, int $color)
引數
imagesetpixel() 接受四個引數:$image、$x、$y 和 $color。
$image − 指定要操作的影像資源。
$x − 指定畫素的 x 座標。
$y − 指定畫素的 y 座標。
$color − 指定畫素的顏色。
返回值 −
imagesetpixel() 成功時返回 True,失敗時返回 False。
示例 1
<?php // Load the png image using imagecreatefromjpeg() function $img = imagecreatefromjpeg('C:\xampp\htdocs\test\29.jpg'); // Draw the line using imagesetpixel() function $blue = imagecolorallocate($img, 255, 255, 0); for ($i = 0; $i < 1000; $i++) { imagesetpixel($img, $i, 100, $blue); } // Show the output image to the browser header('Content-type: image/png'); imagepng($img); ?>
輸出

示例 2
<?php $x = 700; $y = 300; $gd = imagecreatetruecolor($x, $y); $corners[0] = array('x' => 100, 'y' => 10); $corners[1] = array('x' => 0, 'y' => 170); $corners[2] = array('x' => 190, 'y' => 170); $blue = imagecolorallocate($gd, 255, 0, 0); for ($i = 0; $i < 100000; $i++) { imagesetpixel($gd, round($x),round($y), $blue); $a = rand(0, 2); $x = ($x + $corners[$a]['x']) / 2; $y = ($y + $corners[$a]['y']) / 2; } header('Content-Type: image/png'); imagepng($gd); ?>
輸出
.jpg)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP