Home  >  Article  >  Backend Development  >  PHP image processing: Example of adding image watermark using imagecopy function, _PHP tutorial

PHP image processing: Example of adding image watermark using imagecopy function, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:42939browse

PHP image processing using the imagecopy function to add image watermark examples,

Adding watermarks to images is also a common function in image processing. Because as long as you see the pictures on the page, you can easily get them. You don’t want the pictures you worked hard to edit to be taken away and used by others without any effort, so add watermarks to the pictures to confirm the copyright and prevent the pictures from being stolen. You can use text (company name plus website) or pictures (company LOGO) to make watermarks. Picture watermarks are more effective because they can be beautified with some picture-making software. To use text as a watermark, just draw some text on the picture. If you want to make a picture watermark, you need to first understand the imagecopy() function in the GD library, which can copy part of the picture. The prototype of this function looks like this:

Copy code The code is as follows:

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)

The function of this function is to copy a part of the src_im image whose coordinates start from src_x, src_y, have a width of src_w, and a height of src_h to the position of the dst_im image where the coordinates are dst_x and dst_y. Taking a picture in JPEG format as an example, write a function watermark() that adds a watermark to the picture. The code is as follows:

Copy code The code is as follows:

//Add image watermark to the background image (random position), the background image format is jpeg, and the watermark image format is gif
function watermark($filename,$water){
//Get the width and height of the background image
list($b_w,$b_h) = getimagesize($filename);
//Get the width and height of the watermark image
list($w_w,$w_h) = getimagesize($water);
//Put the watermark image in the background image at a random starting position
$posX = rand(0, ($b_w-$w_w));
$posY = rand(0, ($b_h-$w_h));
//Resources for creating background images
$back = imagecreatefromjpeg($filename);
//Resources for creating watermark images
$water = imagecreatefromgif($water);
//Use the imagecopy() function to copy the watermark image to the location specified by the background image
imagecopy($back, $water, $posX, $posY, 0, 0, $w_w, $w_h);
//Save the background image with watermark image
imagejpeg($back,$filename);
imagedestroy($back);
imagedestroy($water);
}
watermark("brophp.jpg", "logo.gif");
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914054.htmlTechArticlePHP image processing: Use the imagecopy function to add image watermark examples. Adding watermarks to images is also a common function in image processing. Because as long as you see the pictures on the page, you can easily...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn