Codeigniter uploads multiple files and creates multiple thumbnails

Source: Internet
Author: User
Tags codeigniter
This article describes how to use Codeigniter to upload multiple files and create multiple thumbnails. For more information, see the CI framework.

This program can achieve:
1. Upload 5 images at the same time
2. generate two types of thumbnails at the same time
3. save to mysql

Controllers: upload. php file:

The code is as follows: Class Upload extends Controller {
Function go (){
If (isset ($ _ POST ['go']) {
// Initialization
$ Config ['upload _ path'] = 'Album/source ';
$ Config ['allowed _ types'] = 'gif | jpg | png | bmp | jpeg ';
$ Config ['encryption _ name'] = TRUE;
$ Config ['remove _ spaces'] = TRUE;
$ Config ['max _ size'] = '0 ';
$ Config ['max _ width'] = '0 ';
$ Config ['max _ height'] = '0 ';

$ This-> load-> library ('upload', $ config );

// 170*170 images
$ ConfigThumb = array ();
$ ConfigThumb ['image _ library'] = 'gd2 ';
$ ConfigThumb ['Source _ image'] = '';
$ ConfigThumb ['create _ thumb'] = TRUE;
$ ConfigThumb ['maintain _ ratio '] = TRUE; // preserve the image proportion
$ ConfigThumb ['new _ image'] = 'Album/thumb ';
$ ConfigThumb ['width'] = 170;
$ ConfigThumb ["height"] = 170;
// 600*600 images
$ ConfigLarge = array ();
$ ConfigLarge ['image _ library'] = 'gd2 ';
$ ConfigLarge ['Source _ image'] = '';
$ ConfigLarge ['create _ thumb'] = TRUE;
$ ConfigLarge ['maintain _ ratio '] = TRUE; // preserve the image proportion
$ ConfigLarge ['new _ image'] = 'Album/large ';
$ ConfigLarge ['width'] = 600;
$ ConfigLarge ['height'] = 600;

$ This-> load-> library ('image _ lib ');

For ($ I = 1; $ I <6; $ I ++ ){
$ Upload = $ this-> upload-> do_upload ('image'. $ I );
If ($ upload = FALSE) continue;
$ Data = $ this-> upload-> data (); // returns an array of all relevant information of the uploaded file.
$ Uid = $ this-> session-> userdata ('uid ');
$ UploadedFiles [$ I] = $ data;

If ($ data ['is _ image'] = 1 ){
// Initialize 170*170
$ ConfigThumb ['Source _ image'] = $ data ['full _ path']; // file path with file name
$ This-> image_lib-> initialize ($ configThumb );
$ This-> image_lib-> resize ();
// Initialize 600*600
$ ConfigLarge ['Source _ image'] = $ data ['full _ path']; // file path with file name
$ This-> image_lib-> initialize ($ configLarge );
$ This-> image_lib-> resize ();
}

// Insert the image information to the album table. the inserted file name is the source directory file name.
$ Picture = array (
'Filename' => $ data ['File _ name'],
'Albumid' => $ this-> uri-> segment ),
'Uid' => $ this-> session-> userdata ('uid '),
'Dateline '=> time (),
'Describe' => '',
'Click' => 0
);

$ This-> load-> model ('Album _ Model ');
$ This-> album_model-> AddPic ($ picture );
$ Picture = array ();
}
}

/* Transfer */
$ AlbumID = $ this-> uri-> segment (4 );
$ Backurl = site_url (). 'photo/editpic/album/'. $ albumID;
$ This-> session-> set_flashdata ('MSG ', 'image uploaded successfully .');
Redirect ($ backurl, 'refresh ');
}
}

Views: new_pic.view file:

The code is as follows:

Note the following:

1. to upload several files at a time, modify the parameters of the loop section in the form and controller.
2. album \ source is the Directory of the uploaded source image. large and thumb run $ this-> image_lib-> resize () twice.
3. if the thumbnail file name must be consistent with the album \ source directory, add the parameter $ config ['thumb _ marker'] = '';
4. $ picture this part of the array is saved to the database, you don't need to worry about it.

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.