1. We first need to create a table
Copy the Code code as follows:
CREATE TABLE IF not EXISTS ' Tp_image ' (
' id ' int (one) not NULL auto_increment,
' Image ' varchar (+) not NULL,
' Create_time ' int (one) is not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8;
2. Then add the configuration in the Conf file (the last configuration is optional, just to facilitate the unified management of the URL path)
Copy the Code code as follows:
Return Array (
' Url_model ' + 2,//If your environment does not support PathInfo please set to 3
' Db_type ' = ' mysql ',
' db_host ' = ' localhost ',
' Db_name ' = ' thinkphp ',
' Db_user ' = ' root ',
' Db_pwd ' = ',
' Db_port ' = ' 3306 ',
' Db_prefix ' = ' tp_ ',
' Show_page_trace ' =>true,//Display page debug details
' tmpl_parse_string ' + array (//address substitution, use the _upload_ directory instead of the UPLOAD directory under the root directory
' __upload__ ' = __root__. ' /uploads ',
),
);
?>
3. Add an image module (name can be taken casually)
Copy the Code code as follows:
Class Imageaction extends action{
/**
* Create INDEX Entry method
*/
Public Function index () {
$image =m (' image ');
$data = $image->order (' create_time desc ')->find (); Get last uploaded image
$this->assign (' data ', $data);
$this->display ();
}
?>
4. Create the appropriate index view file (index.html)
Copy the Code code as follows:
Insert Title here
Upload allowed file type: gif png jpg image file, and generate 2 thumbnails, where a large image with a watermark, generated will delete the original picture.
5. Select the image, click the Upload button, will jump to the image module upload method, the image module does not have this method now, so we create it
Copy the Code code as follows:
Class Imageaction extends action{
/**
* Create INDEX Entry method
*/
Public Function index () {
$image =m (' image ');
$data = $image->order (' create_time desc ')->find (); Get last uploaded image
Var_dump ($data);
$this->assign (' data ', $data);
$this->display ();
}
If the uploaded file is not empty, jump to the _upload method
Public function upload () {
If it is not empty
if (!empty ($_files))
{
$this->_upload ();
}
}
6. If the commit is not NULL, then skip to the _upload method, this method implements the function of picture uploading
Copy CodeThe code is as follows:
Class Imageaction extends action{
/**
* Create INDEX Entry method
*/
Public Function index () {
$image =m (' image ');
$data = $image->order (' create_time desc ')->find (); Get last uploaded image
Var_dump ($data);
$this->assign (' data ', $data);
$this->display ();
}
If the uploaded file is not empty, jump to the _upload method
Public function upload () {
If it is not empty
if (!empty ($_files))
{
$this->_upload ();
}
}
/***
* Image upload is implemented
*/
Public Function _upload () {
Import (' @.org.uploadfile ');
Import Upload Class
$upload = new UploadFile ();
Set Upload file size
$upload->maxsize = 3292200;
Set Upload file type
$upload->allowexts = Explode (', ', ' jpg,gif,png,jpeg ');
Set Attachments Upload Directory
$upload->savepath = './uploads/';
Settings need to generate thumbnails, only valid for image files
$upload->thumb = true;
Set the reference Picture Class Library package path
$upload->imageclasspath = ' @.org.image ';
Set the file suffix to generate thumbnails
$upload->thumbprefix = ' m_,s_ '; Production of 2 thumbnail pictures
Set thumbnail maximum width
$upload->thumbmaxwidth = ' 400,100 ';
Set thumbnail maximum height
$upload->thumbmaxheight = ' 400,100 ';
Set upload file rules
$upload->saverule = ' uniqid ';
Delete Original
$upload->thumbremoveorigin = true;
If the upload is unsuccessful
if (! $upload->upload ())
{
Capturing upload Exceptions
$this->error ($upload->geterrormsg ());
}
Else
{
Get file information for successful uploads
$uploadList = $upload->getuploadfileinfo ();
Import Picture Class
Import (' @.org.image ');
Add watermark to m_ thumbnail, image::water (' Original file path ', ' watermark Image address ')
Image::water ($uploadList [0][' Savepath ']. ' M_ '. $uploadList [0][' Savename '], App_path. ' Tpl/public/images/logo.png ');
Image name assignment to field image
$_post[' image ' = $uploadList [0][' Savename '];
}
$model = M (' image ');
Save current data Object
$data [' image '] = $_post[' image '];
$data [' create_time '] = now_time;
$list = $model->add ($data);
if ($list!== false)
{
$this->success (' Upload picture Success! ');
}
Else
{
$this->error (' Upload image failed! ');
}
}
}
?>
Upload successfully generated two thumbnail images
It should be stated that:
thinkphp in the Picture upload Class (UploadFile.class.php) and Picture model class (Image.class.php), to the full version of the thinkphp package.
There is no need to create a folder in Lib (ORG), and then go to the official website to download the extension package to put the two files in the ORG folder.
Mine is the second case.