CodeIgniter Forums
Image handling and upload - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Image handling and upload (/thread-57537.html)



Image handling and upload - El Forum - 03-21-2013

[eluser]ibnclaudius[/eluser]
I have this form submit, everything works fine, I'm having problem handling the picture, for example, how can I check if the image is a square before uploading it?

Code:
$data['name'] = trim($this->input->post('name'));
$data['username'] = trim($this->input->post('username'));
$data['address'] = trim($this->input->post('address'));

if (!$data['name'] OR !$data['username'] OR !$data['address'])
{
    $data['error'] = 'Preencha todos os campos.';
}
elseif ($data['username'] != $this->session->userdata('username') && $this->nightclubs_model->get_nightclub($data['username']))
{
    $data['error'] = 'Este usuário já existe.';
}
else
{
    $this->load->library('upload');
    $this->upload->initialize(array(
        'upload_path' => './uploads/nightclubs/pictures',
        'allowed_types' => 'gif|jpg|png',
        'file_name' => sha1($this->session->userdata('id')),
        'overwrite' => TRUE
    ));

    if (!$this->upload->do_upload('picture'))
    {
        die($this->upload->display_errors());
    }
    else
    {
        $picture = $this->upload->data();
    }

    $this->settings_model->update_profile($this->session->userdata('id'), $data['name'], $data['username'], $data['address']);

    $this->session->set_userdata('name', $data['name']);
    $this->session->set_userdata('username', $data['username']);
    $this->session->set_userdata('address', $data['address']);

    $data['success'] = 'Seu perfil foi alterado com sucesso!';
}



Image handling and upload - El Forum - 03-21-2013

[eluser]TheFuzzy0ne[/eluser]
You can't check that the image is a square before uploading it without some kind of Flash or Java applet embedded on the page.

Once the file is uploaded, you can either crop it to a square using the image library, or use getimagesize() to get the image dimensions and see if it's square.

Another observation, it would be wiser to call $this->session->set_userdata() once. If you are storing your sessions in the database, or ever decide to, an update will be fired at the database once for each call to $this->session->set_userdata().

Code:
$this->session->set_userdata(array(
    'name' => $data['name'],
    'username' => $data['username'],
    'address' => $data['address'],
));



Image handling and upload - El Forum - 03-21-2013

[eluser]ibnclaudius[/eluser]
[quote author="TheFuzzy0ne" date="1363894881"]You can't check that the image is a square before uploading it without some kind of Flash or Java applet embedded on the page.

Once the file is uploaded, you can either crop it to a square using the image library, or use getimagesize() to get the image dimensions and see if it's square.

Another observation, it would be wiser to call $this->session->set_userdata() once. If you are storing your sessions in the database, or ever decide to, an update will be fired at the database once for each call to $this->session->set_userdata().

Code:
$this->session->set_userdata(array(
    'name' => $data['name'],
    'username' => $data['username'],
    'address' => $data['address'],
));
[/quote]

Thanks! Another thing, is there any way I can check if there's a file to upload? Something like:

Code:
if (theres_file_to_upload)
{
    // run this only if the user selected a file to upload
}