PHP | imagesetstyle() function

The imagesetstyle() function is an inbuilt function in PHP which is used to set the style to be used by all line drawing functions like imageline() or imagepolygon().
Syntax:
bool imagesetstyle( resource $image, array $style )
Parameters:This function accepts two parameters as mentioned above and described below:
- $image: It specifies the image resource to work on.
- $style: It specifies the array of pixel colors.
Return Value: This function returns TRUE on success or FALSE on failure.
Below given programs illustrate the imagesetstyle() function in PHP:
Program 1 (Drawing on an image):
<?php // Load the png image $image = imagecreatefrompng(   // Prepare the colors $red = imagecolorallocate($image, 255, 0, 0); $green  = imagecolorallocate($image, 0, 255, 0);   // Draw a dashed line, 5 red pixels, 5 white pixels $style = array($red, $red, $red, $red, $red, $green, $green, $green, $green, $green); imagesetstyle($image, $style); imageline($image, 0, 100, 800, 100, IMG_COLOR_STYLED);   // Output image to the browser header('Content-type: image/png'); imagepng($image); ?> |
Output:
Program 2 (Drawing on a blank drawing):
<?php //Create a blank image $image = imagecreatetruecolor(700, 200); Â Â // Set the background color of image $background_color = imagecolorallocate($image, 255, 255, 255); Â Â // Fill background with above selected color imagefill($image, 0, 0, $background_color); Â Â // Prepare the colors $red = imagecolorallocate($image, 255, 0, 0); $green = imagecolorallocate($image, 0, 255, 0); Â Â // Draw a dashed line, 7 red pixels, 3 white pixels $style = array($red, $red, $red, $red, $red, $ren, $red, $green, $green, $green); imagesetstyle($image, $style); Â Â // Set the vertices of polygon $values = array( Â Â Â Â 150, 150, Â Â Â Â 450, 150, Â Â Â Â 150, 50, ); Â Â // Draw the polygon imagepolygon($image, $values, 3, IMG_COLOR_STYLED); Â Â // Output image to the browser header('Content-type: image/png'); imagepng($image); ?> |
Output:
Reference: https://www.php.net/manual/en/function.imagesetstyle.php
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!




