Resize Code

I have a problem resizing the code. I download the file as a zip file, and then unpack it, after unpacking I scan the directory to find .jpg. if its extension .jpg needs to be resized. It works when the zip file has only one .jpg image, but it does not work when the zip file has 2 files larger than .jpg images.

Scanning all jpg files is not a resize.

I want to resize all jpg files when downloading a 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(); } } 
+4
source share
2 answers

You should use $ this-> image_lib-> clear ();

The clear function resets all values ​​used in image processing. You want to call it if you process images in a loop.

$ this-> image_lib-> clear ();

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

Also a hint:

You do not need to use string functions to get the file extension. You can use what is really intended for what you want: pathinfo ():

 $ext = pathinfo($image, PATHINFO_EXTENSION); 

In your case:

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

That way, if you added an extension with more than three letters, that would work;)

+2
source

try to clear configuration in 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(); } } 
0
source

All Articles