天天看點

PHP圖檔添加水印

/*
     * 功能:PHP圖檔水印 (水印支援圖檔) 
     * @param   str  $groundImage    背景圖檔,即需要加水印的圖檔,暫隻支援GIF,JPG,PNG格式; 
     * @param  str    $waterPos        水印位置,有10種狀态,0為随機位置; 
     *                        1為頂端居左,2為頂端居中,3為頂端居右; 
     *                        4為中部居左,5為中部居中,6為中部居右; 
     *                        7為底端居左,8為底端居中,9為底端居右; 
     * @param str     $waterImage        圖檔水印,即作為水印的圖檔,暫隻支援GIF,JPG,PNG格式;
     * return str 加好水印圖檔路徑
     */

    public static function imageWaterMark($groundImage, $waterPos = 0, $waterImage = "") {
        $isWaterImage = FALSE;
        $formatMsg = "暫不支援該檔案格式,請用圖檔處理軟體将圖檔轉換為GIF、JPG、PNG格式。";

        //讀取水印檔案 
        if (!empty($waterImage) && file_exists($waterImage)) {
            $isWaterImage = TRUE;
            $water_info = getimagesize($waterImage);
            $water_w = $water_info[0]; //取得水印圖檔的寬 
            $water_h = $water_info[1]; //取得水印圖檔的高 

            switch ($water_info[2]) {//取得水印圖檔的格式 
                case 1:$water_im = imagecreatefromgif($waterImage);
                    break;
                case 2:$water_im = imagecreatefromjpeg($waterImage);
                    break;
                case 3:$water_im = imagecreatefrompng($waterImage);
                    break;
                default:
                    Loger::log(array(
                        'msg' => $formatMsg,
                        'file' => __CLASS__ . ':' . __LINE__,
                            ), CLogger::LEVEL_ERROR);
                    return false;
            }
        }

        //讀取背景圖檔 
        if (!empty($groundImage) && file_exists($groundImage)) {
            $ground_info = getimagesize($groundImage);
            $ground_w = $ground_info[0]; //取得背景圖檔的寬 
            $ground_h = $ground_info[1]; //取得背景圖檔的高 

            switch ($ground_info[2]) {//取得背景圖檔的格式 
                case 1:$ground_im = imagecreatefromgif($groundImage);
                    break;
                case 2:$ground_im = imagecreatefromjpeg($groundImage);
                    break;
                case 3:$ground_im = imagecreatefrompng($groundImage);
                    break;
                default:echo $formatMsg;
                    return false;
            }
        } else {
            Loger::log(array(
                'msg' => "需要加水印的圖檔不存在!",
                'file' => __CLASS__ . ':' . __LINE__,
                    ), CLogger::LEVEL_ERROR);
            return false;
        }

        //水印位置 
        if ($isWaterImage) {//圖檔水印 
            $w = $water_w;
            $h = $water_h;
            $label = "圖檔的";
        }
        if (($ground_w < $w) || ($ground_h < $h)) {
            Loger::log(array(
                'msg' => "需要加水印的圖檔的長度或寬度比水印" . $label . "還小,無法生成水印!",
                'file' => __CLASS__ . ':' . __LINE__,
                    ), CLogger::LEVEL_ERROR);
            return;
        }
        switch ($waterPos) {
            case 0://随機 
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
            case 1://1為頂端居左 
                $posX = 0;
                $posY = 0;
                break;
            case 2://2為頂端居中 
                $posX = ($ground_w - $w) / 2;
                $posY = 0;
                break;
            case 3://3為頂端居右 
                $posX = $ground_w - $w;
                $posY = 0;
                break;
            case 4://4為中部居左 
                $posX = 0;
                $posY = ($ground_h - $h) / 2;
                break;
            case 5://5為中部居中 
                $posX = ($ground_w - $w) / 2;
                $posY = ($ground_h - $h) / 2;
                break;
            case 6://6為中部居右 
                $posX = $ground_w - $w;
                $posY = ($ground_h - $h) / 2;
                break;
            case 7://7為底端居左 
                $posX = 0;
                $posY = $ground_h - $h;
                break;
            case 8://8為底端居中 
                $posX = ($ground_w - $w) / 2;
                $posY = $ground_h - $h;
                break;
            case 9://9為底端居右 
                $posX = $ground_w - $w - 10;
                $posY = $ground_h - $h - 5;
                break;
            default://随機 
                $posX = rand(0, ($ground_w - $w));
                $posY = rand(0, ($ground_h - $h));
                break;
        }

        //設定圖像的混色模式 
        imagealphablending($ground_im, true);

        if ($isWaterImage) {//圖檔水印 
            imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w, $water_h); //拷貝水印到目标檔案         
        }
        //生成水印後的圖檔 
        $arrInfo = pathinfo($groundImage);
        $strNewImage = $arrInfo['dirname'] . '/' . $arrInfo['filename'] . '_shuiyin' . '.' . $arrInfo['extension'];
        //@unlink($groundImage); 
        switch ($ground_info[2]) {//取得背景圖檔的格式 
            case 1:imagegif($ground_im, $strNewImage);
                break;
            case 2:imagejpeg($ground_im, $strNewImage, 100);
                break;
            case 3:imagepng($ground_im, $strNewImage);
                break;
            default:return false;
        }

        //釋放記憶體 
        if (isset($water_info)) {
            unset($water_info);
        }
        if (isset($water_im)) {
            imagedestroy($water_im);
        }
        unset($ground_info);
        imagedestroy($ground_im);
        return $strNewImage;
    }