Thinkphp implements image upload function sharing _ PHP-php Tutorial

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 large 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.

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.