Open In App

PHP | imagesetstyle() function

Last Updated : 14 Feb, 2020
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
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
<?php
// Load the png image
$image = imagecreatefrompng(
    'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

// 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
<?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

Next Article

Similar Reads