RoundCorner.class.php 4.4 KB
<?php
class Util_Image_RoundCorner
{
    private $im;
    private $imageInfo;
    private $radius;
    private $rColor;
    private $bColor;
    private $gColor;
    
    private $imageTypes = array(
                    1 => 'GIF',2 => 'JPG',3 => 'PNG',4 => 'SWF',
                    5 => 'PSD',6 => 'BMP',7 => 'TIFF(intel byte order)',
                    8 => 'TIFF(motorola byte order)',9 => 'JPC',10 => 'JP2',
                    11 => 'JPX',12 => 'JB2',13 => 'SWC',14 => 'IFF',15 => 'WBMP',
                 16 => 'XBM'
                 );
    /**
     * 初始化
     * 
     * @param sring $image
     * @param int $radius
     * @param int $rColor
     * @param int $bColor
     * @param int $gColor
     * 
     */
    public function __construct($image, $radius, $rColor=255, $bColor=0, $gColor=0)
    {
        if(file_exists($image))
        {
            $imageInfo = getimagesize($image);
            $this->imageInfo = array
            (
                'file' => str_replace('\\', '/', realpath($image)),
                'width' => $imageInfo[0],
                'height' => $imageInfo[1],
                'type' => $this->imageTypes[$imageInfo[2]],
                'mime' => $imageInfo['mime']
            );
            //获取原图
            $this->im = $this->getLoadImage($image, $this->imageInfo['type']);
            $this->radius = $radius;
            $this->rColor = $rColor;
            $this->bColor = $bColor;
            $this->gColor = $bColor;
        }
        else
        {
            exit();
        }
    }
    
    /**
     * 加载图片
     * 
     * @param string $image
     * @param string $type
     * @return gd
     */
    private function getLoadImage($image, $type)
    {
        if (empty($type)) 
        {
            return false;
        }
        switch(strtolower($type)) 
        {
            case 'jpg':
                $img = @imagecreatefromjpeg($image);
                break;
            case 'gif':
                $img = @imagecreatefromgif($image);
                break;
            case 'png':
                $img = @imagecreatefrompng($image);
                break;
            default:
            return false;
        }
        return $img;
    }
    
    /**
     * 获取圆角图片
     */
    private function getCornerImage()
    {
        //画布
        $cornerImage = imagecreatetruecolor($this->radius, $this->radius);
        //背景
        $bgColor = imagecolorallocate($cornerImage, $this->rColor, $this->gColor, $this->bColor);
        //前景
        $fgColor = imagecolorallocate($cornerImage, 0, 0, 0);
        imagefill($cornerImage, 0,0 ,$bgColor);
        //四分之一圆
        imagefilledarc($cornerImage, $this->radius, $this->radius, $this->radius*2, $this->radius*2, 180, 270, $fgColor, IMG_ARC_PIE);
        imagecolortransparent($cornerImage, $fgColor);
        return $cornerImage;
    }
    
    /**
     * 圆角
     */
    public function rounded()
    {
        $cornerImage = $this->getCornerImage();
        $rounderImage = imagecreatetruecolor($this->imageInfo['width'], $this->imageInfo['height']);
        $fgColor = imagecolorallocate($rounderImage, $this->rColor, $this->gColor, $this->bColor);
        imagefill($rounderImage, 0, 0, $fgColor);
        imagecopymerge($rounderImage, $this->im, 0, 0,0,0,$this->imageInfo['width'], $this->imageInfo['height'], 100 );
        imagecopymerge($rounderImage, $cornerImage,0,0,0,0, $this->radius,$this->radius,100);
        //左底
        $lbRounderImage = imagerotate($cornerImage, 90, $fgColor);
        imagecopymerge($rounderImage, $lbRounderImage, 0, $this->imageInfo['height']-$this->radius, 0, 0,$this->radius,$this->radius, 100);
        //右底
        $rbCornerImage = imagerotate($cornerImage, 180, $fgColor);
        imagecopymerge($rounderImage, $rbCornerImage, $this->imageInfo['width']-$this->radius, $this->imageInfo['height']-$this->radius, 0, 0, $this->radius, $this->radius, 100);
        //左上
        $ltCornerImage = imagerotate($cornerImage, 270, $fgColor);
        imagecopymerge($rounderImage, $ltCornerImage, $this->imageInfo['width']-$this->radius, 0, 0, 0, $this->radius, $this->radius,100);
        imagecolortransparent($rounderImage, $fgColor);
        //新图片
        $ext = substr($this->imageInfo['file'], strrpos($this->imageInfo['file'], '.'));
        $rounderName = str_replace($ext, '', $this->imageInfo['file']).'_round.png';
        imagepng($rounderImage, $rounderName);
    }

}