Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> PHP

PHP – Thiết lập kiểu nét vẽ với imagesetstyle()

imagesetstyle() là hàm nội bộ trong PHP dùng để thiết lập kiểu (style) cho vẽ đường thẳng. Nó có thể dùng bởi tất cả hàm vẽ đường thẳng như imagepolygon hoặc imageline.

Cú pháp

bool imagesetstyle(GdImage $image, array $style)

Tham số

imagesetstyle() nhận hai tham số: $image và $style.

  • $image – Tài nguyên ảnh để làm việc.

  • $style – Mảng các màu điểm ảnh.

Giá trị trả về

imagesetstyle() trả về true khi thành công hoặc false khi thất bại.

Ví dụ 1 – Vẽ đường nét đứt và dùng brush

<?php
header("Content-type: image/jpeg");
$img = imagecreatetruecolor(700, 300);
$w = imagecolorallocate($img, 122, 122, 122);
$red = imagecolorallocate($img, 255, 0, 0);
/* Vẽ đường nét đứt: 5 pixel đỏ, 5 pixel trắng */
$style = array($red, $red, $red, $red, $red, $w, $w, $w, $w, $w);
imagesetstyle($img, $style);
imageline($img, 0, 0, 200, 200, IMG_COLOR_STYLED);
/* Vẽ đường mặt cười dùng imagesetbrush() với imagesetstyle */
$style = array($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);
?>

Ảnh đầu vào (Brush)

PHP – Thiết lập kiểu nét vẽ với imagesetstyle()

Ảnh đầu ra

PHP – Thiết lập kiểu nét vẽ với imagesetstyle()

Ví dụ 2 – Đường nét đứt xanh lá/xanh dương

<?php
$img = imagecreatefrompng('C:\xampp\htdocs\Images\img34.png');
$blue = imagecolorallocate($img, 0, 0, 255);
$green = imagecolorallocate($img, 0, 255, 0);
/* Vẽ đường nét đứt: 5 pixel xanh dương, 5 pixel xanh lá */
$style = array($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);
?>

Kết quả

PHP – Thiết lập kiểu nét vẽ với imagesetstyle()