Home  >  Article  >  Backend Development  >  PHP supports generating thumbnail file upload code_PHP tutorial

PHP supports generating thumbnail file upload code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:45:19781browse

php tutorial supports generating thumbnail file upload code
?>




Untitled Document


class upfileclass {

var $upfile, $upfile_name, $upfile_size;
# $upfile temporary file name $_files['tmp_name'], $upfile_name file name $_files['name'], $upfile_size file size $_files['size'];

var $new_upfile_name; # The name of the uploaded file;
var $fleth, $fileextent; # File extension (type);
var $f1, $f2, $f3; # File saving path (multi-level) upfiles/2008-01/08/;
var $filename; #File (with path);
var $filepath; #Relative path is used to delete files;
var $maxsize, $file_type; # The size of files allowed to be uploaded and the type of files allowed to be uploaded;

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; #File size 500kb
$this->file_type = "gif/jpg/jpeg/png/bmp"; # File types allowed to be uploaded
}

#Create new file name (original file name)
function createnewfilename($file_name){
$this->fleth = explode(".",$file_name);
$this->fileextent = $this->fleth[(int)count($this->fleth)-1]; # Get file suffix;
$tmps tutorial tr = date('ymd').rand(0,time()) . "." .$this->fileextent; # Create a new file name;
Return $tmpstr;
}

# Check whether the file type is correct
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("The file does not conform to the ".$this->file_type." format!");
}
}

# Prompt an error message and terminate the operation
function msg($error){
echo "n";
Die();
}

# Save file
function savefile(){
$this->chk_fileextent();
$this->chk_filesize();
$this->createfolder( "../".$this->f1 );
$this->createfolder( "../".$this->f2 );
$this->createfolder( "../".$this->f3 );
Return $this->chk_savefile();
}

# Check whether the upload result is successful
function chk_savefile(){
$copymsg = copy($this->upfile,"../".$this->filename);
If( $copymsg ){
Return $this->filename;
}
else{
$this->msg("File upload failed! nnPlease upload again! ");
}
}

# Create folder
function createfolder($foldername){
If( !is_dir($foldername) ){
mkdir($foldername,0777);
}
}

#Detect file size
function chk_filesize(){
If( $this->upfile_size > $this->maxsize ){
$this->msg("The target file cannot be larger than ". $this->maxsize/1024 ." kb");
}
}

#Delete file ($filepath file relative path)
function deletefile($filepath){
If( !is_file($filepath) ){
Return false;
}
else{
$ending = @unlink($filepath);
Return $ending;
}
}

/*
Function: Generate thumbnail
makebuild("/www.bKjia.c0m/a.jpg","news/b.jpg","100");
Parameters:
Echo $buildfile; Original image with path
Echo $newfile; Generated thumbnail with path
Echo $file_width; Thumbnail width value
Echo $file_height; Thumbnail height value (default is the proportion of width)
Echo $rate; Thumbnail image quality;
*/
function makebuild($buildfile,$newfile,$file_width,$file_height=0,$rate=100) {
If(!is_file($buildfile)){
$this->msg("The file ".$buildfile." is not a valid graphics file! The nn system cannot generate a thumbnail for the file!");
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); # Get the width of the original image;
$srch = imagesy($im); # Get the height of the original image;
$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); # Generate thumbnail;
imagedestroy($im); # imagedestroy(resource) Release the memory associated with image
}
}

}
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633037.htmlTechArticlephp tutorial supports generating thumbnail file upload code? !doctype html public -//w3c//dtd xhtml 1.0 transitional //en http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd html xmlns=ht...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn