PHP Doku:: Set the thickness for line drawing - function.imagesetthickness.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzBildverarbeitung und -generierungBildbearbeitung und GDGD- und Image-Funktionenimagesetthickness

Ein Service von Reinhard Neidl - Webprogrammierung.

GD- und Image-Funktionen

<<imagesetstyle

imagesettile>>

imagesetthickness

(PHP 4 >= 4.0.6, PHP 5)

imagesetthicknessSet the thickness for line drawing

Beschreibung

bool imagesetthickness ( resource $image , int $thickness )

imagesetthickness() sets the thickness of the lines drawn when drawing rectangles, polygons, ellipses etc. etc. to thickness pixels.

Parameter-Liste

image

Eine von den verschiedenen Erzeugungsfunktionen wie imagecreatetruecolor() gelieferte Grafikressource.

thickness

Thickness, in pixels.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 imagesetthickness() example

<?php
// Create a 200x100 image
$im imagecreatetruecolor(200100);
$white imagecolorallocate($im0xFF0xFF0xFF);
$black imagecolorallocate($im0x000x000x00);

// Set the background to be white
imagefilledrectangle($im0029999$white);

// Set the line thickness to 5
imagesetthickness($im5);

// Draw the rectangle
imagerectangle($im141418585$black);

// Output image to the browser
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Output of example : imagesetthickness()

Anmerkungen

Hinweis: Diese Funktion setzt die GD-Bibliothek in der Version 2.0.1 oder höher (empfohlen wird mindestens 2.0.28) voraus.


8 BenutzerBeiträge:
- Beiträge aktualisieren...
fred at nowhere dot fr
10.10.2009 21:05
One thing worse to mention is that imagesetthikness() works on the next object you draw.
For ex : you can draw a grid within a grapg with a thickness of 1

by invoking imagesetthickness($image, 1);

... script to draw your grid...

and then invoke imagesetthikness with a draw your graph lines with a thickness of 3
imagesetthickness($image, 3);
... script to draw your graph lines...

Hope this helps...
raffi dot semerciyan at gmail dot com
24.09.2009 17:42
While recently "playing" with GD library for PHP I also encountered the problem of imagesetthickness not working with imageellipse.

I think I found a good work around and wanted to share it  with you:

Instead of using

<?php imageellipse($image, $pos_x, $pos_y, $elipse_w, $elipse_h, $color); ?>

I successfully used:

<?php imagearc($image, $pos_x, $pos_y, $elipse_w, $elipse_h, 0, 359.9, $color); ?>

NOTE: Don't put exactly 360 instead of 359.9 because it seems that the implementation makes the test and uses imageellipse instead!
Below is my Ellipse function that is sensitive to imagesetthickness function AND that creates a properly closed ellipse.

<?php
 
function myimageellipse($image, $x, $y, $rx, $ry, $color)
  {
   
// We don't use imageellipse because the imagesetthickness function has
    // no effect. So the better workaround is to use imagearc.
   
imagearc($image,$x,$y,$rx,$ry, 0,359,$color);

   
// If we stop here, we don't have a properly closed ellipse.
    // Using imagefill at this point will flood outside the ellipse (actually arc).

    // We have to close the arc to make it a real ellipse.
   
$cos359=0.99984769;
   
$sin359=-0.01745240;

   
$x1=round($x+$rx/2*$cos359);
   
$y1=round($y+$ry/2*$sin359);
   
   
$x2=round($x+$rx/2);
   
$y2=round($y);

   
// imageline is sensitive to imagesetthickness as well.
   
imageline($image,$x1,$y1,$x2,$y2,$color);

  }
?>
jean-raymond dot chauviere at gmail dot com
30.06.2009 10:20
An easier to manage thickness is, before to draw in the ellipse to play with 2 ellipse with different color :

<?php
        imagefilledellipse 
($this->image,60,42,57,57,$drawColor);
       
imagefilledellipse  ($this->image,60,42,45,45,$backColor);
?>

The first line draw a filled ellipse with the wanted color, and the 2nd one, draw an ellipse with the background color from the same center, but is smaller.

The drawback of this method is that you erase everything in the middle of the ellipse.
admin at circle14 dot net
2.02.2009 2:33
Here is a custom function I wrote that addresses the line thickness issues with ellipses :

<?php
function draw_oval ($image, $pos_x, $pos_y, $elipse_width, $elipse_height, $color, $px_thick) {
   
$line = 0;
   
$thickness = $px_thick;
   
$elipse_w = $elipse_width;
   
$elipse_h = $elipse_height;
    while (
$line < $thickness) {
       
imageellipse($image, $pos_x, $pos_y, $elipse_w, $elipse_h, $color);
       
$line++;
       
$elipse_w--;
       
$elipse_h--;
    }
}
?>

I hope you find this useful.
bpatru at gmail dot com
28.09.2008 22:01
Apparently imagesetthickness doesn't work if antialiasing is set to true.
baldurien at bbnwn dot eu
12.03.2008 1:28
The way that imagesetthickness works with imagerectangle() is pretty strange.

<?php
imagesetthickness
(1);
imagerectangle($im, 10, 10, 50, 50, $red);
?>

This will draw a 41x41 square (because gd need the bottom right pixel, inclusive. 50 should get replaced by 49). This will "work" like:

<?php
imageline
($im, 10, 10, 10, 50, $red);
imageline($im, 10, 10, 50, 10, $red);
imageline($im, 50, 10, 50, 50, $red);
imageline($im, 10, 50, 50, 50, $red);
?>

The second example:

<?php
imagesetthickness
(2);
imagerectangle($im, 10, 10, 50, 50, $red);
?>

This will draw a 43x43 square because the border (thickness) is set to 2. *however* this is not a "regular" border of 2 pixels around the 41x41 original square!

On the left and right, there will be a thickness of 3, while there we be a thickness of 2.

If you take the imageline example, but set the thickness before to 2, this will *almost* do the trick: the left most pixel of the square will not be drawn.

To conclude:

1) do not forget that (width, height) of drawn rectangle is (x2-x1+1, y2-y1+1)
2) thickness is bad implemented (or at least, the behavior i s not documented) on rectangle, as the left/right thickness is not the wanted one.
3) 4*imageline() should do the trick, but after "patching" the top left pixel.
ab at cd dot com
27.06.2007 13:05
Note: Also, for me (working under PHP 5.0.2) this function ONLY seems to work with imageline...
-private-
28.03.2007 11:01
There is a known bug. Imagesetthickness is NOT working on ellipse.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)