php 給圖片加浮水印

來源:互聯網
上載者:User

 

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;
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.