


Add watermark to php image upload (automatically add watermark)_PHP tutorial
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){
case 2:
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;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: | |
http://www.bkjia.com/PHPjc/444863.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/444863.html

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
