Add image watermark to uploaded images in php_PHP tutorial
相信大家都知道怎么用PHP为图片增加文字水印,可是如果水印效果为图片呢?该怎么办?别担心,下文就为您分解。我也是近日用到此功能,所以去百度上搜集到了这篇文档,希望对各位有所帮助.
Dragon’s Heart Column phpshao.cublog.cn
if($_POST['action']=='doup')
{
$uptypes=array('image/jpg','image/jpeg','image/pjpeg','image/gif');//Upload image file type list
$wFile=$_FILES['upfile'];//Get file path
$waterimg="water.gif";//Watermark image path //print_r($wFile);
if(in_array($wFile['type'], $uptypes))
{//Check the file type, if the uploaded file is a jpg or gif image, add a watermark
If(strstr($wFile['type'],"jp"))
{ //If the uploaded image type is jpg, pjpeg, jpeg, use imagecreatefromjpeg to read the target file
$im = imageCreatefromjpeg($wFile['tmp_name']);
$wfilew=imagesx($im);//Get the width of the image
$wfileh=imagesy($im);//Get the height of the image }
else
{//Otherwise, if the uploaded image type is gif, use imagecreatefromgif to read the target file
$im = imageCreatefromgif($wFile['tmp_name']);
$wfilew=imagesx($im);//Get the width of the image
$wfileh=imagesy($im);//Get the height of the image
}
//Set blending mode
Imagealphablending($im, true);
//Read watermark file
$im2 = imagecreatefrompng($waterimg);//If the watermark image is jpg, this can be changed to $im2 = imagecreatefromjpeg($waterimg) //$white = imagecolorallocate($im2, 255, 255, 255);
// imagecolortransparent($im2,$white); //Set transparent color, these two sentences are not needed $waterw=imagesx($im2);//Get the width of the watermark image
$waterh=imagesy($im2);//Get the height of the watermark image
//Randomly put watermarks into pictures
$randval = rand(0,9);//Generate random numbers between 0-9
if($randval==0||$randval==3||$randval==2||$randval==8||$randval==7){//More positions can be improved here
$wimgx=5;$wimgy=5;//Place it in the upper left corner
}else{
$wimgx=$wfilew-5-$waterw;$wimgy=$wfileh-5-$waterh;//Place in the upper right corner
} //Copy the watermark to the target file
imagecopy($im, $im2, $wimgx, $wimgy, 0, 0, $waterw,$waterh);
//Output picture
If(strstr($wFile['type'],"jp")){ //Same as above
imagejpeg($im,$wFile['tmp_name']);
}else{
imagegif($im,$wFile['tmp_name']);
}
Imagedestroy($im);
Imagedestroy($im2);
copy($wFile['tmp_name'],$wFile['name']); //Upload
}
else echo "The picture does not match!!";
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
