CodeIgniter Image Upload and Resize

CodeIgniter Image Upload and Resize

One common thing that most developers will want to do when creating a web application with CodeIgniter is to give their users the ability to upload a profile image. Your code should be able to resize that profile image to the standard size that your design calls for. CodeIgniter provides two great classes to help make this a piece of cake.

The two library classes we will be working with are the File Uploading and Image Manipulation classes. With these two classes and the CodeIgniter User Guide you can probably figure out most of what you need, but I'll make it even easier for you. First, we will start with what you need in the View. This will create your file upload field:

The View

$Fdata = array('name' => 'user_avatar', 'class' => 'file');
echo form_upload($Fdata);

Also, make sure your form is using 'form_open_multipart'. Now we will move on to the Controller, where the bulk of the code is. The process here is to configure the upload, do the upload, configure the image resizing and then do the image resize. Here's what you should end up with:

The Controller

if($_FILES['user_avatar']['error'] == 0){
	//upload and update the file
	$config['upload_path'] = './images/profile/';
	$config['allowed_types'] = 'gif|jpg|png';
	$config['overwrite'] = false;
	$config['remove_spaces'] = true;
	//$config['max_size']	= '100';// in KB
	
	$this->load->library('upload', $config);

	if ( ! $this->upload->do_upload('user_avatar'))
	{
		$this->session->set_flashdata('message', $this->upload->display_errors('

', '

')); redirect('profile'); } else { //Image Resizing $config['source_image'] = $this->upload->upload_path.$this->upload->file_name; $config['maintain_ratio'] = FALSE; $config['width'] = 40; $config['height'] = 40; $this->load->library('image_lib', $config); if ( ! $this->image_lib->resize()){ $this->session->set_flashdata('message', $this->image_lib->display_errors('

', '

')); } $this->MUser->updateProfile($this->input->post('user_id')); //Need to update the session information if email was changed $this->session->set_userdata('email', $this->input->xss_clean($this->input->post('user_email'))); $this->session->set_flashdata('message', '

Your Profile has been Updated!

'); redirect('profile'); } }

You may need to make some changes to a couple of the configuration options to get what you want. You'll also notice at the bottom that I update some of my user session information afterwards. Next up, we'll handle the updateProfile function within the User Model. This part is pretty simple:

The Model

function updateProfile($user_id){
	$profile_data = array(
	'name' => $this->input->xss_clean($this->input->post('user_name')),
	'email' => $this->input->xss_clean($this->input->post('user_email')),
	'role' => $this->input->xss_clean($this->input->post('user_role')),
	'website' => $this->input->xss_clean($this->input->post('user_website'))
	);
	//check if password was updated
	if($this->input->post('user_password') != ''){
		$profile_data['password'] = md5($this->input->xss_clean($this->input->post('user_password')));
	}
	if($_FILES['user_avatar']['error'] == 0){
		$relative_url = 'images/profile/'.$this->upload->file_name;
		$profile_data['avatar'] = $relative_url;
	}
	$this->db->where('id', $user_id);
	$this->db->update('user', $profile_data);
}

Normally you would want your path to pull from the configuration options you set using $upload['full_path'], but for some reason I've had issues pulling that data from the upload.

Tags: , , , ,

Delicious Digg StumbleUpon Reddit

5 Comments

  • April 1st, 2010 at 10:41 am

    Found this very helpful. Thanks for the write up.

  • Evan
    April 6th, 2010 at 9:25 am

    Great! I’m glad you found it useful.

  • April 18th, 2010 at 4:02 pm

    Thanks a million!!!

  • April 27th, 2010 at 3:33 am

    Recent days I’m developing websites with CI and it is really wonderful. I have question

    form_upload(‘userfile’);

    after submiting

    if ($this->input->post(‘userfile’)) doesn’t work. what is the problem?

    Waiting for reply

  • Evan
    May 3rd, 2010 at 7:20 pm

    @Nurdin - you need to use $_FILES[‘userfile’] instead. I don’t believe file inputs get placed within the input post array. Refer to line 1 of the Controller.

    You can also try $this->upload->data()

Add your 2 cents:

Remember my personal information

Notify me of follow-up comments?

Please enter the word you see in the image below: