Welcome Guest, Not a member yet? Register   Sign In
Multiple image upload, resizes only once
#1

[eluser]Kristof[/eluser]
Code:
function upload_array_files()
        {
            // settings
            $config['upload_path']      = './uploads/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size'] = '1000';
            $config['max_width'] = '1920';
            $config['max_height'] = '1280';    
            
            $this->load->library('upload',$config);
            
            
        if(!$this->upload->do_upload('files')){
            echo $this->upload->display_errors();
        }
        else {
            $fInfo = $this->upload->data();
            #echo '<pre>'; print_r($fInfo); echo '</pre>'; // defaul
            for($i=0;$i<count($fInfo['file_name']);$i++){
                
                echo '$i = '.$i.'filename = '.$fInfo['file_name'][$i].'<br/>';
                $this->_createThumbnail($fInfo['file_name'][$i]);
            }
        }
    
        }
        function _createThumbnail($fileName) {
            //settings
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/' . $fileName;    
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 75;
            $config['height'] = 75;
            
            $this->load->library('image_lib', $config);        
            if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
                  
        }
The upload works great, 1 or 3 files, works fine but
he only does the _createThumbnail once (on the first file) and i don't see why, the echo is correct though. Anyone has an idea ?

thanks in advance
#2

[eluser]designfellow[/eluser]
Hi,

is it uploading every file?

Please put a loop to upload & resize each file separately.

Like..
foreach($files as $file)
{
$upload=$this->upload->do_upload($file);
if($upload)
{
$this->_createThumbnail($upload['file_name']);
}

}


I didn't checked this .

Please try to do similar to this.

I think it will work


Happy Coding,
DesignFellow
#3

[eluser]Kristof[/eluser]
yes it uploads every file that's the strange part.
Hmm ok i'll try


edit: thanks Smile
#4

[eluser]Kristof[/eluser]
Well guess i'm doing it wrong. Although i don't get any errors, nothing is uploading anymore
Code:
$arr = $_FILES['files'];
            foreach($arr as $file)
            {
                //$upload=$this->upload->do_upload('files');
                $upload=$this->upload->do_upload($file);
                print_r($file);
            if($upload)
              {
                      $fInfo = $this->upload->data();
                      $this->_createThumbnail($fInfo['file_name']);
              }
            
            }
        }

this doesn seem to work either:
Code:
foreach($_FILES as $key => $value){
              echo '<pre>'; print_r($key);echo '</pre>';
              $key['file_name'];
                    if( ! empty($key['file_name'])){
                        //$upload=$this->upload->do_upload('files');
                        
                        $upload=$this->upload->do_upload($value);
                        echo '<pre>'; print_r($value);echo '</pre>';
                        if($upload)    {
                                  $fInfo = $this->upload->data();
                                  $this->_createThumbnail($fInfo['file_name']);
                        }
                    }
            }
#5

[eluser]BrianDHall[/eluser]
Going back to the first code you posted that worked for everything except thumbnail, consider this:

Quote:$this->image_lib->clear()

The clear function resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.

$this->image_lib->clear();

Put the clear() call at the top of your thumbnail function and give that a try. Any change?
#6

[eluser]Kristof[/eluser]
Code:
function _createThumbnail($fileName) {
            $this->image_lib->clear();
            //settings
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/' . $fileName;    
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 75;
            $config['height'] = 75;
            
            $this->load->library('image_lib', $config);        
            if(!$this->image_lib->resize()) echo $this->image_lib->display_errors();
                  
        }

gives an error: A PHP Error was encountered

Severity: Notice

Message: Undefined property: Upload_files::$image_lib

Filename: controllers/upload_files.php

Line Number: 112

Fatal error: Call to a member function clear() on a non-object in /opt/www/duquesneh/web/ci.denieuwekantine.be/application/controllers/upload_files.php on line 112


so it still creates just 1 thumb , thanks for reply
#7

[eluser]BrianDHall[/eluser]
Oooo, whoops! Right, the object wasn't created to start with, silly me. OK, put image clear at the very end of the script, not the very top :\
#8

[eluser]BrianDHall[/eluser]
After further reading, something else to try. First, try loading the image library with no config information outside of your create thumbnail function, such as in the calling function with your file uploads.

Then in your create thumbnail function, call $this->image_lib->initialize($config) to set your configuration settings. Process your image, and then before turning from the function call $this->image_lib->clear().

I forgot, loading anything with $this->load functions can often cause a problem if you are doing it in a loop. Better to load it once, then initialize and clear through each iteration of a loop or function.
#9

[eluser]Kristof[/eluser]
[quote author="BrianDHall" date="1253653505"]After further reading, something else to try. First, try loading the image library with no config information outside of your create thumbnail function, such as in the calling function with your file uploads.

Then in your create thumbnail function, call $this->image_lib->initialize($config) to set your configuration settings. Process your image, and then before turning from the function call $this->image_lib->clear().

I forgot, loading anything with $this->load functions can often cause a problem if you are doing it in a loop. Better to load it once, then initialize and clear through each iteration of a loop or function.[/quote]

so someone took some time to recreate my script came with the exact copy of mine, but with your advice it worked. 3 rules differently , all the logic the same, who knew you needed to initialize the config and not load the library within the loop.

MANY THANKS !!!
#10

[eluser]BrianDHall[/eluser]
You are very welcome! But, be sure to remember this issue with only loading a model/helper/library once - it's a 'gotchya' that exists in all of CI and loading anything twice can often result in some very unexpected behavior. Views you can load all you want, but be careful about multiple loads of anything within a single script execution, it's bitten me and many others before too.

Good luck on the rest of your script!




Theme © iAndrew 2016 - Forum software by © MyBB