4

Im having a problem with codeigniter image resize. I upload the file as a zip file then unzip, after unzipping I scan the dir to look for a .jpg. if its a .jpg extension it needs to be resize. It works when the zip file has only one .jpg image but its not working when the zip file has 2 pr more .jpg image files.

It does scan all the jpg files problem is its not resizing.

I wanted to resize all the jpg files when I upload the zip file.

here is my code:

$images = scandir('uploads/new');
//print_r($images);
foreach($images as $image){
    $last = substr($image, -3);
    if($last == 'jpg'){
        $image_path = './uploads/new/'.$image;

        //$config['image_library'] = 'gd2';
        $config['source_image'] = $image_path;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 100;
        $config['height'] = 100;

        $this->load->library('image_lib', $config);

        $this->image_lib->resize(); 
    }
}       

2 Answers 2

2

You should use $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();

From : http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

Also a hint:

No need to use string functions to get your file extension. You can use something that's actually designed for what you want: pathinfo():

$ext = pathinfo($image, PATHINFO_EXTENSION);

In your case :

if(pathinfo($image, PATHINFO_EXTENSION) == 'jpg'){

This way, if you would ever add an extension with more then 3 letters it would work ;)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks its working now. thanks also for the additional advice about the pathinfo function. cheers!
0

try clearing your config in the foreach loop

$images = scandir('uploads/new');
//print_r($images);
foreach($images as $image){
    $this->image_lib->clear(); // clear previous config
    $last = substr($image, -3);
    if($last == 'jpg'){
        $image_path = './uploads/new/'.$image;

        //$config['image_library'] = 'gd2';
        $config['source_image'] = $image_path;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 100;
        $config['height'] = 100;

        $this->load->library('image_lib', $config);

        $this->image_lib->resize(); 
    }
}       

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.