中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

php給圖片加水印的代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

在使用php編程的時(shí)候, 很多時(shí)候需要對(duì)上傳的圖片加水印,來(lái)確定圖片版權(quán)和出處. 但是,一般情況下加水印的位置是圖片的右下角,但是,不同圖片的色階不同,有時(shí)候我們圖片的水印和圖片本身色階相同,就會(huì)造成水印不明顯.下面這段代碼可 以實(shí)現(xiàn)自動(dòng)識(shí)別圖片的色階,更加色階差來(lái)添加圖片的水印,這樣可以避免水印和圖片色階一樣的弊端.

*/
    function add_wm($nmw_water, $src_file, $output_file, $x, $y) {       
        if(file_exists($output_file))       
            return;       

        $w1 = MagickGetImageWidth($nmw_water);       
        $h1 = MagickGetImageHeight($nmw_water);       

        $nmw =NewMagickWand();       
        MagickReadImage($nmw, $src_file);       

        // 默認(rèn)的加水印位置調(diào)整       
        $lt_w = 50;       
        $lt_h = 50;       

        if($x == 0){       
            $w = MagickGetImageWidth($nmw);       
            $h = MagickGetImageHeight($nmw);       

            $x = $w;       
            $y = $h;       
        }else{       
            // 根據(jù)具體情況調(diào)整       
            $lt_w = 30;       
            $lt_h = 40;       
        }       

        MagickCompositeImage($nmw, $nmw_water, MW_OverCompositeOp, $x - $w1 - $lt_w, $y - $h1 - $lt_h);       
        MagickWriteImage($nmw, $output_file);       

        DestroyMagickWand($nmw);               
    }       

    // 還是groovy的eachFileRecurse好用啊       
    function add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr) {       
        $dp = dir($to_dir);       
        while($file=$dp->read()){       
            if($file != '.' && $file != '..'){       

                if(is_dir($to_dir . '/' . $file)){       
                    mkdir($output_dir . '/' . $file);       
                    add_wm_recurse($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $arr);       
                }else{       
                    if(!array_key_exists($to_dir . '/' . $file, $arr)){       
                        continue;       

                    }       

                    $sub_arr = $arr[$to_dir . '/' . $file];       
                    if($sub_arr){       
                        $x = intval($sub_arr[0]);       
                        $y = intval($sub_arr[1]);       
                        add_wm($nmw_water, $to_dir . '/' . $file, $output_dir . '/' . $file, $x, $y);       
                    }       
                }       
            }       
        }       
        $dp->close();       
    }       

    $to_dir = './resized';       
    $output_dir = './output';       

    // 這個(gè)是我用java的ImageIO遍歷圖片像素獲取的符合褲子顏色的區(qū)域的坐標(biāo)array(posX, posY)       
    $arr = array(       
        array(50, 50)       
    );       

    $water = './water.png';       
    $nmw_water =NewMagickWand();       
    MagickReadImage($nmw_water, $water);       

    add_wm_recurse($nmw_water, $to_dir, $output_dir, $arr);       

    DestroyMagickWand($nmw_water);

標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:C#實(shí)現(xiàn)SAFER加密

下一篇:Python實(shí)現(xiàn) 深度優(yōu)先算法生成迷宮