Open In App

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:

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


Article Tags :