福州網(wǎng)站建設(shè)>網(wǎng)站新聞>php技術(shù)

        php給圖片添加文字或圖片水印實現(xiàn)代碼

        發(fā)布日期:2018-03-14瀏覽次數(shù):1150 來源:福州網(wǎng)站建設(shè)

        有時上傳圖片時,需要給圖片添加水印,水印一般為文字或圖片logo水印,下面就來看看兩種添加方法。

        一、文字水印

        文字水印就是在圖片上加上文字,主要使用gd庫的imagefttext方法,并且需要字體文件。效果圖如下:

        $dst_path = 'dst.jpg';
        //創(chuàng)建圖片的實例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        
        //打上文字
        $font = './simsun.ttc';//字體路徑
        
        $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字體顏色
        imagefttext($dst, 13, 0, 20, 20, $black, $font, '快樂編程');
        //輸出圖片
        list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
        switch ($dst_type) {
            case 1://GIF
                header('Content-Type: image/gif');
                imagegif($dst);
                break;
            case 2://JPG
                header('Content-Type: image/jpeg');
                imagejpeg($dst);
                break;
            case 3://PNG
                header('Content-Type: image/png');
                imagepng($dst);
                break;
            default:
                break;
        }
        imagedestroy($dst);

        新創(chuàng)建一張圖片,然后打印文字水印:

        // imagecreatefromstring
        // imageCreateFromPng  Create a new image from file or URL   創(chuàng)建圖片對象
        
        // Create a 300x100 image,新創(chuàng)建一張圖片
        $im = imagecreatetruecolor(500, 300);
        
        // set color
        $red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
        $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
        
        // Make the background red
        // function imagefilledrectangle ($image, $x1, $y1, $x2, $y2, $color) {}
        imagefilledrectangle($im, 0, 0, 300, 100, $red);
        
        // Path to our ttf font file
        $font_file = './font/Arial.ttf';
        
        
        // imagefttext ($image, $size, $angle, $x, $y, $color, $fontfile, $text, $extrainfo = null )
        // Draw the text 'PHP Manual' using font size 13
        imagefttext($im, 13, 0, 150, 50, $black, $font_file, 'PHP Manual');
        
        // Output image to the browser
        header('Content-Type: image/png');
        
        imagepng($im);
        imagedestroy($im);
        

        二、圖片水印

        圖片水印就是將一張圖片加在另外一張圖片上,主要使用gd庫的imagecopy和imagecopymerge。

        $dst_path = 'myimage.jpg';
        $src_path = 'http://www.logodashi.com/FileUpLoad/inspiration/636003768803214440.jpg';
        //創(chuàng)建圖片的實例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($src_path));
        //獲取水印圖片的寬高
        list($src_w, $src_h) = getimagesize($src_path);
        //將水印圖片復(fù)制到目標圖片上,最后個參數(shù)50是設(shè)置透明度,這里實現(xiàn)半透明效果
        imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 30);
        
        //如果水印圖片本身帶透明色,則使用imagecopy方法
        // imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
        
        //輸出圖片
        list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
        switch ($dst_type) {
            case 1://GIF
                header('Content-Type: image/gif');
                imagegif($dst);
                break;
            case 2://JPG
                header('Content-Type: image/jpeg');
                imagejpeg($dst);
                break;
            case 3://PNG
                header('Content-Type: image/png');
                imagepng($dst);
                break;
            default:
                break;
        }
        imagedestroy($dst);
        imagedestroy($src);
        

        效果圖:

        三、其他有關(guān)圖像處理的函數(shù)

         
        /*
         *返回圖像的大小及圖像類型
        // Get the size of an image
        $size = getimagesize("http://image18-c.poco.cn/mypoco/myphoto/20160901/20/17857099520160901203311082.jpg?750x956_120");
        
        print_r($size);
        
        
        // 打印結(jié)果
        Array
        (
            [0] => 750
            [1] => 956
            [2] => 2
            [3] => width="750" height="956"
            [bits] => 8
            [channels] => 3
            [mime] => image/jpeg
        )
        
        */
        
        
        /**
         * imagecopy — 拷貝圖像的一部分
         */
        
        // bool imagecopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
        // 將 src_im 圖像中坐標從 src_x,src_y 開始,寬度為 src_w,高度為 src_h 的一部分拷貝到 dst_im 圖像中坐標為 dst_x 和 dst_y 的位置上。
        
        
        
        /*
         *
         * http://php.net/manual/zh/function.imagecopymerge.php
         *imagecopymerge — 拷貝并合并圖像的一部分
         *bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
         *
         *
         */

        以上是由福州網(wǎng)站建設(shè)的小編為你分享了"php給圖片添加文字或圖片水印實現(xiàn)代碼"文章,如果你在這方面有什么問題,隨時聯(lián)系我們

        php技術(shù)有關(guān)的文章
        如果您有什么問題,歡迎咨詢我們客服! 點擊QQ咨詢