Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

PHP – ตั้งค่า Line Style ด้วย imagesetstyle()

ฟังก์ชัน imagesetstyle() ใช้ตั้งค่า style สำหรับการวาดเส้น ใช้ร่วมกับ imagepolygon() หรือ imageline()

รูปแบบการใช้งาน (Syntax)

bool imagesetstyle(GdImage $image, array $style)

พารามิเตอร์

  • $image – GdImage resource
  • $style – array ของ pixel colors (สลับสีเพื่อสร้าง dashed line ฯลฯ)

ค่าที่คืนกลับ

คืน true เมื่อสำเร็จ false เมื่อล้มเหลว

ตัวอย่าง 1: Dashed Line และ Styled Brush

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

// Dashed line: 5 red, 5 white
$style = [$red, $red, $red, $red, $red, $w, $w, $w, $w, $w];
imagesetstyle($img, $style);
imageline($img, 0, 0, 200, 200, IMG_COLOR_STYLED);

// Styled brush with happy face
$style = [$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);
?>

Input Image (Brush)

PHP – ตั้งค่า Line Style ด้วย imagesetstyle()

Output Image

PHP – ตั้งค่า Line Style ด้วย imagesetstyle()

ตัวอย่าง 2: Dashed Line สีน้ำเงิน-เขียว

<?php
$img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
$blue = imagecolorallocate($img, 0, 0, 255);
$green = imagecolorallocate($img, 0, 255, 0);
$style = [$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);
?>

ผลลัพธ์

PHP – ตั้งค่า Line Style ด้วย imagesetstyle()