<?php
class CRoundCorner{
const TRANSPARENT = 127;
const CORNER_TL = 0;
const CORNER_TR = 1;
const CORNER_BL = 2;
const CORNER_BR = 3;
private $color;
private $colorOpacity;
private $background;
private $bgOpacity;
private $width;
private $height;
private $image;
private $corner;
/*constructor
* @param int $corner: the corner you want to generate (use class constant to indicate)
* @param string $color: the color of the corner (format: RRGGBB)
* @param string $background: the background color of the corner (format: RRGGBB or class constant TRANSPARENT)
* @param int $radius: the radius of the corner
* @param int $width: the width of the corner
* @param int $height: the height of the corner
* Note: If $radius is set, $width and $height can be omitted
*/
public function __construct($corner = self::CORNER_TL, $color = "FFFFFF", $background = self::TRANSPARENT, $radius = 0, $width = 0, $height = 0){
$this->setCorner($corner);
$this->color = $color;
$this->background = $background;
$this->colorOpacity = 0;
$this->bgOpacity = ($background == self::TRANSPARENT ? 127 : 0);
$this->width = $width;
$this->height = $height;
if ($radius){
$this->width = $radius;
$this->height = $radius;
}
}
private function hex2dec($str){
$str = strtoupper($str);
$search = array('A','B','C','D','E','F');
$replace = array('10','11','12','13','14','15');
$num = 0;
$strLen = strlen($str);
for ($i = ($strLen - 1); $i >=0; $i--){
$n = str_replace($search, $replace, $str[$i]);
$num += $n * pow(16,$strLen - $i - 1);
}
return $num;
}
/*setDimension: set the dimension
* @param int $radius: the radius of the corner (if $height is passed also, this will be the width of the corner)
* @param int $height: the height of the corner (can be omitted)
*/
public function setDimension($radius, $height = 0){
$this->width = $radius;
if ($height){
$this->height = $height;
}else{
$this->height = $radius;
}
}
/*setCorner: set the corner to be generated
* @param int $corner: set the corner you want to generate (use class constants)
*/
public function setCorner($corner = self::CORNER_TL){
$this->corner = CRoundCorner::CORNER_TL;
if ($corner == CRoundCorner::CORNER_TL)
$this->corner = CRoundCorner::CORNER_TL;
if ($corner == CRoundCorner::CORNER_TR)
$this->corner = CRoundCorner::CORNER_TR;
if ($corner == CRoundCorner::CORNER_BL)
$this->corner = CRoundCorner::CORNER_BL;
if ($corner == CRoundCorner::CORNER_BR)
$this->corner = CRoundCorner::CORNER_BR;
}
/*setColorOpacity: set the foreground(the corner) color opacity
* @param int $opacity: the opacity of the corner (0 - 127)
*/
public function setColorOpacity($opacity){
if ($opacity)
$this->colorOpacity = (int) $opacity;
}
/*setBgOpacity: set the background color opacity
* @param int $opacity: the opacity of the background (0 - 127)
*/
public function setBgOpacity($opacity){
if ($opacity)
$this->bgOpacity = (int) $opacity;
}
/*draw: draw the corner
* @return resource: the image resource generated
*/
public function draw(){
$this->image = imagecreate($this->width, $this->height);
$rgb = str_split($this->background, 2);
$bgColor = imagecolorallocatealpha($this->image, (int) $this->hex2dec($rgb[0]), (int) $this->hex2dec($rgb[1]), (int) $this->hex2dec($rgb[2]), $this->bgOpacity);
imagefill($this->image, 0,0, $bgColor);
$rgb = str_split($this->color, 2);
$color = imagecolorallocatealpha($this->image, $this->hex2dec($rgb[0]), $this->hex2dec($rgb[1]), $this->hex2dec($rgb[2]), $this->colorOpacity);
$centerX = ($this->corner == CRoundCorner::CORNER_TR || $this->corner == CRoundCorner::CORNER_BR) ? 0 : $this->width;
$centerY = ($this->corner == CRoundCorner::CORNER_BL || $this->corner == CRoundCorner::CORNER_BR) ? 0 : $this->height;
imagefilledellipse($this->image, $centerX, $centerY, $this->width * 2, $this->height * 2, $color);
return $this->image;
}
/*drawFlush: draw the corner and flush it to the browser
*/
public function drawFlush(){
$image = $this->draw();
header('Content-type: image/png');
imagepng($image);
}
/*drawFile: draw the corner to a file
* @param string $path: the path of the file to store the corner
*/
public function drawFile($path){
$image = $this->draw();
imagepng($image, $path);
}
}
?>