Search code examples
phpcodeigniterimagelibrary

Failed to create thumb image in $_FILES loop


I iterate through $_FILES to create thumb images of uploaded pictures. It works fine for the first image but fails for the following pictures. Do I miss to add a special line or there is flow in my code?

Note: Original files gets uploaded successfully and exist in folder before creating thumb out of them.

When I echo error, I get this: "Your server does not support the GD function required to process this type of image.". When I upload it on its own, it works!!!!

Thanks

public function upload_image()
{
    $config['upload_path']      = './web/uploads/images/';
    $config['allowed_types']    = 'gif|jpg|png';
    $config['max_size']         = 5120;
    $config['max_width']        = '0';
    $config['max_height']       = '0';
    $config['encrypt_name']     = true;

    $this->load->library('upload');
    $this->upload->initialize($config);     

   foreach ($_FILES as $file => $value)
   {
      $this->upload->do_upload($file);

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

      if ($this->manipulate_image($result['file_name']) === false)
      {
          echo 'Failed to create thumb for the image ' . $value['name'] . '<br />';
      }
   }
}

    public function manipulate_image($file_name)
    {
        $config['image_library']    = 'gd2';
        $config['source_image']     = './web/uploads/images/' . $file_name;
        $config['create_thumb']     = true;
        $config['maintain_ratio']   = false;
        $config['width']            = 100;
        $config['height']           = 100;
        //$config['master_dim']     = 'width';
        $config['thumb_marker']     = '_thumb';

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

        if (! $this->image_lib->resize())
        {
            $this->image_lib->clear();
            return false;
        }

        $this->image_lib->clear();
        return true;
    }

Solution

  • I see two things, first, I would move the load on the library outside of the foreach loop then use initialize inside the loop to set the config:

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

    Also, as documented here you can use

    echo $this->image_lib->display_errors();
    

    To get more insight on your problem