Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the image thickness for line drawing using imgesetthickness() function in PHP?
imagesetthickness() is an inbuilt function in PHP that is used to set the thickness for line drawing.
Syntax
bool imagesetthickness($image, $thickness)
Parameters
imagesetthickness() accepts two parameters − $image and $thickness.
-
$image − This parameter is returned by an image creation function such as imagecreatetruecolor(). It is used to create the size of an image.
-
$thickness − This parameter sets the thickness in pixels.
Return Values
imagesetthickness() returns True on success and False on failure.
Note: This function requires the GD extension to be enabled in PHP. Most PHP installations include GD by default.
Example 1 − Drawing a Rectangle with Thick Border
<?php
// Create an image of a given size
$img = imagecreatetruecolor(700, 300);
$gray = imagecolorallocate($img, 0, 0, 255);
$white = imagecolorallocate($img, 0xff, 0xff, 0xff);
// Set the gray background color
imagefilledrectangle($img, 0, 0, 700, 300, $gray);
// Set the line thickness to 10
imagesetthickness($img, 10);
// Draw the rectangle
imagerectangle($img, 30, 30, 200, 150, $white);
// Output image to the browser
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>
The above code creates a blue background with a white rectangle having a 10−pixel thick border.
Example 2 − Drawing a Thick Line
<?php
// Create an image of given size using imagecreatetruecolor() function
$img = imagecreatetruecolor(700, 300);
$blue = imagecolorallocate($img, 0, 0, 255);
$white = imagecolorallocate($img, 0xff, 0xff, 0xff);
// Set the blue background color
imagefilledrectangle($img, 0, 0, 700, 300, $blue);
// Set the line thickness to 50
imagesetthickness($img, 50);
// Draw the white line
imageline($img, 50, 50, 250, 50, $white);
// Output image to the browser
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
?>
This example demonstrates drawing a horizontal line with 50−pixel thickness on a blue background.
Key Points
-
The thickness setting affects all subsequent line drawing operations on the image resource.
-
Thickness applies to functions like
imageline(),imagerectangle(), andimagepolygon(). -
Default line thickness is 1 pixel if not explicitly set.
Conclusion
The imagesetthickness() function provides a simple way to control line thickness in PHP image manipulation. It's essential for creating bold graphics and emphasizing visual elements in dynamically generated images.
