PHP file upload class can generate thumbnail code_PHP tutorial
php教程文件上传类可生成缩略图代码
if ($_GET['action'] == 'save') {
$up = new upload();
$up->set_dir(dirname(__FILE__).'/upload/','{y}/{m}');
$up->set_thumb(100,80);
$up->set_watermark(dirname(__FILE__).'/jblog/images/watermark.png',6,90);
$fs = $up->execute();
var_dump($fs);
}
?>
test
class upload { //Allow attachment types to be uploadedwww.bkjia.com
var $field; //Upload control name
var $maxsize; //Maximum allowed file size in KB
var $thumb_width; //Thumbnail width
var $thumb_height; //Thumbnail height
var $watermark_file; //Watermark image address var $watermark_pos; //Watermark position
var $watermark_trans;//Watermark transparency
//Constructor function
//$types: File types allowed to be uploaded, $maxsize: Allowed size, $field: Upload control name, $time: Custom upload time
function upload($types = 'jpg |png', $maxsize = 1024, $field = 'attach', $time = '') {
$this->allow_types = explode('|',$types);
$this-> ;maxsize = $maxsize * 1024;
//Set and create the directory where files are stored specifically. "
" //$basedir: Base directory, must be a physical path. d} 🎜> if (!emptyempty($filedir)) { ;time),date('m',$this->time),date('d',$this->time)),strtolower($filedir)); ',$filedir);
foreach ($dirs as $d) { !emptyempty($d) && $dir .= $d.'/';
!is_dir($dir) && @ mkdir($dir,0777); 🎜>
//Image thumbnail settings, no need to set if thumbnails are not generated
$this->thumb_width = $width; $this->thumb_height = $height; , if no watermark is generated, there is no need to set it.
//$file: watermark image, $pos: watermark position, $trans: watermark transparency
function set_watermark ($file, $pos = 6, $trans = 80) {
$this-> watermark_file = $file;
$this->watermark_pos = $pos;
$this->watermark_trans = $trans;
} > /*------- -------------------------------------------------- ------- Execute the file upload, and return a file information array containing a successful or failure of uploading or failure. If the upload fails, it is the local file name.
dir is the physical path to store the attachment on the server. If the upload fails, the value does not exist.
size is the size of the attachment. If the upload fails, the value does not exist. 1 means success, -1 means the file type is not allowed, -2 means the file size exceeds
--------------------------------- ------------------------------------*/ files = array(); //Successfully uploaded file information foreach ($keys as $key) { _FILES[$field]['name'][$key]); //Get the file extension
$filename = date('Ymdhis',$this->time).mt_rand(10,99).' . '. $ fileExt; // Generate file name
$ Filedir = $ this- & gt; dir; // The actual storage catalog of the attachment
$ Filesize = $ _Files [$ Field] [' SIZE '] [$ Key ]; //File size
$files[$key]['name'] = $_FILES[$field]['name'][$key];
$files[$key]['flag'] = -1;
continue;
}
//文件大小超出
if ($filesize > $this->maxsize) {
$files[$key]['name'] = $_FILES[$field]['name'][$key];
$files[$key]['name'] = $filesize;
$files[$key]['flag'] = -2;
continue;
}
$files[$key]['name'] = $filename;
$files[$key]['dir'] = $filedir;
$files[$key]['size'] = $filesize;
//保存上传文件并删除临时文件
if (is_uploaded_file($_FILES[$field]['tmp_name'][$key])) {
move_uploaded_file($_FILES[$field]['tmp_name'][$key],$filedir.$filename);
@unlink($_FILES[$field]['tmp_name'][$key]);
$files[$key]['flag'] = 1;
//对图片进行加水印和生成缩略图
if (in_array($fileext,array('jpg','png'))) {
if ($this->thumb_width) {
if ($this->create_thumb($filedir.$filename,$filedir.'thumb_'.$filename)) {
$files[$key]['thumb'] = 'thumb_'.$filename; //缩略图文件名
}
}
$this->create_watermark($filedir.$filename);
}
//Create a thumbnail, generate a thumbnail with the same extension
//$src_file : Source image path, $thumb_file : Thumbnail path = $this->thumb_height;
//If the source image is less than or equal to the thumbnail then Copy the source image as a thumbnail if ($src_info[0] if (!copy($src_file,$thumb_file)) {
; > // Calculate the thumbnail size proportionally src_info[1] - $t_height) { _width = ($t_height / $src_info [1]) * $src_info[0]; 🎜>
switch ($fileext) {
case 'gif' :
$src_img = ImageCreateFromGIF($src_file); break;
}
If the degree is good, priority will be given
if (function_exists('imagecopyresampled')) {
@ImageCopyResampled($thumb_img,$src_img,0,0,0,0,$t_width,$t_height,$src_info[0],$src_info[1]) ; 🎜> }
b_img,$thumb_file); break;
ImageGIF($thumb_img,$thumb_file); break;
case 'png' :
ImagePNG($thumb_img,$thumb_file); break; Image @ I> // $ file: The file to add watermark
Function Create_watermark ($ File) {
// The file does not exist, and return
if (! File_exists ($ This- & GT; Watermark_file) || !file_exists($file)) return; $gd_allow_types = array();
if (function_exists('ImageCreateFromGIF')) $gd_allow_types['image/gif'] = 'ImageCreateFromGIF'; reateFromPNG ';
if (function_exists('ImageCreateFromJPEG')) $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG';
//获取文件信息
$fileinfo = getImageSize($file);
$wminfo = getImageSize($this->watermark_file);
if ($fileinfo[0]
if (array_key_exists($fileinfo['mime'],$gd_allow_types)) {
if (array_key_exists($wminfo['mime'],$gd_allow_types)) {
//从文件创建图像
$temp = $gd_allow_types[$fileinfo['mime']]($file);
$temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file);
//水印位置
switch ($this->watermark_pos) {
case 1 : //顶部居左
$dst_x = 0; $dst_y = 0; break;
case 2 : //顶部居中
$dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break;
case 3 : //顶部居右
$dst_x = $fileinfo[0]; $dst_y = 0; break;
case 4 : //底部居左
$dst_x = 0; $dst_y = $fileinfo[1]; break;
case 5 : //底部居中
$dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break;
case 6 : //底部居右
$dst_x = $fileinfo[0]-$wminfo[0]; $dst_y = $fileinfo[1]-$wminfo[1]; break; t_rand(0, $fileinfo[0]-$wminfo[0]); $dst_y = mt_rand(0,$fileinfo[1]-$wminfo[1]); 'ImageAlphaBlending' )) ImageAlphaBlending($temp_wm,True); //Set the image blending mode
if (function_exists('ImageSaveAlpha')) ImageSaveAlpha($temp_wm,True); //Save the complete alpha channel information 🎜> x,$dst_y,0,0,$wminfo[0],$wminfo [1],$this->watermark_trans); info[1] );
case 'image/jpeg' : file); ,$file); imageGIF($temp,$file); //Destroy the zero-time image >
function fileext($filename) {
return strtolower(substr(strrchr($filename,'.'),1,10));
} }
}
http://www.bkjia.com/PHPjc/444941.html
true

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor
