PHP画像の透かし処理

ウォーターマークの生成は、テクノロジー全体の中で最も簡単なステップです。透かしの位置を特定するには、最初は浅い幾何学的知識が少し必要です。

前の章では、画像のトリミング技術を学びました。ウォーターマークは、画像トリミング技術を少し変形したものにすぎません。

重要な幾何学的知識:

1. 画像のサイズ

2. 画像が配置される座標

3. 画像の幅と高さ

画像の透かし技術の中核は 2 枚の画像に相当します: 1 つの大きな画像 ; 1 つの小さな画像。小さい画像を大きい画像内のどこかに配置します。

ウォーターマーク技術は、その中で最も単純な技術です。実装方法は次のとおりです:

1. 元の画像 (操作のターゲット画像とも呼ばれます) を開きます

2. ウォーターマーク画像 (ウォーターマークのソース画像とも呼ばれます) を開きます。 3. imagecopymerge を使用します 小さい画像を大きい画像の指定された位置にマージします

4. 画像を出力します

.5 リソースを破棄します

1. シンプルな画像透かし 必要なターゲット画像透かしが入っている (コンピューターの d に保存されていると仮定します:/www/img/meinv.jpg)、画像は次のとおりです:

99.png 追加する必要があるロゴ画像 (d に保存されていると仮定します)。私のコンピューターでは /www/img/logo.png)、画像は次のとおりです:

document_2015-09-22_56010df4559d3.png最も重要なことは、この関数を使用することです:

bool imagecopymerge (リソース $target image, resource $source image, int $target starting x, int $target starting y, int $source x, int $source y, int $source width, int $source height, int $transparency)

注:

透明度の値は、次の整数です。 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 2番目に、スマートな画像透かし機能を作成します

まず、自動画像オープニングを作成できます。 function 関数

私たちは皆、これまでに画像を作成したり、画像を開く関数を学習したことがあります:

1.imagecreate

2.imagecreatetruecolor

3.imagecreatefromjpeg など

理由を考えてみましょう。画像の MIME タイプを取得する方法が見つかったら、MIME タイプに基づいてファイルを開く関数を見つけるだけです。

したがって、このステップは 2 つの部分に分かれています:

1. ファイルの MIME タイプと戻り値のタイプを取得します。

2. パスを渡し、関数を開いてリソースを返します。

したがって、上記の 2 つのブロックを 2 つの関数にできます。

画像のパスを渡し、画像の幅、高さ、MIME タイプを配列に返します。必要に応じて、対応するパラメーターを使用します。

$data の型連想配列に MIME タイプを渡すことができます。コードは次のとおりです。

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

ファイルを開き、画像のタイプを渡し、画像のパスを渡す関数。画像が開かれ、リソースのタイプが返されます。

以下の例では、switch...caseで$typeを判定し、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;
}

位置を自動的に計算します:

位置を 0 ~ 9 の範囲の 10 個の値に分割できます。

位置を表すために描画を使用します:

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




?>

この記事は技術者のみが学びを交換し、技術を交換することができます。

この記事で使用されている画像:

ファン・ビンビンさんのイメージ写真は商用目的ではありません。すべての所有権はファン・ビンビン氏および関連機関に帰属します。

この記事で使用されているロゴはBaiduに属します。

ここに宣言します!


学び続ける
||
<?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 学習者の迅速な成長を支援します!