PHP adds watermarks to images

Source: Internet
Author: User
Tags imagejpeg

Accounting for 134 (experimental Class) Janga

Some sites may be for the purpose of promoting or anti-theft in the uploading of images to add a logo watermark, I have seen some webmaster directly with the image tool to add watermark and then upload, of course, this can also achieve the effect. But PHP can also be done to add watermark to the image function. The function of PHP function here is no longer waste of breath introduced, directly say how to achieve. There are generally the following steps

First, prepare the logo map.

This logo is not available in any format, GIF or PNG or JPG can be adjusted to the right size. Transparency is not required because we can flexibly set the transparency in PHP. If you do not use the cartographic tools to create a logo can go to google Online logo production, click into a link to tap a few words can be generated, of course, free of charge.

Second, write the Watermark class

Why write class, this is tending to human thought, also convenient to use maintenance. Here is the key to give the following function name, that is, the realization of the watermark function is mainly by these functions function operation
1.imagecreatefromjpeg//Open JPG image 2.imagecreatefromgif//open GIF picture
3.imagecreatefrompng//Open PNG image
4.imagecreatefromwbmp//Open wbmp picture (less)
5.getimagesize//Get picture size information
6.imagecopymerge//Integrate multiple images (the main function of adding watermarks)
7.imagejpeg//Save JPG Images
8.imagegif//Save GIF pictures
9.imagepng//Save PNG images

After completing the above steps, you will be able to add watermarks to your uploaded images. Here's a little demo I wrote.
Watermark Class

<?php
/**
* Add Watermark Class
*
*/

Class water{

Private $imgPath; Picture path

Public function __construct ($imgPath = "./") {
$this->imgpath = RTrim ($imgPath, "/"). " /";
}

Write Watermark Action
Public Function Waterinfo ($ground, $water, $pos =0, $prefix = "Lee_", $tm =50) {
$allPathGround = $this->imgpath. $ground;
$allPathWater = $this->imgpath. $water;
$groundInfo = $this->imginfo ($allPathGround);
$waterInfo = $this->imginfo ($allPathWater);

Determine if the watermark image is larger than the original
if (! $newPos = $this->imgpos ($groundInfo, $waterInfo, $pos)) {
echo "Your watermark picture is bigger than the original!"
return false;
}

Open Resource
$groundRes = $this->imgres ($allPathGround, $groundInfo [' mime ']);
$waterRes = $this->imgres ($allPathWater, $waterInfo [' mime ']);

Consolidate resources
$newGround = $this->imgcopy ($groundRes, $waterRes, $newPos, $waterInfo, $TM);

Saving Resources
$this->saveimg ($newGround, $ground, $groundInfo [' MIME '], $prefix);

}

Private Function Saveimg ($img, $ground, $info, $prefix) {
$path = $this->imgpath. $prefix. $ground;
Switch ($info) {
Case "Image/jpg":
Case "Image/jpeg":
Case "Image/pjpeg":
Imagejpeg ($img, $path);
Break
Case "Image/gif":
Imagegif ($img, $path);
Break
Case "Image/png":
Imagepng ($img, $path);
Break
Default
IMAGEGD2 ($img, $path);
}
}

Private Function Imgcopy ($ground, $water, $pos, $waterInfo, $tm) {
Imagecopymerge ($ground, $water, $pos [0], $pos [1],0,0, $waterInfo [0], $waterInfo [1], $TM);
return $ground;
}

Private Function Imgres ($img, $imgType) {
Switch ($imgType) {
Case "Image/jpg":
Case "Image/jpeg":
Case "Image/pjpeg":
$res =imagecreatefromjpeg ($IMG);
Break
Case "Image/gif":
$res =imagecreatefromgif ($IMG);
Break
Case "Image/png":
$res =imagecreatefrompng ($IMG);
Break
Case "Image/wbmp":
$res =imagecreatefromwbmp ($IMG);
Break
Default
$res =imagecreatefromgd2 ($IMG);
}
return $res;
}

Location is
1 top left 2 Upper 3 upper right
4 left Middle 5 Middle 6 right middle
7 lower left 8 lower 9 right lower
0 random position
Private Function Imgpos ($ground, $water, $pos) {
if ($ground [0]< $water [0] | | $ground [1]< $water [1])//Judging watermark compared to the original image if the watermark is high or wide than the original teenager returns false
return false;
Switch ($pos) {
Case 1:
$x = 0;
$y = 0;
Break
Case 2:
$x =ceil (($ground [0]-$water [0])/2);
$y = 0;
Break
Case 3:
$x = $ground [0]-$water [0];
$y = 0;
Break
Case 4:
$x = 0;
$y =ceil (($ground [1]-$water [1])/2);
Break
Case 5:
$x =ceil (($ground [0]-$water [0])/2);
$y =ceil (($ground [1]-$water [1])/2);
Break
Case 6:
$x = $ground [0]-$water [0];
$y =ceil (($ground [1]-$water [1])/2);
Break
Case 7:
$x = 0;
$y = $ground [1]-$water [1];
Break
Case 8:
$x =ceil ($ground [0]-$water [0]/2];
$y = $ground [1]-$water [1];
Break
Case 9:
$x = $ground [0]-$water [0];
$y = $ground [1]-$water [1];
Break
Case 0:
Default
$x =rand (0, $ground [0]-$water [0]);
$y =rand (0, $ground [1]-$water [1]);
}
$xy []= $x;
$xy []= $y;
return $xy;
}

function to get picture information
Private Function Imginfo ($img) {
Return getimagesize ($IMG);
}
}
?>

Working with pages

<?php   // 包含类文件 include ( "water.class.php" );      // 可以传进一个添加水印后保存的路径,路径相对于类脚本 // 如果为空则默认是脚本当前路径 $water = new water();     // 参数: // 1.  源图  // 2. 水印图 即 LOGO // 3.  位置 // 位置为 // 1 左上 2中上 3右上 // 4 左中 5中中 6右中 // 7 左下 8中下 9右下 // 0 随机位置   // 4. 保存添加水印图片的文件名前缀  //  5. 透明度 $water ->waterInfo( "02.jpg" , "01.jpg" ,9, "haha" ,20);

?>

Add a watermark to the image in addition to the base of the GD library implementation, in fact, you can also imagemagick this tool, it's more powerful, but generally not much demand, with the above code.

PHP adds watermarks to images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.