萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php 給圖片加水印

php 給圖片加水印

下面是一個給圖片加水印的函數,我們會看到很多網站都會給圖片加上水印是吧,下面我們就把這個函數給拿出來吧。

 

public function water($filename,$water,$pos=0,$pct=80){
                // 加載水印圖片
                $info = $this->getImageInfo($water);
                if(!empty($info[0])){
                        $water_w = $info[0];
                        $water_h = $info[1];
                        $type = $info['type'];
                        $fun  = 'imagecreatefrom'.$type;
                        $waterimg = $fun($water);
                } else{
                        return false;
                }
                // 加載背景圖片
                $info = $this->getImageInfo($this->img);
                if(!empty($info[0])){
                        $old_w = $info[0];
                        $old_h = $info[1];
                        $type  = $info['type'];
                        $fun   = 'imagecreatefrom'.$type;
                        $oldimg = $fun($this->img);
                } else{
                        return false;
                }
                // 剪切水印
                $water_w >$old_w && $water_w = $old_w;
                $water_h >$old_h && $water_h = $old_h;

                // 水印位置
                switch($pos){
                        case 0://隨機
                    $posX = rand(0,($old_w - $water_w));
                    $posY = rand(0,($old_h - $water_h));
                    break;
                case 1://1為頂端居左
                    $posX = 0;
                    $posY = 0;
                    break;
                case 2://2為頂端居中
                    $posX = ($old_w - $water_w) / 2;
                    $posY = 0;
                    break;
                case 3://3為頂端居右
                    $posX = $old_w - $water_w;
                    $posY = 0;
                    break;
                case 4://4為中部居左
                    $posX = 0;
                    $posY = ($old_h - $water_h) / 2;
                    break;
                case 5://5為中部居中
                    $posX = ($old_w - $water_w) / 2;
                    $posY = ($old_h - $water_h) / 2;
                    break;
                case 6://6為中部居右
                    $posX = $old_w - $water_w;
                    $posY = ($old_h - $water_h) / 2;
                    break;
                case 7://7為底端居左
                    $posX = 0;
                    $posY = $old_h - $water_h;
                    break;
                case 8://8為底端居中
                    $posX = ($old_w - $water_w) / 2;
                    $posY = $old_h - $water_h;
                    break;
                case 9://9為底端居右
                    $posX = $old_w - $water_w;
                    $posY = $old_h - $water_h;
                    break;
                default: //隨機
                    $posX = rand(0,($old_w - $water_w));
                    $posY = rand(0,($old_h - $water_h));
                    break;
                }
            // 設定圖像的混色模式
                imagealphablending($oldimg, true);
                // 添加水印
                imagecopymerge($oldimg, $waterimg, $posX, $posY, 0, 0, $water_w,$water_h,$pct);
                $fun = 'image'.$type;
                !@$fun($oldimg, $filename,100) && die('保存失敗!檢查目錄是否存在並且可寫?');
                  imagedestroy($oldimg);
                  imagedestroy($waterimg);
                  return $filename;
        }

copyright © 萬盛學電腦網 all rights reserved