0

I have a script to add band name + picture of band. This is my controller:

public function addBand()
{
    $config['upload_path']   = './uploads/bands';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['encrypt_name'] = TRUE;
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('picture')) {
        $error = array('error' => $this->upload->display_errors());
        $this->session->set_flashdata('error', $error);
        $this->load->view('concerts/index');
    }

    else {

        $upload_data = $this->upload->data();
        $file_name = $upload_data['file_name'];

        $data = array(
            'bName' => $this->input->post('name'),
            'bPicture' => $file_name
        );

        $this->band_model->insertBand($data);
        $this->session->set_flashdata('success', "Groupe ajouté");
        redirect('');
    }
}

This controller work nice! So. Now i add Cropper plugin in jquery. I get the value of "#imagebase64" and he return me the good result. After, i put a code to create picture from this data64. He work nice to. Now i want to add this code with Codeigniter.. and its dont work:

public function addBand()
{
    $config['upload_path']   = './uploads/bands';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['encrypt_name'] = TRUE;
    $this->load->library('upload', $config);

    $img64 = $this->input->post('imagebase64');
    $exploded = explode(',', $img64, 2);
    $encoded = $exploded[1];
    $decoded = base64_decode($encoded);
    $img_handler = imagecreatefromstring($decoded);
    $finalImg = imagepng($img_handler, "png.png");

    if ( ! $this->upload->do_upload('picture')) {
        $error = array('error' => $this->upload->display_errors());
        $this->session->set_flashdata('error', $error);
        $this->load->view('concerts/index');
    }

    else {

        $finalImg = $this->upload->data();
        $file_name = $finalImg['file_name'];

        $data = array(
            'bName' => $this->input->post('name'),
            'bPicture' => $file_name
        );

        $this->band_model->insertBand($data);
        $this->session->set_flashdata('success', "Groupe ajouté");
        redirect('');
    }
}

When i make var_dump of $finalImg juste before "$finalImg= $this->upload->data();" i have "boolean: 1" , when i make the var_dump juste after the $file_name = $finalImg['file_name']; i have:

'file_name' => string 'eca69e25e70c469e73a5bfa114c5a87d.png' (length=36)
  'file_type' => string 'image/png' (length=9)
  'file_path' => string 'C:/wamp64/www/concerts/uploads/bands/' (length=37)
  'full_path' => string 'C:/wamp64/www/concerts/uploads/bands/eca69e25e70c469e73a5bfa114c5a87d.png' (length=73)
  'raw_name' => string 'eca69e25e70c469e73a5bfa114c5a87d' (length=32)
  'orig_name' => string 'Sans_titre-1.png' (length=16)
  'client_name' => string 'Sans titre-1.png' (length=16)
  'file_ext' => string '.png' (length=4)
  'file_size' => float 368.2
  'is_image' => boolean true
  'image_width' => int 784
  'image_height' => int 419
  'image_type' => string 'png' (length=3)
  'image_size_str' => string 'width="784" height="419"' (length=24)

So, how can i generated picture in PHP and upload this picture with codeigniter ? Thanks!

9
  • $this->upload->data(); returns all the details of file uploaded,
    – Pradeep
    May 29, 2018 at 22:36
  • What doesn't work? Do you get an error message from the upload library? Further, I don't think you even need the upload library as you are doing your own logic with base64. The library needs a file not a string! So you could save your base64 decoded image as a file and just use that.
    – Alex
    May 30, 2018 at 0:37
  • Hello! I want codeigniter librairie to use the encrypt file, allowed file, and upload path :/
    – TiboK
    May 30, 2018 at 7:01
  • can you post base 64 code?? May 30, 2018 at 10:04
  • @SachinAghera To decrypt the picture ? Its on my post (2nd code)
    – TiboK
    May 30, 2018 at 10:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.