Welcome Guest, Not a member yet? Register   Sign In
uploading and manipulating images
#1

[eluser]brian88[/eluser]
Here is a function that uploads a bunch of images. Everything works fine besides a bug in the image manipulation where i highlighted the code below.
There are 8 file uploads. So users can upload 8 images and then i want to resize each one of those images.

Take out the image manipulation and all the images upload just fine. But with the highlighted code below it only uploads the first image and resizes it. doesnt do anymore uploads

Code:
$data['num_of_images'] = 8;
// if user presses submit button
  if($this->input->post()){

   // get the name of the truck
   $db_data['name'] = $this->input->post('name');
  
   // format the truck name to be a folder name
   $folder = str_replace( " ", "", strtolower($this->input->post('name')) );
  
   // if the name of the truck folder does not currently exist.
   if(!is_dir('assets/images/'.$folder)){

    // make a folder based off the name of the truck
    mkdir('assets/images/'.$folder, 0777);

    // config for file uploads
    $config['upload_path'] = FCPATH . 'assets/images/' . $folder;
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $this->load->library('upload', $config);
    
    // upload users submitted images
    for ($i=1; $i<=$data['num_of_images']; $i++){
     // change file name in folder
     $config['file_name']  = $folder . '-' . $i;
     $this->upload->initialize($config);
     // if user uploaded an image
     if ( !empty($_FILES["file" . $i]['name']) ){
      // upload file
      if($this->upload->do_upload("file".$i)){
       $upload_data = $this->upload->data();

       // store name for database
       $db_data['image'.$i] = $folder.'/'.$upload_data['file_name'];


       // error: resize only image happens for the first image, and only 1st image uploads
// -----------------------------------
       if( $this->input->post('dontResize') != 'true'){
        $config = array(
         'source_image' => $upload_data['full_path'],
         'maintain_ration' => true,
         'create_thumb' => true,
         'master_dim' => 'width',
         'quality' => 60,
         'width' => 800,
         'height' => 600
        );
        
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
       }
// -----------------------------------
// end resize

      }else{ // error
       echo $this->upload->display_errors();
      } // end if do_upload
     } // end if empty file upload
    } // end for each image

    // add to database
    $this->main_mod->addTruck($db_data);

    // refresh the page to see view new data added.
    redirect('/');
   }else{
    // The truck name folder exist. Need a new name for the folder.
    echo 'Error: This truck name already exist. Choose a different name.';
   }
#2

[eluser]Karman de Lange[/eluser]
can you please supply the view as well please or the output of print_r($this->input->post())
thanks

L:
#3

[eluser]CroNiX[/eluser]
You need to clear() the image_lib if you are using it in a loop as per the user guide.
You shouldn't be loading the image_lib in a loop. Once is enough. Just image_lib::clear() and image_lib::initialize($config) in the loop.
#4

[eluser]Otemu[/eluser]
Auto load the library, then use:

Code:
$this->image_lib->initialize($config);
instead of
Code:
$this->load->library('image_lib', $config);
#5

[eluser]brian88[/eluser]
thanks guys, i move the image_lib loader out of the loop then i discovered that my upload and image_lib libraries both had the same variable $config.

I change my image_lib config to $configResize and it worked!
#6

[eluser]Karman de Lange[/eluser]
[quote author="brian88" date="1340281619"]thanks guys, i move the image_lib loader out of the loop then i discovered that my upload and image_lib libraries both had the same variable $config. [/quote]

HAHA , been there, done that!!
#7

[eluser]Unknown[/eluser]
[quote author="Otemu" date="1340276040"]Auto load the library, then use:

Code:
$this->image_lib->initialize($config);
instead of
Code:
$this->load->library('image_lib', $config);
[/quote]

Yesterday I spent 3 hours on this and couldn't figure it out.
This did the trick. Thanks a lot!




Theme © iAndrew 2016 - Forum software by © MyBB