imagesetstyle

(PHP 4 >= 4.0.6, PHP 5)

imagesetstyle -- Set the style for line drawing

Description

bool imagesetstyle ( resource image, array style )

imagesetstyle() sets the style to be used by all line drawing functions (such as imageline() and imagepolygon()) when drawing with the special color IMG_COLOR_STYLED or lines of images with color IMG_COLOR_STYLEDBRUSHED. Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

The style parameter is an array of pixels. Following example script draws a dashed line from upper left to lower right corner of the canvas:

Пример 1. imagesetstyle() example

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

/* Draw a dashed line, 5 red pixels, 5 white pixels */
$style = array($red, $red, $red, $red, $red, $w, $w, $w, $w, $w);
imagesetstyle($im, $style);
imageline($im, 0, 0, 100, 100, IMG_COLOR_STYLED);

/* Draw a line of happy faces using imagesetbrush() with 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);
imagedestroy($im);
?>

See also imagesetbrush(), imageline().

Замечание: This function was added in PHP 4.0.6



imagesetstyle
Cruz at FtUC dot net
15-Jan-2006 03:45
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.
zackbloom at gmail dot com
06-Dec-2005 02:44
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;
}
yhoko at yhoko dot com
11-Aug-2004 06:24
When drawing lines that are >1px thick you'll have to setup the array corresponding to this fact (since there's no multi-array support for this function).

For example if the line's 3 pixels thick each 2nd color goes to the 2nd line, each 3rd color to the 3rd line and so on.
08-Aug-2001 02:49
The correct way to do dashed lines with transparent spaces between the dashes is to use the constant IMG_COLOR_TRANSPARENT instead of a color, like in this example:

$style=array($color, $color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT );
ImageSetStyle($img, $style);
hatamoto at hhole dot com
27-Jul-2001 07:12
Okay, I answered my own question. ;)

If you use truecolor image modes and generate colors that have alpha values, those colors will be drawn correctly (ie: with appropriate transparency). So the following should draw a blue box with a white dashed line diagonally across it. It should NOT be a white solid line.

<?
$image
= imagecreatetruecolor(200,200);
imagealphablending($image, TRUE);
$blue = imagecolorallocate($image, 0, 0, 0xff);
$white = imagecolorallocate($image, 0xff, 0xff, 0xff);
$t = imagecolorresolvealpha($image, 0xff, 0xff, 0xff, 0xff);

$style = array($white, $white, $white, $t, $t, $t);

imagesetstyle($image, $style);
imagefilledrectangle($image, 0, 0, 199, 199, $blue);
imageline($image, 0, 0, 199, 199, IMG_COLOR_STYLED);

header("Content-type: image/jpeg");
imagejpeg($image);
?>

<imagesetpixelimagesetthickness>
 Last updated: Tue, 15 Nov 2005