D3bd7339e1941bc25c05110b69a82721

A quick PHP progressbar class using the gdlib. What can I improve?

<?php

/**
 * progressBar
 * @param int curVal Current value (x in x/y)
 * @param int maxVal Max value (y in x/y) default 100
 * @param int width Width of progressbar in pixels default 500
 * @param int height Height of progressbar in pixels default 20
 */
class progressBar
{
	private $curVal;
	private $maxVal;
	private $width;
	private $height;

	private $backgroundColor = Array(255, 255, 255);
	private $borderColor = Array(0, 0, 0);
	private $progressColor = Array(238, 181, 12);
	private $fontColor = Array(0, 0, 0);

	function __construct($curVal, $maxVal=100, $width=500, $height=20)
	{
		$this->curVal = $curVal;
		$this->maxVal = $maxVal;
		$this->width = $width;
		$this->height = $height;
	}

	public function setBackgroundColor($r, $g, $b)
	{
		$this->backgroundColor[0] = $r;
		$this->backgroundColor[1] = $g;
		$this->backgroundColor[2] = $b;
	}

	public function setBorderColor($r, $g, $b)
	{
		$this->borderColor[0] = $r;
		$this->borderColor[1] = $g;
		$this->borderColor[2] = $b;
	}

	public function setProgressColor($r, $g, $b)
	{
		$this->progressColor[0] = $r;
		$this->progressColor[1] = $g;
		$this->progressColor[2] = $b;
	}

	public function setFontColor($r, $g, $b)
	{
		$this->fontColor[0] = $r;
		$this->fontColor[1] = $g;
		$this->fontColor[2] = $b;
	}

	public function draw()
	{
		// Set content-type
		header('Content-type: image/png');

		// Create image
		$image = @imagecreate($this->width, $this->height);

		// Define colors
		$backgroundColor = imagecolorallocate($image, $this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]);
		$borderColor = imagecolorallocate($image, $this->borderColor[0], $this->borderColor[1], $this->borderColor[2]);
		$progressColor = imagecolorallocate($image, $this->progressColor[0], $this->progressColor[1], $this->progressColor[2]);
		$fontColor = imagecolorallocate($image, $this->fontColor[0], $this->fontColor[1], $this->fontColor[2]);

		// Calculate progress width and percent
		$progressPercent = round($this->curVal/$this->maxVal*100, 1);
		$progressWidth = round($this->curVal*$this->width/$this->maxVal, 0);
		
		// Draw progressbar
		imagefilledrectangle($image, 0, 0, $progressWidth, $this->height-1, $progressColor);
		imagerectangle($image, 0, 0, $this->width-1, $this->height-1, $borderColor);

		// Draw text
		$textPosX = 5;
		$textPosY = $this->height/2-7;
		imagestring($image, 3, $textPosX, $textPosY, $progressPercent."%", $fontColor);

		// Show image
		imagepng($image);
	}

}

?>

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel