php圖片水印處理

產生浮水印是整個技術裡面最簡單的一步。定位水印位置的時候涉及一點點、最初淺的幾何知識。

而上一章我們學習了圖片的裁切技術。水印只不過是圖片裁剪技術的一點點的小變形的體現。

一點點幾何上的重點知識:

    1.圖片大小

    2.圖片放在哪個座標上

#    3.圖片的寬高

圖片浮水印技術的核心相當於是兩張圖片:一張大圖;一張小圖。將小圖放置在大圖的某個位置。

水印技術是這個裡面最簡單的技術,實現方式:

    1.打開原圖(也叫操作的目標圖片)

    2.打開水印圖(也稱為水印來源圖片)

    3.使用imagecopymerge 將小圖合併至大圖的指定位置

    4.輸出圖片

    

一、簡單圖片浮水印

需要加浮水印的目標圖片(假設儲存在我電腦的d:/www/img/meinv.jpg) ,圖片如下:

99.png

需要加上的logo圖片(假設儲存在我電腦的d:/www/img/logo.png),圖片如下:

document_2015-09-22_56010df4559d3.png

最主要的是使用這個函數:

bool imagecopymerge ( resource $目標圖片, resource $來源圖片, int $目標開始的x , int $目標開始的y, int $來源的x , int $來源的y , int $來源的寬, int $來源的高, int $透明度)

注意:

透明度的值為0-100的整數。 imagecopy和imagecopymerge的差別在於一個有透明度,一個沒有透明度。

依照總結的步驟,做一個簡單的方法:

<?php
//打开目标图片
$dst = imagecreatefrompng('/upload/course/000/000/002/5833ebba648cf229.png');

//打开Logo来源图片
$src = imagecreatefrompng('/upload/course/000/000/002/5833ebe90cc11285.png');

//得到目标图片的宽高
$dst_info = getimagesize('5833ebba648cf229.png');

//得到logo图片的宽高
$src_info = getimagesize('5833ebe90cc11285.png');

//放到最右下脚可得出图片水印图片需要开始的位置即:
//x点位置:需要大图的宽 - 小图的宽;
//y点位置:放大图的高 - 小图的高

$dst_x = $dst_info[0] - $src_info[0];

$dst_y = $dst_info[1] - $src_info[1];

//要将图片加在右下脚
imagecopymerge($dst, $src, $dst_x, $dst_y, 0, 0, $src_info[0], $src_info[1], 100);

header('Content-type:image/png');
imagepng($dst);

imagedestroy($dst);

imagedestroy($src);

?>

我們看看最終的效果如下: 

11.png

二、做一個智慧的圖片水印函數

一、 我們可以做一個自動化打開圖片的函數

之前創建圖片或打開圖片的函數我們都學過:

1. imagecreate

2.imagecreatetruecolor

3.imagecreatefromjpeg等

我們來推理下下。我們如果能夠想辦法得到圖片的MIME類型,根據MIME類型找到開啟該檔案的函數就行了。

因此,做這一步分成兩塊來完成:

#1.得到檔案MIME類型,回傳類型。

2.傳入路徑,開啟函數,傳回資源。

因此,上面兩塊,我們都可以做成兩個函數。

傳入圖片的路徑,將圖片的寬、高、圖片的MIME類型全部傳回一個數組,需要的時候使用對應的參數即可。

我們可以將mime型別傳到$data當中的type關聯數組。程式碼如下:

function getImageInfo($path) {
    $info = getimagesize($path);
    $data['width'] = $info[0];
    $data['height'] = $info[1];
    $data['type'] = $info['mime'];
    return $data;
}

開啟檔案的函數,傳入一個圖片的類型,傳入一個圖片的路徑就開啟了圖片,回傳成了資源類型。

下面的範例中,$type使用swithc...case來判斷,如果是imagejpeg就使用imagecreatefromjpeg來開啟$path中路徑指定的檔案。最後,傳回一個資源類型。

function openImg($path, $type) {
    switch ($type) {
        case 'image/jpeg':
        case 'image/jpg':
        case 'image/pjpeg':
            $img = imagecreatefromjpeg($path);
            break;
        case 'image/png':
        case 'image/x-png':
            $img = imagecreatefrompng($path);
            break;
        case 'image/gif':
            $img = imagecreatefromgif($path);
            break;
        case 'image/wbmp':
            $img = imagecreatefromwbmp($path);
            break;
        default:
            exit('图片类型不支持');
    }
    return $img;
}

自動計算位置:

我們可將位置分為10個值,分別為0-9。

我們用畫圖來表示位置:

document_2015-09-22_5600ef919671d.png


#註:
0為隨機位置,可出現在頁面中的任意處。但是不能超過圖片的範圍。

0的位置為:

x = 0 至 (大图宽 - 小图宽)
y = 0 至  (大图高 - 小图高)

1的位置為:

x = 0 
y = 0

2的位置為:

x = (大图宽 - 小图宽) /2 
y = 0

3的位置為:

x = 大图宽 - 小图宽
y = 0

4的位置為:

x = 0
y = (大图高 - 小图高) / 2

... ....依此類推。

我們來推理0-9的實作程式碼:

 switch($pos){
        case 1:
            $x=0;
            $y=0;
            break;
        case 2:
            $x=ceil(($info['width']-$logo['width'])/2);
            $y=0;
            break;
        case 3:
            $x=$info['width']-$logo['width'];
            $y=0;
            break;
        case 4:
            $x=0;
            $y=ceil(($info['height']-$logo['height'])/2);
            break;
        case 5:
            $x=ceil(($info['width']-$logo['width'])/2);
            $y=ceil(($info['height']-$logo['height'])/2);
            break;
        case 6:
            $x=$info['width']-$logo['width'];
            $y=ceil(($info['height']-$logo['height'])/2);
            break;
        case 7:
            $x=0;
            $y=$info['height']-$logo['height'];
            break;
        case 8:
            $x=ceil(($info['width']-$logo['width'])/2);
            $y=$info['height']-$logo['height'];
            break;
        case 9:
            $x=$info['width']-$logo['width'];
            $y=$info['height']-$logo['height'];
            break;
        case 0:
        default:
            $x=mt_rand(0,$info['width']-$logo['width']);
            $y=mt_rand(0,$y=$info['height']-$logo['height']);
            break;
    }

最後呼叫一下圖片的合併、輸出和銷毀程式碼即可:

imagecopymerge($dst,$src,$x,$y,0,0,$logo['width'],$logo['height'],$tm);

我們將最終的程式碼整合好後給大家實驗看效果:

<?php

water('/upload/course/000/000/002/5833ebba648cf229.png','/upload/course/000/000/002/5833ebe90cc11285.png',0,50);

function water($img,$water,$pos=9,$tm=100){

   $info=getImageInfo($img);

   $logo=getImageInfo($water);

   $dst=openImg($img,$info['type']);
   $src=openImg($water,$logo['type']);


   switch($pos){
       case 1:
           $x=0;
           $y=0;
           break;
       case 2:
           $x=ceil(($info['width']-$logo['width'])/2);
           $y=0;
           break;
       case 3:
           $x=$info['width']-$logo['width'];
           $y=0;
           break;
       case 4:
           $x=0;
           $y=ceil(($info['height']-$logo['height'])/2);
           break;
       case 5:
           $x=ceil(($info['width']-$logo['width'])/2);
           $y=ceil(($info['height']-$logo['height'])/2);
           break;
       case 6:
           $x=$info['width']-$logo['width'];
           $y=ceil(($info['height']-$logo['height'])/2);
           break;

       case 7:
           $x=0;
           $y=$info['height']-$logo['height'];
           break;
       case 8:
           $x=ceil(($info['width']-$logo['width'])/2);
           $y=$info['height']-$logo['height'];
           break;
       case 9:
           $x=$info['width']-$logo['width'];
           $y=$info['height']-$logo['height'];
           break;
       case 0:
       default:
           $x=mt_rand(0,$info['width']-$logo['width']);
           $y=mt_rand(0,$y=$info['height']-$logo['height']);
           break;

   }
   imagecopymerge($dst,$src,$x,$y,0,0,$logo['width'],$logo['height'],$tm);


   imagejpeg($dst);

   imagedestory($dst);
   imagedestory($src);

}



   function openImg($path,$type){
       switch($type){
           case 'image/jpeg':
           case 'image/jpg':
           case 'image/pjpeg':
               $img=imagecreatefromjpeg($path);
               break;
           case 'image/png':
           case 'image/x-png':
               $img=imagecreatefrompng($path);
               break;
           case 'image/gif':
               $img=imagecreatefromgif($path);
               break;
           case 'image/wbmp':
               $img=imagecreatefromwbmp($path);
               break;
           default:
               exit('图片类型不支持');


       }
       return $img;
   }




?>

本文僅為技術人員交流學習、交流技術使用。

本文所使用的圖像:

範冰冰女士的形象照片不可用於商業使用。所有權均為范冰冰女士及相關機構所有。

本文所使用到的logo歸百度公司所有。

特此聲明!


繼續學習
||
<?php water('zxy.jpg','logo.gif',0,50); function water($img,$water,$pos=9,$tm=100){ $info=getImageInfo($img); $logo=getImageInfo($water); $dst=openImg($img,$info['type']); $src=openImg($water,$logo['type']); switch($pos){ case 1: $x=0; $y=0; break; case 2: $x=ceil(($info['width']-$logo['width'])/2); $y=0; break; case 3: $x=$info['width']-$logo['width']; $y=0; break; case 4: $x=0; $y=ceil(($info['height']-$logo['height'])/2); break; case 5: $x=ceil(($info['width']-$logo['width'])/2); $y=ceil(($info['height']-$logo['height'])/2); break; case 6: $x=$info['width']-$logo['width']; $y=ceil(($info['height']-$logo['height'])/2); break; case 7: $x=0; $y=$info['height']-$logo['height']; break; case 8: $x=ceil(($info['width']-$logo['width'])/2); $y=$info['height']-$logo['height']; break; case 9: $x=$info['width']-$logo['width']; $y=$info['height']-$logo['height']; break; case 0: default: $x=mt_rand(0,$info['width']-$logo['width']); $y=mt_rand(0,$y=$info['height']-$logo['height']); break; } imagecopymerge($dst,$src,$x,$y,0,0,$logo['width'],$logo['height'],$tm); imagejpeg($dst); imagedestory($dst); imagedestory($src); } function openImg($path,$type){ switch($type){ case 'image/jpeg': case 'image/jpg': case 'image/pjpeg': $img=imagecreatefromjpeg($path); break; case 'image/png': case 'image/x-png': $img=imagecreatefrompng($path); break; case 'image/gif': $img=imagecreatefromgif($path); break; case 'image/wbmp': $img=imagecreatefromwbmp($path); break; default: exit('图片类型不支持'); } return $img; } ?>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!