


PHP image file upload class (can automatically generate thumbnails)
分享一个php图片上传类,且可以自动生成缩略图,有需要的朋友,可以作个参考,希望可以对您有用。
php实现的图片文件上传类,代码: <?php /** 功 能:文件上传类 支持文件夹自动分组保存; 创建类:参数(文件域,文件原名,文件大小); $myupload = new upfileClass($upfile,$upfile_name,$upfile_size); $myupload->savefile(); # 保存方法 并返回保存路径附带文件名; @ echo MakeBuild($BuildFile,$newFile,$File_width); 生成指定文件的缩略图; $myupload->MakeBuild("images/a.jpg","news/b.jpg","100"); */ class upfileClass { var $upfile, $upfile_name, $upfile_size; var $new_upfile_name; # 上传后的文件名称 ; var $fleth, $fileExtent; # 文件扩展名(类型) ; var $f1, $f2, $f3; # 文件保存路径(多级) upfiles/2008-01/08/; var $filename; # 文件(带路径) ; var $maxSize, $File_type; # 允许上传文件的大小 允许上传文件的类型 ; var $BuildFile,$newFile,$File_width,$File_height,$rate; function upfileClass($upfile,$upfile_name,$upfile_size) { $this->upfile = $upfile; $this->upfile_name = $upfile_name; $this->upfile_size = $upfile_size; $this->new_upfile_name = $this->CreateNewFilename($this->upfile_name); $this->f1 = "upfiles"; $this->f2 = $this->f1."/".date('Y')."-".date('m'); $this->f3 = $this->f2."/".date('d'); $this->filename = $this->f3 . "/" . $this->new_upfile_name; $this->maxSize = 500*1024; # 文件大小 500KB $this->File_type = "gif/jpg/jpeg/png"; # 允许上传的文件类型 } # 创建新文件名 (原文件名) function CreateNewFilename($file_name) { $this->fleth = explode(".",$file_name); $this->fileExtent = $this->fleth[(int)count($this->fleth)-1]; # 获取文件后缀; $tmpstr = date('YmdHis') . "." .$this->fileExtent; # 创建新文件名; return $tmpstr; } # 检测文件类型是否正确 function chk_fileExtent() { $iwTrue = 0; $fle = explode("/",$this->File_type); for($i=0; $i < count($fle); $i++){ if( $this->fileExtent == $fle[$i] ) { $iwTrue = (int) $iwTrue + 1; } } if( $iwTrue == 0 ){ $this->msg("文件不符合 ".$this->File_type." 格式!"); } } # 提示错误信息并终止操作 function msg($Error) { echo "<script language=/"javascript/">/n"; echo " alert('".$Error."');/n"; echo " window.history.back();/n"; echo "</script>/n"; die(); } # 保存文件 function savefile() { $this->chk_fileExtent(); $this->chk_fileSize(); $this->CreateFolder( "../".$this->f1 ); $this->CreateFolder( "../".$this->f2 ); $this->CreateFolder( "../".$this->f3 ); return $this->chk_savefile(); } # 检测上传结果是否成功 function chk_savefile() { $copymsg = copy($this->upfile,"../".$this->filename); if( $copymsg ){ return $this->filename; } else{ $this->msg("文件上传失败! /n/n请重新上传! "); } } # 创建文件夹 function CreateFolder($foldername) { if( !is_dir($foldername) ){ mkdir($foldername,0777); } } # 检测文件大小 function chk_fileSize() { if( $this->upfile_size > $this->maxSize ){ $this->msg("目标文件不能大于". $this->maxSize/1024 ." KB"); } } # 删除文件($filePath 文件相对路径) function Deletefile($filePath) { if( !is_file($filePath) ){ return false; } else{ $ending = @unlink($filePath); return $ending; } } /* 函数:生成缩略图 MakeBuild("images/a.jpg","news/b.jpg","100"); 参数: echo $BuildFile; 原图 带路径 echo $newFile; 生成的缩略图 带路径 echo $File_width; 缩略图宽度值 echo $File_height; 缩略图高度值 (默认为宽度的比例值) echo $rate; 缩略图象品质; */ function MakeBuild($BuildFile,$newFile,$File_width,$File_height=0,$rate=100) { if(!is_file($BuildFile)){ $this->msg("文件 ".$BuildFile." 不是一个有效的图形文件!/n/n系统无法生成该文件的缩略图!"); return false; } $data = GetImageSize($BuildFile); switch($data[2]){ case 1: $im = @ImageCreateFromGIF($BuildFile); break; case 2: $im = @ImageCreateFromJPEG($BuildFile); break; case 3: $im = @ImageCreateFromPNG($BuildFile); break; } if(!$im){ return false; } else{ $srcW=ImageSX($im); # 取得原图宽度; $srcH=ImageSY($im); # 取得原图高度; $dstX=0; $dstY=0; if($File_height==0){ $File_height = $File_width/$srcW*$srcH; } if ($srcW*$File_height>$srcH*$File_width){ $fFile_height = round($srcH*$File_width/$srcW); $dstY = floor(($File_height-$fFile_height)/2); $fFile_width = $File_width; } else { $fFile_width = round($srcW*$File_height/$srcH); $dstX = floor(($File_width-$fFile_width)/2); $fFile_height = $File_height; } $ni = ImageCreateTrueColor($File_width,$File_height); $dstX = ($dstX<0)?0:$dstX; $dstY = ($dstX<0)?0:$dstY; $dstX = ($dstX>($File_width/2))?floor($File_width/2):$dstX; $dstY = ($dstY>($File_height/2))?floor($File_height/s):$dstY; ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fFile_width,$fFile_height,$srcW,$srcH); ImageJpeg($ni,$newFile,$rate); # 生成缩略图; imagedestroy($im); # imagedestroy(resource) 释放image关联的内存 } } } ?> Copy after login |

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

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

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
