PHP support to generate thumbnail file upload Code _php tutorial

Source: Internet
Author: User
Tags save file
PHP tutorial support to generate thumbnail file upload code
?>




<title>Untitled Document</title>


Class Upfileclass {

var $upfile, $upfile _name, $upfile _size;
# $upfile temporary filename $_files[' tmp_name '), $upfile _name filename $_files[' name '], $upfile _size file size $_files[' size '];

var $new _upfile_name; # The file name after uploading;
var $fleth, $fileextent; # file extension (type);
var $f 1, $f 2, $f 3; # File save path (multilevel) upfiles/2008-01/08/;
var $filename; # file (with path);
var $filepath; #相对路径用来删除文件;
var $maxsize, $file _type; # Allow upload file size allows uploading of file types;

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"; # allowed file types to upload
}

# Create new file name (original filename)
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;
}

# Detect 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 ("file does not match". $this->file_type. "Format!");
}
}

# Prompt error message and terminate 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 if upload results are successful
function Chk_savefile () {
$copymsg = Copy ($this->upfile, ".. /". $this->filename);
if ($copymsg) {
return $this->filename;
}
else{
$this->msg ("File upload failed! nn please re-upload! ");
}
}

# Create a 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 ("Destination file cannot be greater than". $this->maxsize/1024. "KB");
}
}

# Delete Files ($filepath file relative path)
function DeleteFile ($filepath) {
if (!is_file ($filepath)) {
return false;
}
else{
$ending = @unlink ($filepath);
return $ending;
}
}

/*
Functions: Generating thumbnails
Makebuild ("/www.bkjia.c0m/a.jpg", "news/b.jpg", "100");
Parameters:
Echo $buildfile; Original with Path
Echo $newfile; Generated thumbnail with path
echo $file _width; Thumbnail width value
echo $file _height; Thumbnail height value (default is the width of the scale value)
Echo $rate; thumbnail image quality;
*/
function Makebuild ($buildfile, $newfile, $file _width, $file _height=0, $rate =100) {
if (!is_file ($buildfile)) {
$this->msg ("file". $buildfile. "is not a valid graphics file! The NN system cannot generate thumbnails for this 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 original width;
$srch = Imagesy ($im); # get the original height;
$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 thumbnails;
Imagedestroy ($im); # Imagedestroy (Resource) releases the memory associated with the image
}
}

}
?>


http://www.bkjia.com/PHPjc/633037.html www.bkjia.com true http://www.bkjia.com/PHPjc/633037.html techarticle PHP Tutorial support to generate 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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.