Home Backend Development PHP Tutorial Add watermark to php image upload (automatically add watermark)_PHP tutorial

Add watermark to php image upload (automatically add watermark)_PHP tutorial

Jul 20, 2016 am 11:08 AM
php upload code add picture Increase document watermark beautiful automatic

PHP image upload and watermark (automatically add watermark)/*This is a perfect PHP file upload code. After the image is uploaded successfully, it will automatically add a watermark to the image. This is very good and fast. You have to add it one by one manually. watermark effect.

php tutorial picture upload and watermark (automatically add watermark)
/*
This is a perfect php file upload code. After the picture is uploaded successfully, it will automatically add a watermark to the picture, which is very fast and fast. To improve, you have to manually add the watermark effect one by one.
*/

function upload($uploadfile,$watermark=1,$watertype=1,$content){
foreach($uploadfile['name'] as $key=>$name) { // Multi-file upload function
uploadall($uploadfile,$key,$watermark,$watertype,$content);
}
}
function uploadall($uploadfile,$i,$watermark,$watertype ,$content) {
$watermark=$watermark; //Whether to attach a watermark (1 is watermark, others are not watermarked);
$watertype=$watertype; //Watermark type (1 is text, 2 is the picture)
$watercontent=$content; //The content of the watermark
if(empty($uploadfile['name'][$i])) {
die("No file uploaded" );
}
if($uploadfile['error'][$i] == 2) { //Verify the html judgment result
die("The uploaded file is too large");
}
$allow_filemaxsize = 2048000; // 2m
$filesize = $uploadfile['size'][$i];
if($filesize > $allow_filemaxsize) {
die(" The uploaded file is too large");
}
$allow_filetypes = array("jpeg", "gif", "png","jpg","pjpeg");//Unified resources for files allowed to be uploaded Type mimetype
$allow = false; $t) {
if(strpos($mimetype, $t) !== false) {
$allow = true; //File type that meets the upload conditions was found
break;
}
}
if($allow == false) {
die("The uploaded file type is not allowed");
}
$result = is_uploaded_file($uploadfile['tmp_name' ][$i]); //Determine whether it is generated by the upload action
if(!$result) {
die("The uploaded file is incorrect");
}
$uploaddir = "img/"; //Upload file saving directory
if(!file_exists($uploaddir)) mkdir($uploaddir, 0777, true);//If the upload saving directory does not exist, create it recursively
/* *Rename file**/
$filetype = explode(".", $uploadfile['name'][$i]);
$filetype = array_pop($filetype);
$uploadfilename = time ().".".$filetype;
$_session['filename'] = $uploadfilename;
//end
$result = move_uploaded_file($uploadfile['tmp_name'][$i], $uploaddir.$uploadfilename);
if($result) {
echo "File uploaded successfully";
}else{
switch($uploadfile['error'][$i]) {
case 1:return "The uploaded file exceeds the maximum value set in php.ini";break;
case 2:return "The uploaded file exceeds the maximum value set in html";break;
case 3:return "Only part of the file was uploaded";break;
case 4;return "No file was uploaded";break;
default:die("File upload failed");
}
}
if($watermark==1) {
$iinfo=getimagesize($uploaddir.$uploadfilename); //Get the relevant information of the image and get the array
$nimage=imagecreatetruecolor( $iinfo[0],$iinfo[1]);
$white=imagecolorallocate($nimage,255,255,255); //Set the background color to white
$black=imagecolorallocate($nimage,0,0,0 ); //Set the background color to black
$red=imagecolorallocate($nimage,255,0,0); //Set the background color to red
imagefill($nimage,0,0,$white); //The background is filled with white
switch ($iinfo[2]) {
case 1:
$simage =imagecreatefromgif($uploaddir.$uploadfilename);
break;
case 2:
$simage =imagecreatefromjpeg($uploaddir.$uploadfilename);
break;
case 3:
$simage =imagecreatefrompng($uploaddir.$uploadfilename);
break;
case 6:
$simage =imagecreatefromwbmp($uploaddir.$uploadfilename);
break;
default:
die("Unsupported file type");
exit;
}
imagecopy($nimage,$simage,0,0,0,0,$iinfo['0'],$iinfo['1']);
switch($watertype) {
case 1: // Watermark string
imagestring($nimage,5,$iinfo['0']/2-50,$iinfo['1']-30,$watercontent,$black ; $simage1,$iinfo['0']/2+50,$iinfo['1']-100,0,0,$size[0],$size[1]);
imagedestroy($simage1) ;
break;
}

switch ($iinfo[2]) {
case 1:
imagejpeg($nimage, $uploaddir.$uploadfilename); //Change the image $nimage creates a jpeg format file with the $destination file name
break;

case 2:

imagejpeg($nimage, $uploaddir.$uploadfilename);
break;
case 3:
imagepng($nimage, $uploaddir.$uploadfilename);
break;
case 6:
imagewbmp($nimage, $uploaddir.$uploadfilename);
break;
}
imagedestroy($nimage); //Overwrite the original uploaded file
imagedestroy($simage);
}
}
if(@$_get['act'] == "insert" ) { //No parameter verification is done
//The four parameters of this function are: the name value of the upload control; whether to add a watermark (1 is plus, other numbers are bad);
//Watermark Type (1 is a string, 2 is a picture); the content of the watermark, write the data when it is a string, and write the address of the picture when it is a picture;
$picture = upload($_files['picture'],1,2, "img/watermark.gif");//Upload the file and return the uploaded file path name

}

?>

Upload code



Product picture:

      
                             ;td colspan="2" align="center">









http://www.bkjia.com/PHPjc/444863.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/444863.html

php image upload with watermark (automatically add watermark)/*This is a perfect php file upload code, picture After the upload is successful, a watermark will be automatically added to the image, which will improve the quality quickly...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

See all articles