Sunday, May 24, 2026
HomeLanguagesPHP | imagesetstyle() function

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, neveropen 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!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS