Welcome Guest, Not a member yet? Register   Sign In
Processing Multiple Images?
#1

[eluser]Pawel K[/eluser]
I have been trying to use the image library to resize an image in to a 400x400 thumbnail as well as a 200x200 thumb. I am able to do only the first image, I would appreciate any help in understanding why this is happening.

Code:
$image_info = $this->upload->data();
                    
                    $uploaded_name = $image_info['raw_name'];
                    $uploaded_ext = $image_info['file_ext'];
                    
                    chmod($image_info['full_path'], 0777);
                    
                    $thumb_name = $image_info['raw_name']."-thumb".$image_info['file_ext'];
                    $big_name = $image_info['raw_name']."-main".$image_info['file_ext'];
                    
                    #Main Image
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $image_info['full_path'];
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = $this->config->item('img_width');
                    $config['height'] = $this->config->item('img_height');
                    $config['new_image'] = $big_name;
                    
                    
                    $this->load->library('image_lib', $config);
    
                    $this->image_lib->resize();
                    
                    #Thumb Image
                    $config['image_library'] = 'gd';
                    $config['source_image'] = $image_info['file_path'].$big_name;
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = FALSE;
                    $config['width'] = $this->config->item('thumb_width');
                    $config['height'] = $this->config->item('thumb_height');
                    $config['new_image'] = $thumb_name;
                    
                    $this->load->library('image_lib', $config);
    
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    
                    echo "Image 1 errors:".$this->image_lib->display_errors()."<br>";
                    
                    #Post Data
                    $prod_name = $this->input->post('product_name');
                    $prod_description = $this->input->post('product_desc');
                    $prod_price = $this->input->post('product_price');
                    $prod_cat = $this->input->post('category');


Only the first thumbnail (main) gets created. The second one doesn't. No errors show up.
#2

[eluser]pickupman[/eluser]
User guide is your friend.

Code:
$this->image_lib->clear();

Quote: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.
#3

[eluser]Pawel K[/eluser]
I should have said I used that but had same issue although an error would popup that the library doesnt support your image type:

Code:
else
                {
                    
                    $image_info = $this->upload->data();
                    
                    $uploaded_name = $image_info['raw_name'];
                    $uploaded_ext = $image_info['file_ext'];
                    
                    chmod($image_info['full_path'], 0777);
                    
                    $thumb_name = $image_info['raw_name']."-thumb".$image_info['file_ext'];
                    $big_name = $image_info['raw_name']."-main".$image_info['file_ext'];
                    
                    #Main Image
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $image_info['full_path'];
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = $this->config->item('img_width');
                    $config['height'] = $this->config->item('img_height');
                    $config['new_image'] = $big_name;
                    
                    
                    $this->load->library('image_lib', $config);
    
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    
                    #Thumb Image
                    $config['image_library'] = 'gd';
                    $config['source_image'] = $image_info['file_path'].$big_name;
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = FALSE;
                    $config['width'] = $this->config->item('thumb_width');
                    $config['height'] = $this->config->item('thumb_height');
                    $config['new_image'] = $thumb_name;
                    
                    $this->load->library('image_lib', $config);
    
                    $this->image_lib->resize();
                    
                    
                    echo "Image 1 errors:".$this->image_lib->display_errors()."<br>";
                    
                    #Post Data
                    $prod_name = $this->input->post('product_name');
                    $prod_description = $this->input->post('product_desc');
                    $prod_price = $this->input->post('product_price');
                    $prod_cat = $this->input->post('category');
                    
                    #Get Cat lft/rgt values
                    $cat_get_query = $this->db->query("SELECT * FROM category WHERE id=$prod_cat");
                    $cat_get_row = $cat_get_query->row();
                    $cat_left = $cat_get_row->lft;
                    $cat_right = $cat_get_row->rgt;

                    $this->db->query("INSERT INTO products (name, description, image, image_ext, price, cat_id, lft, rgt) VALUES ('$prod_name', '$prod_description', '$uploaded_name', '$uploaded_ext', '$prod_price', '$prod_cat', '$cat_left', '$cat_right')");
                    
                    #Load Successful Page
                    $this->load->view('admin_add_success');
                }

Erorr: Your server does not support the GD function required to process this type of image.
#4

[eluser]pickupman[/eluser]
Shouldn't it be:
Code:
$this->image_lib->clear();

#Thumb Image
$config['image_library'] = 'gd2'; //not gd

You have it right for the first image, but a typo for the 2nd.
#5

[eluser]Pawel K[/eluser]
It should be, that was part of me debugging. gd2 does the same thing.
#6

[eluser]pickupman[/eluser]
Can you var_dump the uploaded data to see if the image resources are still available after being processed the first time?
#7

[eluser]Pawel K[/eluser]
Not sure what you mean by var_dump. print_r($image_info); after the last image is processed produces:

Code:
Array (
[file_name] => 1273890547.jpg
[file_type] => image/jpeg [file_path] => /home/----//public_html/cart/images/products/
[full_path] => /home/----//public_html/cart/images/products/1273890547.jpg
[raw_name] => 1273890547
[orig_name] => 1273890547.jpg
[file_ext] => .jpg
[file_size] => 25.88
[is_image] => 1
[image_width] => 400
[image_height] => 300
[image_type] => jpeg
[image_size_str] => width="400" height="300"
)

echo $config['source_image']; produces:

Code:
/home/----/public_html/cart/images/products/1273890641-main.jpg

The file 1273890641-main.jpg is on the server in the right place with permissions 777.

I'm not sure if this is what you are looking for, my apologies if it isn't.
#8

[eluser]atlanta[/eluser]
Pawel K did you ever find out anything on this ?
#9

[eluser]atlanta[/eluser]
Hey Pawel K,

I finally found it on the 2nd, 3rd , etc calls instead of doing

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

do

$this->image_lib->initialize($config);

and that will load the new config files.
#10

[eluser]Unknown[/eluser]
Thank you! This thread helped solve my image resizing woes with the CI 2.0 image manipulation class. I was trying to resize a few images in a loop and the docs weren't very clear about how to accomplish this. Here's how I did it:

Code:
$this->load->library('image_lib');

$imgs = array('img_1.jpg', 'img_2.jpg', 'img_3.jpg');

foreach($imgs as $img)
{
  $config['source_image'] = $img;
  $config['width'] = 800;
  $config['height'] = 600;
  $config['new_image'] = '/resized/' . $img;

  $this->image_lib->initialize($config);
  $this->image_lib->resize();
  $this->image_lib->clear();
}




Theme © iAndrew 2016 - Forum software by © MyBB