PHP image water...LOGIN

PHP image watermark processing

Generating watermark is the simplest step in the entire technology. Locating the watermark location involves a little, initially shallow geometric knowledge.

In the previous chapter, we learned the image cropping technology. Watermark is just a small deformation of image cropping technology.

A little bit of key geometric knowledge:

1. Picture size

2. Which coordinates the picture is placed on

3. The width and height of the picture

The core of image watermarking technology is equivalent to two pictures: one big picture; one small picture. Place the small image somewhere within the larger image.

Watermark technology is the simplest technology among them. Implementation method:

1. Open the original image (also called the target image of the operation)

2. Open the watermark image (Also called watermark source image)

3. Use imagecopymerge to merge the small image into the specified position of the large image

4. Output the image

.5 Destroy resources

1. Simple image watermark

The target image that needs to be watermarked (assuming it is stored in d:/www/img/meinv.jpg on my computer) , the picture is as follows:

99.png

The logo picture that needs to be added (assuming it is stored in d:/www/img/logo.png on my computer), the picture is as follows:

document_2015-09-22_56010df4559d3.png

The most important thing is to use this function:

bool imagecopymerge (resource $target image, resource $source image, int $target starting x, int $target start y, int $source x, int $source y, int $source width, int $source height, int $transparency)

Note:
Transparency The value is an integer from 0-100. The difference between imagecopy and imagecopymerge is that one has transparency and the other does not.

Follow the summarized steps and do a simple method:

<?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);

?>

Let’s see the final effect as follows:

11.png

2. Do An intelligent image watermark function

1. We can make a function that automatically opens images

We have all learned the functions of creating images or opening images before:

1. imagecreate

2.imagecreatetruecolor

3.imagecreatefromjpeg etc.

Let’s reason. If we can find a way to get the MIME type of the image, we can just find the function to open the file based on the MIME type.

Therefore, this step is divided into two parts:

1. Get the file MIME type and return type.

2. Pass in the path, open the function, and return the resource.

Therefore, we can make the above two blocks into two functions.

Pass in the path of the image, return the width, height, and MIME type of the image into an array, and use the corresponding parameters when needed.

We can pass the mime type to the type associative array in $data. The code is as follows:

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

The function that opens a file passes in the type of a picture, the path of a picture is passed in, the picture is opened, and the resource type is returned.

In the following example, $type is judged using switch...case. If it is imagejpeg, imagecreatefromjpeg is used to open the file specified by the path in $path. Finally, a resource type is returned.

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

Automatically calculate position:

We can divide the position into 10 values, ranging from 0-9.

We use drawing to represent the position:

document_2015-09-22_5600ef919671d.png


##Note:

0 is a random position and can appear in anywhere on the page. But it cannot exceed the scope of the picture. The position of

0 is:

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

1’s position is:

x = 0 
y = 0

2’s position is:

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

3’s position:

The position of

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

4 is:

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

... ...and so on.

Let’s reason about the implementation code of 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;
    }

Finally call the image merging, output and destruction code:

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

We will integrate the final code After that, I will give you an experiment to see the effect:

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




?>

This article is only for technical personnel to communicate and learn and use technology.

Images used in this article:

Photos of Ms. Fan Bingbing are not for commercial use. All ownership rights belong to Ms. Fan Bingbing and related institutions.

The logo used in this article belongs to Baidu.

I hereby declare!


Next Section

<?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; } ?>
submitReset Code
ChapterCourseware