imagesetstyle

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

imagesetstyle β€” УстанавливаСт ΡΡ‚ΠΈΠ»ΡŒ рисования Π»ΠΈΠ½ΠΈΠΉ

ОписаниС

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

Ѐункция imagesetstyle() устанавливаСт ΡΡ‚ΠΈΠ»ΡŒ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄ΡƒΡ‚ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ рисования Π»ΠΈΠ½ΠΈΠΉ Π½Π°ΠΏΠΎΠ΄ΠΎΠ±ΠΈΠ΅ imageline() ΠΈ imagepolygon() ΠΏΡ€ΠΈ установкС ΡΠΏΠ΅Ρ†ΠΈΠ°Π»ΡŒΠ½ΠΎΠ³ΠΎ Ρ†Π²Π΅Ρ‚Π° IMG_COLOR_STYLED ΠΈΠ»ΠΈ IMG_COLOR_STYLEDBRUSHED.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.
style

Массив с Ρ†Π²Π΅Ρ‚Π°ΠΌΠΈ пиксСлСй. ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ константу IMG_COLOR_TRANSPARENT для добавлСния ΠΏΡ€ΠΎΠ·Ρ€Π°Ρ‡Π½ΠΎΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ нСльзя ΠΏΠ΅Ρ€Π΅Π΄Π°Π²Π°Ρ‚ΡŒ Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ style пустой массив.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

Π‘Π»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΉ ΠΏΡ€ΠΈΠΌΠ΅Ρ€ рисуСт ΠΏΡƒΠ½ΠΊΡ‚ΠΈΡ€Π½ΡƒΡŽ линию ΠΈΠ· Π»Π΅Π²ΠΎΠ³ΠΎ Π²Π΅Ρ€Ρ…Π½Π΅Π³ΠΎ ΡƒΠ³Π»Π° изобраТСния Π² ΠΏΡ€Π°Π²Ρ‹ΠΉ Π½ΠΈΠΆΠ½ΠΈΠΉ:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ установки стиля рисования Π»ΠΈΠ½ΠΈΠΉ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagesetstyle()

<?php

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

/* РисованиС ΠΏΡƒΠ½ΠΊΡ‚ΠΈΡ€Π½ΠΎΠΉ Π»ΠΈΠ½ΠΈΠΈ, 5 красных Ρ‚ΠΎΡ‡Π΅ΠΊ, 5 Π±Π΅Π»Ρ‹Ρ… */
$style = array($red, $red, $red, $red, $red, $w, $w, $w, $w, $w);
imagesetstyle($im, $style);
imageline($im, 0, 0, 100, 100, IMG_COLOR_STYLED);

/* РисованиС Π»ΠΈΠ½ΠΈΠΈ ΠΈΠ· смайликов функциями imagesetbrush() с imagesetstyle() */
$style = array($w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $w, $red);
imagesetstyle($im, $style);

$brush = imagecreatefrompng("http://www.libpng.org/pub/png/images/smile.happy.png");
$w2 = imagecolorallocate($brush, 255, 255, 255);
imagecolortransparent($brush, $w2);
imagesetbrush($im, $brush);
imageline($im, 100, 0, 0, 100, IMG_COLOR_STYLEDBRUSHED);

imagejpeg($im);

?>

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°: imagesetstyle()

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagesetbrush() - УстанавливаСт ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ кисти для рисования Π»ΠΈΠ½ΠΈΠΉ
  • imageline() - РисуСт ΠΎΡ‚Ρ€Π΅Π·ΠΎΠΊ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 7 notes

up
1
Michael_Todd_335 at yahoo dot com ΒΆ
18 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.
up
0
dazbert ΒΆ
9 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.
up
0
Anonymous ΒΆ
13 years ago
a shortcut for basic dashed lines, making it easy to adjust the lengths:

<?php

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

?>
up
-1
Cruz at FtUC dot net ΒΆ
20 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.
up
-2
php at imperium dot be ΒΆ
15 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:

<?php
/*
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;
        do $a[] = $x; while (--$i>0); }
    return $a;
}
?>
up
-2
Wander ΒΆ
18 years ago
Function to make a line with random fading:

<?php
    function fading_line($img,$sx,$sy,$ex,$ey){
        $r=rand(0,5);$g=rand(0,5);$b=rand(0,5);
        $l=sqrt((($ex-$sx)*($ex-$sx))+(($ey-$sy)*($ey-$sy)));
        for($i=0;$i<$l;$i++){
            $a = array(255-((255/$l)*$i), 255,0,(255/$l)*$i/2,(255/$l)*$i,(255-((255/$l)*$i))/2);
            $style[]=imagecolorallocate($img,$a[$r],$a[$g],$a[$b]);
        }
        imagesetstyle($img,$style);
        imageline($img,$sx,$sy,$ex,$ey,IMG_COLOR_STYLED);
    }
    
    fading_line($img,10,20,490,40);    // image, start x, start y, end x, end y
?>
up
-1
zackbloom at gmail dot com ΒΆ
20 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:

$im=dashed($im,"4r255g0b0","2r0g255b0","5r0g0b255");
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);
if(!is_resource($arg)){
$r=substr($arg,strpos($arg,"r")+1,
strpos($arg,"g")-strpos($arg,"r")-1);
$g=substr($arg,strpos($arg,"g")+1,
strpos($arg,"b")-strpos($arg,"g")-1);
$b=substr($arg,strpos($arg,"b")+1,
strlen($arg)-strpos($arg,"b"));
$color = imagecolorallocate($im,$r,$g,$b);
$x = substr($arg,0,strpos($arg,"r"));
$vals[$i] = array_fill(0,$x,$color);
}
}
for($k=0;$k<count($vals)+1;$k++)
if(array_key_exists($k,$vals)) $prop=array_merge($prop,$vals[$k]);
imagesetstyle($im, $prop);
return $im;
}