PHP 8.1.24 Released!


(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

imagesetstyleConfigure le style pour le dessin des lignes


imagesetstyle(GdImage $image, array $style): bool

imagesetstyle() permet de choisir le style à utiliser lors du dessin des lignes (comme avec les fonctions imageline() et imagepolygon()) lors de l'utilisation de la couleur spéciale IMG_COLOR_STYLED ou bien lors du dessin de lignes avec la couleur IMG_COLOR_STYLEDBRUSHED.

Liste de paramètres


Un objet GdImage, retournée par une des fonctions de création d'images, comme imagecreatetruecolor().


Un tableau de couleurs de pixels. Vous pouvez utiliser la constante IMG_COLOR_TRANSPARENT pour ajouter un pixel transparent. Notez que style ne doit pas être un tableau array vide.

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.


L'exemple suivant dessine une ligne pointillée depuis le coin supérieur gauche vers le coin inférieur droit de l'image :

Exemple #1 Exemple pour imagesetstyle()

("Content-type: image/jpeg");
$im = imagecreatetruecolor(100, 100);
$w = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocate($im, 255, 0, 0);

/* Dessine une ligne pointillée de 5 pixels rouges, 5 pixels blancs */
$style = array($red, $red, $red, $red, $red, $w, $w, $w, $w, $w);
imagesetstyle($im, $style);
imageline($im, 0, 0, 100, 100, IMG_COLOR_STYLED);

/* Dessine une ligne avec des smileys, en utilisant imagesetbrush() et imagesetstyle */
$style = array($w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $red);
imagesetstyle($im, $style);

$brush = imagecreatefrompng("");
$w2 = imagecolorallocate($brush, 255, 255, 255);
imagecolortransparent($brush, $w2);
imagesetbrush($im, $brush);
imageline($im, 100, 0, 0, 100, IMG_COLOR_STYLEDBRUSHED);


Résultat de l'exemple ci-dessus est similaire à :

Affichage de l'exemple : imagesetstyle()

Voir aussi

add a note

User Contributed Notes 7 notes

Michael_Todd_335 at yahoo dot com
16 years ago
Watch out! If you pass imagesetstyle() an empty array as the second argument, it will crash your server!
I was messing with it just earlier and accidentally did so, and the page took a good minute to process, when my Apache server came up with the good ol' Windows 'Send Error Report' window.
6 years ago
To clarify, for lines where the thickness is greater than 1, the total length of the $style array needs to be an exact divisor of the length of the line.

This is because the pattern is repeated lengthways and wraps onto the second row of pixels, causing staggering to occur.

So if you have 5 red and 5 white pixels, and you want a line length of 55 pixels, either change the length to a multiple of 10, or change the dashes to, say, 6 red and 5 white.
15 years ago
Function to make a line with random fading:

function fading_line($img,$sx,$sy,$ex,$ey){
$a = array(255-((255/$l)*$i), 255,0,(255/$l)*$i/2,(255/$l)*$i,(255-((255/$l)*$i))/2);

fading_line($img,10,20,490,40); // image, start x, start y, end x, end y
Cruz at FtUC dot net
17 years ago
When lines drawn with imagesetstyle seem to produce a thin white line only, make sure antialiasing is disabled.

imageantialias($im, false);
$style = array($gridxcolor, $gridxcolor, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT);
imagesetstyle($im, $style);
imageline($im, $x, 0, $x, $ymax+5, IMG_COLOR_STYLED);
imageantialias($im, true);

Setstyle and Antialias don't go together.
10 years ago
a shortcut for basic dashed lines, making it easy to adjust the lengths:


= 20;
$length2 = 10;
$style = array_merge(array_fill(0, $length1, $red), array_fill(0, $length2, $w));
imagesetstyle($im, $style);

php at imperium dot be
13 years ago
Be aware that styles are applied towards the width of the line instead of linear.
To convert a style to be used for thick lines you can use the function below:

ImageStyleThicken(<aStyle>,<iThickness>) --> <aThickStyle>

<aStyle> is the style array for a thickness of 1 (see imagesetstyle()).
<iThickness> is the new thickness to apply (see imagesetthickness()).

<aThickStyle> is the style array suitable for the given thickness.
function ImageStyleThicken($_1,$_2) {
$a = array();
foreach (
$_1 as $x) {
$i = $_2;
$a[] = $x; while (--$i>0); }
zackbloom at gmail dot com
17 years ago
Use this to set the style to any combination of pixels.
You can pass as many modifiers as you wish.
Use the format [num]r[red]g[green]b[blue].

For example:

imageline($im, 0, 0, 600, 600, IMG_COLOR_STYLED);

function dashed($im){
$size = func_num_args();
for($i = 0; $i < $size; $i++){
$arg = func_get_arg($i);
$color = imagecolorallocate($im,$r,$g,$b);
$x = substr($arg,0,strpos($arg,"r"));
$vals[$i] = array_fill(0,$x,$color);
if(array_key_exists($k,$vals)) $prop=array_merge($prop,$vals[$k]);
imagesetstyle($im, $prop);
return $im;
To Top