Thinkphp implements the image upload function _ php instance

Source: Internet
Author: User
Image Upload is a common function on websites. ThinkPHP also has its own Image Upload class (UploadFile. class. php) and Image model class (Image. class. php ). The following describes how to implement the image upload function. 1. Create a table first.

The Code is as follows:


Create table if not exists 'tp _ image '(
'Id' int (11) not null AUTO_INCREMENT,
'Image' varchar (200) not null,
'Create _ time' int (11) 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 unified management of the URL path)

The Code is as follows:


Return array (
'Url _ model' => 2, // if your environment does not support PATHINFO, set it to 3
'Db _ type' => 'mysql ',
'Db _ host' => 'localhost ',
'Db _ name' => 'thinkphp ',
'Db _ user' => 'root ',
'Db _ pwd' => '',
'Db _ port' => '123 ',
'Db _ prefix' => 'tp _',

'Show _ PAGE_TRACE '=> true, // display page debugging details

'Tmpl _ PARSE_STRING '=> array (// address replacement, replace the UPLOAD directory under the root directory with the _ Upload _ directory
'_ UPLOAD _' =>. '/Uploads ',
),
);
?>

3. Add an Image module (the name can be obtained at will)

The Code is as follows:


Class ImageAction extends Action {

/**
* Method for creating an index Entry
*/
Public function index (){
$ Image = M ('image ');
$ Data = $ image-> order ('create _ time desc ')-> find (); // get the last uploaded image
$ This-> assign ('data', $ data );
$ This-> display ();
}
?>

4.create the corresponding indexvideo file (index.html)

The Code is as follows:






Insert title here



Upload allowed file type: gif. png. jpg image file, and generate two thumbnails. The larger image contains a watermark. After the image is generated, the original image is deleted.







5. Select an Image and click the upload button. It will jump to the upload method of the Image module. This method is not available in the Image module, So we create it.

The Code is as follows:


Class ImageAction extends Action {

/**
* Method for creating an index Entry
*/
Public function index (){
$ Image = M ('image ');
$ Data = $ image-> order ('create _ time desc ')-> find (); // get the 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 not empty
If (! Empty ($ _ FILES ))
{
$ This-> _ upload ();
}

}

6. If the submitted object is not NULL, jump to the _ upload method. This method uploads images.

The Code is as follows:


Class ImageAction extends Action {

/**
* Method for creating an index Entry
*/
Public function index (){
$ Image = M ('image ');
$ Data = $ image-> order ('create _ time desc ')-> find (); // get the 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 not empty
If (! Empty ($ _ FILES ))
{
$ This-> _ upload ();
}

}


/***
* Uploads images.
*/
Public function _ upload (){
Import ('@. ORG. uploadfile ');
// Import upload class
$ Upload = new UploadFile ();
// Set the size of the uploaded file
$ Upload-> maxSize = 3292200;
// Set the Upload File Type
$ Upload-> allowExts = explode (',', 'jpg, gif, png, jpeg ');
// Set the attachment upload directory
$ Upload-> savePath = './Uploads /';
// Set the thumbnail to be generated, which is only valid for image files
$ Upload-> thumb = true;
// Set the path of the referenced Image Library package
$ Upload-> imageClassPath = '@. ORG. image ';
// Set the file suffix for generating thumbnails
$ Upload-> thumbPrefix ='m _, s _ '; // produce two thumbnails.
// Set the maximum width of the thumbnail.
$ Upload-> thumbMaxWidth = '000000 ';
// Set the maximum height of the thumbnail.
$ Upload-> thumbMaxHeight = '000000 ';
// Set file upload rules
$ Upload-> saveRule = 'uniqid ';
// Delete the source Image
$ Upload-> thumbRemoveOrigin = true;


// If the upload fails
If (! $ Upload-> upload ())
{
// Capture upload exceptions
$ This-> error ($ upload-> getErrorMsg ());
}
Else
{
// Obtain the information of the successfully uploaded file
$ UploadList = $ upload-> getUploadFileInfo ();


// Import the image class
Import ('@. ORG. image ');

// Add a watermark to the 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 ');

// Assign the image name to the image field
$ _ POST ['image'] = $ uploadList [0] ['savename'];
}
$ Model = M ('image ');
// Save the current data object
$ Data ['image'] = $ _ POST ['image'];
$ Data ['create _ time'] = NOW_TIME;
$ List = $ model-> add ($ data );
If ($ list! = False)
{
$ This-> success ('image uploaded successfully! ');
}
Else
{
$ This-> error ('image Upload Failed! ');
}
}
}
?>

Two thumbnails are generated successfully after the upload.

Note:

The built-in Image Upload class (UploadFile. class. php) and Image model class (Image. class. php) in ThinkPHP require the full version of the ThinkPHP package.

If not, create a folder (ORG) in Lib and download the extension package from the official website to put the two files in the ORG folder.

The second case is mine.

Related Article

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.