[练手]俩功能的图片处理类

class Image{

        protected $ori;//要处理的图
        protected $width = 200;//缩略图宽
        protected $height = 200;//缩略图高
        protected $logo = 'D:\www\dl\upload\2014\1008\thumb\thumb_4j9c.jpg';//水印路径
        protected $position = 3;//默认水印位置
        protected $fac = 30;//默认的透明度
        //函数地图
        public $fucmap = array(
                1=>'imagecreatefromgif',
                2=>'imagecreatefromjpeg',
                3=>'imagecreatefrompng' ,
                6=>'imagecreatefromwbmp',
            );

        //构造的时候需要传一个原图的路径
        public function __construct($ori){
                $this->ori = $ori;

        } 



        /*缩略图*/
       public  function makeThumb(){
                list($ow,$oh,$otype) = getimagesize($this->ori);
                //原图
                $big = $this->fucmap[$otype]($this->ori);
                //小图
                $smal = imagecreatetruecolor($this->width, $this->height);
                $white = imagecolorallocate($smal,255,255,255); 
                imagefill($smal, 0, 0,$white);
                //计算比例
                $bl = min($this->width/$ow,$this->height/$oh);
                $hw = $ow*$bl;
                $hh = $oh*$bl;
                //比例拷贝
                imagecopyresampled($smal, $big, ($this->width - $hw)/2, ($this->height-$hh)/2, 0, 0, $hw, $hh , $ow, $oh);
                $path = dirname($this->ori).'/thumb/thumb_'.basename($this->ori);
                imagepng($smal,$path);
                return $path;

       }

        /*水印没有存到硬盘上直接打印的.....*/
       public  function waterMark($position){
            $this ->position = $position;
              if($this->position>4){
                echo '你的位置不大对呀!';
                    return false;

                }else{

            list($ow,$oh,$otype)=getimagesize($this->ori);
            list($ww,$wh,$wtype)=getimagesize($this->logo);

            $big = $this->fucmap[$otype]($this->ori);
            $smal =$this->fucmap[$wtype]($this->logo);

        switch ($this->position) {
            case 0:
                imagecopymerge($big, $smal, 0, 0, 0, 0, $ww, $wh,$this->fac);
                break;
            case 1:
                imagecopymerge($big, $smal, ($ow-$ww), 0, 0, 0, $ww, $wh,$this->fac);
                break;
            case 2:
                imagecopymerge($big, $smal, 0, ($oh-$wh), 0, 0, $ww, $wh,$this->fac);
                break;
            case 3:
                imagecopymerge($big, $smal, ($ow-$ww), ($oh-$wh), 0, 0, $ww, $wh,$this->fac);
                break;
            case 4:
                imagecopymerge($big, $smal, ($ow-$ww)/2, ($oh-$wh)/2, 0, 0, $ww, $wh,$this->fac);
                break;
        }


          header('content-type:image/png');
            imagepng($big);

                    

     }



       }



}


/*$img = new Image('D:\www\dl\upload\2014\1008\4j9c.jpg');
echo $img->waterMark(5);
*/

 

posted @ 2014-10-08 16:36  不能没有Ni  阅读(82)  评论(0)    收藏  举报