Myke Solidum
Myke Solidum

Reputation: 127

codeigniter image resize

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(); 
    }
}       

Upvotes: 4

Views: 1445

Answers (2)

Timmetje
Timmetje

Reputation: 7694

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 ;)

Upvotes: 2

This_is_me
This_is_me

Reputation: 918

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(); 
    }
}       

Upvotes: 0

Related Questions