php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch PHP-code_to_patch_bug_43475.txt for GD related Bug #43475

Patch version 2010-08-06 13:57 UTC

Return to Bug #43475 | Download this patch
Patch Revisions:

Developer: php@imperium.be

Patch based on the source code used for PHP bug 43475.
The functions below come from Q-lib, my own library.
Full authorisation to be used in the public domain.

Problem 1
	Horizontal lines aren't rendered correctly.
	To me it is weird that vertical lines work fine.
	In any case, this is fixed by ImageLineStyled().

Problem 2
	Style usage isn't clearly documented.
	I will add ImageStyleThicken() to the function notes.
	To my personal opinion, the way styles are applied IS CORRECT.
	When styles would be applied linear instead, things like
	rainbow lines and chained lines wouldn't be possible
	(unless using a very slow ImageSetPixel() variant).

<?php
$img = imagecreate(1600,800);
$bg = ImageColorAllocate($img, 255, 255, 255);
$fg = ImageColorAllocate($img,   0,   0,   0);
$style = array(); # Make a pattern of 16 on, 4 off, 8 on, 4 off
for ($i = 0; $i < 16; $i++) $style[] = $fg;
for ($i = 0; $i <  4; $i++) $style[] = IMG_COLOR_TRANSPARENT;
for ($i = 0; $i <  8; $i++) $style[] = $fg;
for ($i = 0; $i <  4; $i++) $style[] = IMG_COLOR_TRANSPARENT;

ImageSetStyle($img,$style);
ImageSetThickness($img,1);
ImagePolygon($img,array(50,250,550,250,550,750),3,IMG_COLOR_STYLED);
ImageSetStyle($img,ImageStyleThicken($style,2));
ImageSetThickness($img,2);
ImagePolygon($img,array(100,200,600,200,600,700),3,IMG_COLOR_STYLED);
ImageSetStyle($img,ImageStyleThicken($style,4));
ImageSetThickness($img,4);
ImagePolygon($img,array(150,150,650,150,650,650),3,IMG_COLOR_STYLED);
ImageSetStyle($img,ImageStyleThicken($style,6));
ImageSetThickness($img,6);
ImagePolygon($img,array(200,100,700,100, 700,600),3,IMG_COLOR_STYLED);

ImageLineStyled($img,1350,750,850,250,$style);
ImageLineStyled($img,850,250,1350,250,$style);
ImageLineStyled($img,1350,250,1350,750,$style);
$thickstyle = ImageStyleThicken($style,2);
ImageLineStyled($img,1400,700,900,200,$thickstyle,2);
ImageLineStyled($img,900,200,1400,200,$thickstyle,2);
ImageLineStyled($img,1400,200,1400,700,$thickstyle,2);
$thickstyle = ImageStyleThicken($style,4);
ImageLineStyled($img,1450,650,950,150,$thickstyle,4);
ImageLineStyled($img,950,150,1450,150,$thickstyle,4);
ImageLineStyled($img,1450,150,1450,650,$thickstyle,4);
$thickstyle = ImageStyleThicken($style,6);
ImageLineStyled($img,1500,600,1000,100,$thickstyle,6);
ImageLineStyled($img,1000,100,1500,100,$thickstyle,6);
ImageLineStyled($img,1500,100,1500,600,$thickstyle,6);

header('Content-type: image/png');
ImagePng($img);
ImageDestroy($img);

/*
ImageLineStyled(<rImage>,<iX1>,<iY1>,<iX2>,<iY2>,<aStyle>[,<iThickness>]) --> <bSuccess>

<rImage> is the image resource (see imagecreatetruecolor()).
<iX1>,<iY1> are the from-coordinates.
<iX2>,<iY2> are the to-coordinates.
<aStyle> is the style array (see imagesetstyle()).
Use ImageStyleThicken() if the style is linear and <iThickness> is greater than 1.
<iThickness> is the line thickness; default 1.

<bSuccess> is true on success or false on failure.
*/
function ImageLineStyled($_1,$_2,$_3,$_4,$_5,$_6,$_7 = 1) {
	static $a;
	imagesetthickness($_1,$_7);
	if ($_6!==$a) imagesetstyle($_1,$a = $_6);
	if ($_3!=$_5 or $_7<=1) return imageline($_1,$_2,$_3,$_4,$_5,IMG_COLOR_STYLED);
	if ($_2>$_4) { $i = $_2; $_2 = $_4; $_4 = $i; }
	$_3 -= $_7>>1;
	$_5 = ($i = $_3)+$_7-1;
	do {
		foreach ($a as $x) {
			if ($x>=0) imagesetpixel($_1,$_2,$_3,$x);
			if (++$_3>$_5) {
				if (++$_2>$_4) return true;
				$_3 = $i; }
			}
		}
	while (true); }

/*
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; }
?>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC