Question

i have a problem while uploading and resizing images in a loop.

can anyone please provide me the working sample of code of codeigniter for uploading and resizing at the same time in a loop.

I want to upload and resize images uploaded from the form. There will be more than 1 images so i have to upload them in loop.

My code first uploads the image then resizes it. 1ts images is uploaded and resized correctly but during 2nd loop the image is uploaded but not resized. It throws this error:

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

I have tried the clear function too

$this->image_lib->clear(); 

can anyone please help

Was it helpful?

Solution

Dont load image_lib multiple times. Add image_lib in autoload libs and change

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

to

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

OTHER TIPS

I had the same problem but this seemed to work:

    // Do upload
    if (! $this->upload->do_upload($image_name)) {
        // return errors
         return array('errors' => $this->upload->display_errors());
    }

    $data = $this->upload->data();

    $config_manip = array(
        'image_library' => 'gd2',
        'source_image' => "./assets/uploads/{$data['file_name']}",
        'new_image' => "./assets/uploads/thumbs/{$data['file_name']}",
        'create_thumb' => true,
        'thumb_marker' => '',
        'maintain_ratio' => true,
        'width' => 140,
        'height' => 140
    );
    // Create thumbnail
    $this->load->library('image_lib');
    $this->image_lib->resize();
    $this->image_lib->clear();
    $this->image_lib->initialize($config_manip);

    if ( ! $this->image_lib->resize()){
        return array('errors' => $this->image_lib->display_errors());
    }

Notice the Create Thumbnail goes: Load Library, Resize Image, CLEAR, Initialize Config

I was putting the clear after the initialize which was causing the same error you're getting.

Here is a working code from my image gallery controller. This function uploads a batch of images, resizes them and saves them to database.

public function create_photo_batch() {      
$this->load->library('image_lib');
$this->load->library('upload');

// Get albums list for dropdown
$this->data['albums']   = $this->gallery_m->get_albums_list();

if(isset($_FILES['images']) && $_FILES['images']['size'] > 0) {
  $album_id = $this->input->post('albumid');
  $images = array();
  $ret = array();

  // Upload
  $files = $_FILES['images'];
  foreach ($files['name'] as $key => $image) {
    $_FILES['images[]']['name']= $files['name'][$key];
    $_FILES['images[]']['type']= $files['type'][$key];
    $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
    $_FILES['images[]']['error']= $files['error'][$key];
    $_FILES['images[]']['size']= $files['size'][$key];

    $upload_config = array(
      'allowed_types' => 'jpg|jpeg|gif|png',
      'upload_path' => realpath(APPPATH . "../uploads/gallery"),
      'max_size'    => 5000,
      'remove_spaces' => TRUE,
      'file_name'   => md5(time())
    );
    $this->upload->initialize($upload_config);

    if ($this->upload->do_upload('images[]')) {
      $image_data = $this->upload->data();  
      $images[] = $image_data['file_name']; 
    } else {
      $this->session->set_flashdata('error', $this->upload->display_errors() );
      redirect('admin/gallery/create_photo_batch');
    }
  }

  // Resize
  foreach ($images as $image) {
        $resize_config = array(
            'source_image'      => realpath(APPPATH . '../uploads/gallery') .'/'. $image,
            'new_image'             => realpath(APPPATH . '../uploads/gallery/thumbs'),
            'maintain_ratio'    => TRUE,
            'width'                     => 500,
            'height'                    => 500
        );
        $this->image_lib->initialize($resize_config);

        if ( ! $this->image_lib->resize() ) {
      echo $this->image_lib->display_errors(); 
      die;
    }
        $this->image_lib->clear();
  }

  // Save to db
  foreach ($images as $image) {
    $ret[] = array(
      'AlbumID' => (int) $album_id, 
      'Url' => $image,
      'IsActive' => 1
    );
  }    

  $this->gallery_m->save_images($ret);
  redirect('admin/gallery/album/'.$album_id);       
}

//Load view
$this->data['subview'] = 'admin/gallery/photo_upload_batch_view';
$this->load->view('admin/_layout_main', $this->data);

}

Your error message suggest that it's not the loop that is the problem, but rather that the 2nd file is of a different filetype than the 1st. And that the underlying server don't have the needed libraries (http://www.libgd.org/Main_Page) installed to handle that file type.

$config['image_library'] = 'gd2';

        $config['source_image'] = './assets/upload_images/A.jpg';
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width']         = 1600;
        $config['height']       = 900;
        $config['new_image']       = './assets/upload_images/'.$randy;

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

I had same problem but solved these by this code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top