The php tutorial allows you to generate a 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 = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>
<Body>
<? Php
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; # Name of the uploaded file;
Var $ fleth, $ fileextent; # file extension (type );
Var $ f1, $ f2, $ f3; # file storage path (multilevel) upfiles/2008-01/08 /;
Var $ filename; # file (with Path );
Var $ filepath; # The relative path is used to delete objects;
Var $ maxsize, $ file_type; # the type of the file 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; # the file size is kb.
$ This-> file_type = "gif/jpg/jpeg/png/bmp"; # types of files that can be uploaded
}
# Create a new file name (original file name)
Function createnewfilename ($ file_name ){
$ This-> fleth = explode (".", $ file_name );
$ This-> fileextent = $ this-> fleth [(int) count ($ this-> fleth)-1]; # obtain the file suffix;
$ Tmps tutorial tr = date ('ymmd'). 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 match". $ this-> file_type. "format! ");
}
}
# Prompt the error message and terminate the operation
Function msg ($ error ){
Echo "<script language =" webpage effect "> n ";
Echo "alert ('". $ error. "'); n ";
Echo "window. history. back (); n ";
Echo "</script> n ";
Die ();
}
# Saving files
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! Nn upload again! ");
}
}
# Creating folders
Function createfolder ($ foldername ){
If (! Is_dir ($ foldername )){
Mkdir ($ foldername, 0777 );
}
}
# Check file size
Function chk_filesize (){
If ($ this-> upfile_size> $ this-> maxsize ){
$ This-> msg ("the target file cannot be greater than". $ this-> maxsize/1024. "kb ");
}
}
# Delete a file (relative path of $ filepath)
Function deletefile ($ filepath ){
If (! Is_file ($ filepath )){
Return false;
}
Else {
$ Ending = @ unlink ($ filepath );
Return $ ending;
}
}
/*
Function: generate a thumbnail.
Makebuild ("/www. bKjia. c0m/a.jpg", "news/B .jpg", "100 ");
Parameters:
Echo $ buildfile; original image path
Echo $ newfile; generated thumbnail with Path
Echo $ file_width; thumbnail width value
Echo $ file_height; thumbnail height value (default: width ratio)
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 graphic file! The nn system cannot generate a thumbnail of 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); # obtain the source image width;
$ Srch = imagesy ($ im); # obtain the height of the source 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, $ ffile_width, $ ffile_height, $ srcw, $ srch );
Imagejpeg ($ ni, $ newfile, $ rate); # generate a thumbnail;
Imagedestroy ($ im); # imagedestroy (resource) releases the memory associated with the image
}
}
}
?>
</Body>
</Html>