/**
* File upload class
*/
class uploadFile {
public $max_size = '1000000';//Set the upload file size
public $file_name = 'date';//The renaming method represents naming based on time, and others use the given name
public $allow_types;//The file extensions allowed to be uploaded, different file types are separated by "|"
public $errmsg = '';//Error message
public $uploaded = '';//Uploaded file name (including file path)
public $save_path;//Uploaded file save path
private $ files;//Submitted files waiting to be uploaded
private $file_type = array();//File type
private $ext = '';//Uploaded file extension
/**
* Constructor, initialization class
* @access public
* @param string $file_name Uploaded file name
* @param string $save_path Uploaded target folder
* /
public function __construct($save_path = './upload/',$file_name = 'date',$allow_types = '') {
$this->file_name = $file_name;//Renaming method Represents naming based on time, others use the given name
$this->save_path = (preg_match('//$/',$save_path)) ? $save_path : $save_path . '/';
$ this->allow_types = $allow_types == '' ? 'jpg|gif|png|zip|rar' : $allow_types;
}
/**
* Upload files
* @access public
* @param $files Files waiting to be uploaded ($_FILES[] from the form)
* @return boolean Return Boolean value
*/
public function upload_file( $files) {
$name = $files['name'];
$type = $files['type'];
$size = $files['size'];
$ tmp_name = $files['tmp_name'];
$error = $files['error'];
switch ($error) {
case 0 : $this->errmsg = '';
break;
case 1 : $this->errmsg = 'Exceeded the file size in php.ini';
break;
case 2 : $this->errmsg = 'Exceeded MAX_FILE_SIZE The file size specified by the option';
break;
case 3 : $this->errmsg = 'Only part of the file was uploaded';
break;
case 4 : $this->errmsg = 'No file was uploaded';
break;
case 5 : $this->errmsg = 'The uploaded file size is 0';
break;
default : $this->errmsg = 'Failed to upload file! ';
break;
}
if($error == 0 && is_uploaded_file($tmp_name)) {
//Detect file type
if($this->check_file_type($ name) == FALSE){
return FALSE;
}
//Detect file size
if($size > $this->max_size){
$this-> errmsg = 'The uploaded file '.$name.' is too large. The maximum supported file is '.ceil($this->max_size/1024).' kb file';
return FALSE;
}
$this->set_save_path();//Set the file storage path
$new_name = $this->file_name != 'date' ? $this->file_name.'.'.$this->ext : date('YmdHis').'.'.$this->ext;//Set a new file name
$this->uploaded = $this->save_path.$new_name;//Uploaded file name
//Move file
if(move_uploaded_file($tmp_name,$this->uploaded)) {
$this->errmsg = 'File'.$this->uploaded.'Uploaded successfully! ';
return TRUE;
}else{
$this->errmsg = 'File'.$this->uploaded.'Upload failed !';
return FALSE;
}
}
}
/**
* Check the uploaded file type
* @access public
* @param string $filename The file name waiting to be checked
* @return TRUE if the check passes, FALSE and an error message if it fails
*/
public function check_file_type($filename){
$ext = $this ->get_file_type($filename);
$this->ext = $ext;
$allow_types = explode('|',$this->allow_types);//Split the file extensions that are allowed to be uploaded Named array
//echo $ext;
//Check whether the uploaded file extension is among the file extensions allowed to be uploaded
if(in_array($ext,$allow_types)){
return TRUE;
}else{
$this->errmsg = 'Upload file'.$filename.'Type error, only supports uploading'.str_replace('|',',',$this->allow_types).' and other file types!';
return FALSE;
}
}
/**
* Get the file type
* @access public
* @param string $filename The target file name to get the file type
* @return string file type
*/
public function get_file_type($filename){
$info = pathinfo($filename);
$ext = $info['extension'];
return $ext;
}
/**
* Set the saving path after uploading the file
*/
public function set_save_path(){
$this->save_path = (preg_match('//$/',$this ->save_path)) ? $this->save_path : $this->save_path . '/';
if(!is_dir($this->save_path)){
//If the directory is not Exists, creates the directory
$this->set_dir();
}
}
/**
* Create directory
* @access public
* @param string $dir Path to create directory
* @return boolean Return error message and FALSE on failure
*/
public function set_dir($dir = null){
//Check whether the path exists
if(!$dir){
$dir = $this->save_path;
}
if(is_dir($dir)){
$ this->errmsg = 'The folder to be created already exists! ';
}
$dir = explode('/', $dir);
foreach($dir as $v){
if($v){
$d .= $v . '/';
if(!is_dir($d)){
$state = mkdir($d, 0777);
if(!$state)
$this-> ;errmsg = 'Error creating directory ' . $d . '!';
}
}
}
return true;
}
}
/*************************************************
* Image processing class
*
* You can generate thumbnails, watermark and other operations on images
* The default encoding of this class is UTF8. If you want to use it under GBK, please add a Chinese string watermark in the img_mark method. Remove the iconv annotation
*
* Since the sizes (pixels) of UTF8 Chinese characters and English letters are not easy to determine, when there are too many mixed Chinese and English characters, the string may be biased to the left
* or to the right, please depend on the project The environment needs GD library support for $strc_w = strlen($this->mark_str)*7+5 in the get_mark_xy method
*. For better use of this class, it is recommended to use GD library 2.0+
*
* @author kickflip@php100 QQ263340607
********************************** ****************/
class uploadImg extends uploadFile {
public $mark_str = 'kickflip@php100'; //Watermark string
public $str_r = 0; //String color R
public $str_g = 0; //String color G
public $ str_b = 0; //String color B
public $mark_ttf = './upload/SIMSUN.TTC'; //Watermark text font file (including path)
public $mark_logo = './upload/logo .png'; //Watermark image
public $resize_h;//Generate thumbnail height
public $resize_w;//Generate thumbnail width
public $source_img;//Source image file
public $dst_path = './upload/';//Thumbnail file storage directory, if left blank, the source image storage directory
/**
* Generate thumbnail image
* @access public
* @param integer $w The width of the reduced image (px)
* @param integer $h The height of the reduced image (px) px)
* @param string $source_img source image (path + file name)
*/
public function img_resized($w,$h, $source_img = NULL){
$source_img = $source_img == NULL ? $this->uploaded : $source_img;//Get the address of the source file. If it is empty, it defaults to the last uploaded image
if(!is_file($source_img)) { //Check whether the source image exists
$this->errmsg = 'File'.$source_img.'does not exist';
return FALSE;
}
$this->source_img = $source_img;
$img_info = getimagesize($source_img);
$source = $this->img_create($source_img); //Create source image
$ this->resize_w = $w;
$this->resize_h = $h;
$thumb = imagecreatetruecolor($w,$h);
imagecopyresized($thumb,$source,0, 0,0,0,$w,$h,$img_info[0],$img_info[1]);//Generate thumbnail image
$dst_path = $this->dst_path == '' ? $this ->save_path : $this->dst_path; //Get the target folder path
$dst_path = (preg_match('//$/',$dst_path)) ? $dst_path : $dst_path . '/'; //Add / after the target folder
if(!is_dir($dst_path)) $this->set_dir($dst_path); //Create the target folder if it does not exist
$dst_name = $ this->set_newname($source_img);
$this->img_output($thumb,$dst_name);//Output image
imagedestroy($source);
imagedestroy($thumb);
}
/**
*Watermark
*@access public
*@param string $source_img Source image path + file name
*@param integer $mark_type Watermark type (1 is English string, 2 is Chinese character String, 3 is the picture logo, the default is an English string)
*@param integer $mark_postion watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, the default is the lower right corner) ;
*@return watermarked image
*/
public function img_mark($source_img = NULL,$mark_type = 1,$mark_postion = 2) {
$source_img = $source_img == NULL ? $ this->uploaded : $source_img;//Get the address of the source file. If it is empty, it defaults to the last uploaded image
if(!is_file($source_img)) { //Check whether the source image exists
$this->errmsg = 'File'.$source_img.'does not exist';
return FALSE;
}
$this->source_img = $source_img;
$img_info = getimagesize ($source_img);
$source = $this->img_create($source_img); //Create the source image
$mark_xy = $this->get_mark_xy($mark_postion);//Get the watermark position
$mark_color = imagecolorallocate($source,$this->str_r,$this->str_g,$this->str_b);
switch($mark_type) {
case 1: //Add English string watermark
$str = $this->mark_str;
imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
$ this->img_output($source,$source_img);
break;
case 2: //Add Chinese string watermark
if(!is_file($this->mark_ttf)) { // Check if the font file exists
$this->errmsg = 'Watermarking failed: font file'.$this->mark_ttf.' does not exist!';
return FALSE;
}
$str = $this->mark_str;
//$str = iconv('gbk','utf-8',$str);//Convert character encoding If you use GBK encoding, please remove this line comment
imagettftext($source,12,0,$mark_xy[2],$mark_xy[3],$mark_color,$this->mark_ttf,$str);
$this->img_output($source,$ source_img);
break;
case 3: //Add image watermark
if(is_file($this->mark_logo)){ //If there is a watermarked logo image, get the basic information of the logo image , exit if it does not exist
$logo_info = getimagesize($this->mark_logo);
}else{
$this->errmsg = 'Watermarking failed: logo file '.$this->mark_logo.' does not exist! ';
return FALSE;
}
$logo_info = getimagesize($this->mark_logo);
if($logo_info[0]>$img_info[0] || $logo_info[ 1]>$img_info[1]) { //If the source image is smaller than the logo size, exit
$this->errmsg = 'Watermarking failed: source image'.$this->source_img.'than' .$this->mark_logo.'Small!';
return FALSE;
}
$logo = $this->img_create($this->mark_logo);
imagecopy ( $source, $logo, $mark_xy[4], $mark_xy[5], 0, 0, $logo_info[0], $logo_info[1]);
$this->img_output($source,$source_img);
break;
default: //Others are text images
$str = $this->mark_str;
imagestring($source,5,$mark_xy[0],$mark_xy[1],$str,$mark_color);
$this->img_output($source,$source_img);
break;
}
imagedestroy($source);
}
/**
* Get the watermark position
* @access private
* @param integer $mark_postion The position of the watermark (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner, others are right Lower corner)
* @return array $mark_xy The coordinates of the watermark position (index 0 is the English string watermark coordinate X, index 1 is the English string watermark coordinate Y,
* index 2 is the Chinese string watermark coordinate X, Index 3 is the Chinese string watermark coordinate Y, index 4 is the watermark image coordinate X, and index 5 is the watermark image coordinate Y)
*/
private function get_mark_xy($mark_postion){
$img_info = getimagesize($this->source_img);
$stre_w = strlen($this->mark_str)*9+5; //Watermark in English The length of the string (px) (the size of English characters in size 5 is about 9px, plus 5px for beauty)
//(The size of Chinese characters in size 12 is 12px, and the length of a Chinese character in utf8 is 3 characters A byte of a section is 4px, and the length of an English character is about 9px.
// In order to display completely when Chinese and English are mixed, set its length to the number of bytes * 7px)
$strc_w = strlen ($this->mark_str)*7+5; //The length of the watermark Chinese string (px)
if(is_file($this->mark_logo)){ //If there is a picture of the watermark logo, get it Basic information of the logo image
$logo_info = getimagesize($this->mark_logo);
}
//Since the starting positions of the strings in the imagestring function and the imagettftext function are different, the English and Chinese strings The Y position is also different
//The imagestring function is referenced from the upper left corner of the text. The imagettftext function is referenced from the lower left corner of the text.
switch($mark_postion){
case 1: //Position lower left corner
$mark_xy[0] = 5; //Watermark English string coordinates X
$mark_xy[1] = $img_info[1]-20; //Watermark English string coordinates Y
$mark_xy[2 ] = 5; //Watermark Chinese string coordinate X
$mark_xy[3] = $img_info[1]-5; //Watermark Chinese string coordinate Y
$mark_xy[4] = 5;// Watermark image coordinates 🎜>$mark_xy[0] = $img_info[0]-$stre_w; //Watermark English string coordinates X
$mark_xy[1] = $img_info[1]-20; //Watermark English string coordinates Y
$mark_xy[2] = $img_info[0]-$strc_w; //Watermark Chinese string coordinates X
$mark_xy[3] = $img_info[1]-5; //Watermark Chinese string coordinates Y
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//Watermark image coordinates X
$mark_xy[5] = $img_info[1]-$logo_info[1] -5; //Watermark image coordinate Y
break;
case 3: //Position upper left corner
$mark_xy[0] = 5; //Watermark English string coordinate X
$mark_xy[ 1] = 5; // Watermark English string coordinates Y
$mark_xy[2] = 5; // Watermark Chinese string coordinates X
$mark_xy[3] = 15; // Watermark Chinese string coordinates Y
$mark_xy[4] = 5; // Watermark image coordinates X
$mark_xy[5] = 5; // Watermark image coordinates Y
break;
case 4: //Position upper right Angle
$mark_xy[0] = $img_info[0]-$stre_w; //Watermark English string coordinates X
$mark_xy[1] = 5; //Watermark English string coordinates Y
$ mark_xy[2] = $img_info[0]-$strc_w; //Watermark Chinese string coordinates X
$mark_xy[3] = 15; //Watermark Chinese string coordinates Y
$mark_xy[4] = $img_info[0]-$logo_info[0]-5; //Watermark image coordinates X
$mark_xy[5] = 5; //Watermark image coordinates Y
break;
default : //Others The default is the lower right corner
$mark_xy[0] = $img_info[0]-$stre_w; //Watermark English string coordinate X
$mark_xy[1] = $img_info[1]-5;//Watermark English string coordinate Y
$mark_xy[2] = $img_info[0]-$strc_w; //Watermark Chinese string coordinate X
$mark_xy[3] = $img_info[1]-15;// Watermark Chinese string coordinate Y
$mark_xy[4] = $img_info[0]-$logo_info[0]-5;//Watermark image coordinate X
$mark_xy[5] = $img_info[1]- $logo_info[1]-5;//Watermark image coordinate Y
break;
}
return $mark_xy;
}
/**
* Create source image
* @access private
* @param string $source_img Source image (path + file name)
* @return img New image from target file
*/
private function img_create($source_img) {
$info = getimagesize($source_img);
switch ($info[2]){
case 1:
if(!function_exists('imagecreatefromgif')){
$source = @imagecreatefromjpeg($source_img);
}else{
$source = @imagecreatefromgif($source_img);
}
break;
case 2:
$source = @imagecreatefromjpeg($source_img);
break;
case 3:
$source = @imagecreatefrompng($source_img);
break;
case 6:
$source = @imagecreatefromwbmp($source_img);
break;
default:
$source = FALSE;
break;
}
return $source;
}
/**
* Rename image
* @access private
* @param string $source_img Source image path + file name
* @return string $dst_name Renamed image name (path + file name)
*/
private function set_newname($sourse_img) {
$info = pathinfo($sourse_img);
$new_name = $this->resize_w.'_' .$this->resize_h.'_'.$info['basename'];//Change the file name to: width_height_filename
if($this->dst_path == '') { //If the thumbnail storage path is empty, it defaults to the same folder as the source file
$dst_name = str_replace($info['basename'],$new_name,$sourse_img);
}else{
$dst_name = $this->dst_path.$new_name;
}
return $dst_name;
}
/**
* Output image
* @access private
* @param $im Processed image
* @param $dst_name Output image name (path + file name)
* @ return output image
*/
public function img_output($im,$ dst_name) {
$info = getimagesize($this->source_img);
switch ($info[2]){
case 1:
if(!function_exists('imagegif')) {
imagejpeg($im,$dst_name);
}else{
imagegif($im, $dst_name);
}
break;
case 2:
imagejpeg ($im,$dst_name);
break;
case 3:
imagepng($im,$dst_name);
break;
case 6:
imagewbmp($im, $dst_name);
break;
}
}
}
?>
php file upload class source code packaging

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
