


PHP image upload code (with the function of generating thumbnails and adding watermarks)_PHP tutorial
This image upload source code can upload images and also has the function of generating thumbnails and adding watermarks to uploaded images. It can be said to be a perfect image uploader.
php tutorial image upload code (with the function of generating thumbnails and adding watermarks)
This image upload source code can upload images and also has the function of generating thumbnails and adding watermarks to uploaded images. It can be said to be a perfect image uploader.
class upfile {
public $filepath = "www.bKjia.c0m/"; //Folder to store uploaded filespublic $filesize = 1000000; //Allow upload size
//If you want to modify the types of files allowed to be uploaded, please search [ switch ($upfiletype) { //File type ]
public $reimagesize = array (
true, //Whether to generate thumbnails
400, //Thumbnail width
300,//Thumbnail height
"" //Thumbnail storage folder. If empty, it is in the same directory as the current file to generate thumbnails. The file prefix is r_
); //Whether to generate thumbnail array (generate or not, thumbnail width, thumbnail height, storage folder); Note: the storage folder is followed by '/'public $india = true; //Whether to watermark true or false
public $indiaimage = ""; //If the watermark image address is empty, the image watermark will not be printed. If there is a text watermark, it is recommended not to enable the image watermark
public $indiaimagex = 100; //The distance between the image and the left side of the image
public $indiaimagey = 10; //The distance between the picture and the top of the picture
public $indiatext = "www.bKjia.c0m"; //Watermark text
public $fontsize = 6; //Watermark text size, 1 minimum and 6 maximum
public $indiatextx = 10; //The distance between the text and the left side of the image
public $indiatexty = 10; //The distance between the text and the top of the picture
public $r = 250; //Three primary colors of picture color $r red
public $g = 250; //$g green
public $b = 250; //$bblue
public $indiapath = ""; //The watermarked picture saving path, if it is empty, it will directly replace the original picture
//开始上传处理
function uploadfile($upfile) {
if ($upfile == "") {
die("uploadfile:参数不足");
}
if (!file_exists($this->filepath)) {
mkdir($this->filepath);
}
$upfiletype = $upfile['type'];
$upfilesize = $upfile['size'];
$upfiletmpname = $upfile['tmp_name'];
$upfilename = $upfile['name'];
$upfileerror = $upfile['error'];
if ($upfilesize > $this->filesize) {
return false; //文件过大
}
switch ($upfiletype) { //文件类型
case 'image/jpeg' :
$type = 'jpg';
break;
case 'image/pjpeg' :
$type = 'jpg';
break;
case 'image/png' :
$type = 'png';
break;
case 'image/gif' :
$type = 'gif';
break;
}
if (!isset ($type)) {
return false; //不支持此类型
}
if (!is_uploaded_file($upfiletmpname) or !is_file($upfiletmpname)) {
return false;
; //文件不是经过正规上传的;
}
if ($this->upfileerror != 0) {
return false; //其他错误
}
if ($this->upfileerror == 0) {
if (!file_exists($upfiletmpname)) {
return false; //临时文件不存在
} else {
$filename = date("ymdhis", time() + 3600 * 8); //图片已当前时间命名
$filename = $this->filepath . $filename . "." . $type;
if (!move_uploaded_file($upfiletmpname, $filename)) {
return false; //文件在移动中丢失
} else {
if ($this->india == true) {
$this->goindia($filename, $type,true);
} else {
if ($this->reimagesize[0] == true) {
$this->goreimagesize($filename, $type);
} else {
return true; //上传成功!
unlink($upfiletmpname);
}
}
}}
}}
//Add watermark processing
function goindia($filename, $filetype,$reimage=false) {
if (!file_exists($filename)) {
$this->reerror(7); //The file to be watermarked does not exist
} else {
if ($filetype == "jpg") {
$im = imagecreatefromjpeg($filename);
} else
if ($filetype == "gif") {
$im = imagecreatefromgif($filename);
} else
If ($filetype == "png") {
$im = imagecreatefrompng($filename);
}
If ($this->indiatext != "") { //If the watermark text is not empty
$textcolor = imagecolorallocate($im, $this->r, $this->g, $this->b); //Set text color
Imagestring($im, $this->fontsize, $this->indiatextx, $this->indiatexty, $this->indiatext, $textcolor); //Write text into the image
}
if ($this->indiaimage != "") {//If the watermark image is not empty
$indiaimagetype = getimagesize($this->indiaimage);
$logow = $indiaimagetype[0]; //Get the width of the watermark image
$logoh = $indiaimagetype[1]; //Get the height of the watermark image
Switch ($indiaimagetype[2]) { //Determine the format of the watermark image
case 1 :
$indiaimagetype = "gif";
$logo = imagecreatefromgif($this->indiaimage);
break;
Case 2:
$indiaimagetype = "jpg";
$logo = imagecreatefromjpeg($this->indiaimage);
break;
Case 3:
$indiaimagetype = "png";
$logo = imagecreatefrompng($this->indiaimage);
break;
}
Imagealphablending($im, true); //Turn on color blending mode
Imagecopy($im, $logo, $this->indiaimagex, $this->indiaimagey, 0, 0, $logow, $logoh);
Imagedestroy($im);
Imagedestroy($logo);
}
}
if ($this->indiapath == "") { //If the watermark storage address is not empty
if ($filetype == "jpg") {
Imagejpeg($im, $filename);
} else
if ($filetype == "gif") {
Imagegif($im, $filename);
} else
If ($filetype == "png") {
Imagepng($im, $filename);
}
if($reimage == true){
$this->goreimagesize($filename,$filetype);
}else{
Return true; //Watermark added successfully
}
} else {
if (!file_exists($this->indiapath)) {
mkdir($this->indiapath);
Return false; //Please re-upload
} else {
$indianame = basename($filename);
$indianame = $this->indiapath . $indianame;
If ($filetype == "jpg") {
Imagejpeg($im, $indianame);
} else
If ($filetype == "gif") {
Imagegif($im, $indianame);
} else
If ($filetype == "png") {
Imagepng($im, $indianame);
}
If($reimage == true){
$this->goreimagesize($indianame,$filetype);
echo $indianame;
}else{
Return true; //Watermark added successfully
}
}
}
}
function goreimagesize($filename, $filetype) {
if (!file_exists($filename)) {
Return false; //The picture to be generated as a thumbnail does not exist
} else {
if ($filetype == 'jpg') {
$reimage = imagecreatefromjpeg($filename);
}
elseif ($filetype == 'png') {
$reimage = imagecreatefrompng($filename);
} else
if ($filetype == 'gif') {
$reimage = imagecreatefromgif($filename);
}
if (isset ($reimage)) {
$srcimagetype = getimagesize($filename);
$srcimagetypew = $srcimagetype[0]; //Get the original image width
$srcimagetypeh = $srcimagetype[1]; //Get the original image height
$reim = imagecreatetruecolor($this->reimagesize[1], $this->reimagesize[2]);
Imagecopyresized($reim, $reimage, 0, 0, 0, 0, $this->reimagesize[1], $this->reimagesize[2], $srcimagetypew, $srcimagetypeh);
$reimagepath = $this->reimagesize[3];
If ($reimagepath != "") { //If the watermark storage address is not empty
If (!file_exists($reimagepath)) {
mkdir($reimagepath);
} else {
$reimagename = basename($filename);
$reimagename = $reimagepath . "r_" . $reimagename;
If ($filetype == "gif")
Imagegif($reim, $reimagename);
else
If ($filetype == "jpg")
Imagejpeg($reim, $reimagename);
else
if ($filetype == "png")
Imagepng($reim, $reimagename);
return true;
}
} else {
$filename = basename($filename);
If($this->indiapath == ""){
$filename = $this->filepath."r_" . $filename;
}else{
$filename = $this->indiapath."r_" . $filename;
}
If ($filetype == "gif")
Imagegif($reim, $filename);
else
If ($filetype == "jpg")
Imagejpeg($reim, $filename);
else
If ($filetype == "png")
Imagepng($reim, $filename);
Return true;
}}
}
}}
if ($_post["submit"]) {
$file = $_files['uploadfile'];
$upfile = new upfile();
echo $upfile->uploadfile($file);
}
?>

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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.

In this chapter, we are going to learn the following topics related to routing ?

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

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

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
