TBC
TBC

Reputation: 89

Image resize not working in Codeigniter PHP

I have a Codeigniter website with multiple image upload, my controller is like below:

$this->load->library('upload');
$image      = array();
$ImageCount = count($_FILES['pimage']['name']);
for ($i = 0; $i < $ImageCount; $i++) {
    $_FILES['file']['name']     = $_FILES['pimage']['name'][$i];
    $_FILES['file']['type']     = $_FILES['pimage']['type'][$i];
    $_FILES['file']['tmp_name'] = $_FILES['pimage']['tmp_name'][$i];
    $_FILES['file']['error']    = $_FILES['pimage']['error'][$i];
    $_FILES['file']['size']     = $_FILES['pimage']['size'][$i];

    // File upload configuration
    $uploadPath              = './uploads/products/';
    $config['upload_path']   = $uploadPath;
    $config['allowed_types'] = 'jpg|jpeg|png|gif';

    // Load and initialize upload library
    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    // Upload file to server
    if ($this->upload->do_upload('file')) {
        // Uploaded file data
        $imageData = $this->upload->data();
        // $uploadImgData[$i]['image_name'] = $imageData['file_name'];
        $uploadImgData[] = $imageData['file_name'];

        $data      = array('upload_data' => $this->upload->data());
        $path      = $data['upload_data']['full_path'];
        $q['name'] = $data['upload_data']['file_name'];

        $config['image_library']  = 'gd2';
        $config['source_image']   = $path;
        $config['maintain_ratio'] = true;
        $config['width']          = 320;
        $config['height']         = 230;

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

        unset($config);
        $this->load->library('image_lib');
        $this->image_lib->clear();
    }
}

Image is getting uploaded but resize is not happening, can anyone please tell me what is wrong in here?

Upvotes: 0

Views: 50

Answers (1)

Druvi Shah
Druvi Shah

Reputation: 99

$this->load->library('upload');
$image = array();
$ImageCount = count($_FILES['pimage']['name']);
$this->load->library('image_lib'); // Move this outside the loop

$config['image_library'] = 'gd2'; // Set the image library
$config['maintain_ratio'] = true;
$config['width'] = 320;
$config['height'] = 230;

$this->image_lib->initialize($config); // Initialize the image library

for ($i = 0; $i < $ImageCount; $i++) {
    $_FILES['file']['name'] = $_FILES['pimage']['name'][$i];
    $_FILES['file']['type'] = $_FILES['pimage']['type'][$i];
    $_FILES['file']['tmp_name'] = $_FILES['pimage']['tmp_name'][$i];
    $_FILES['file']['error'] = $_FILES['pimage']['error'][$i];
    $_FILES['file']['size'] = $_FILES['pimage']['size'][$i];

    // File upload configuration
    $uploadPath = './uploads/products/';
    $config['upload_path'] = $uploadPath;
    $config['allowed_types'] = 'jpg|jpeg|png|gif';

    // Load and initialize upload library
    $this->upload->initialize($config);

    // Upload file to server
    if ($this->upload->do_upload('file')) {
        // Uploaded file data
        $imageData = $this->upload->data();
        $uploadImgData[] = $imageData['file_name'];

        $path = $imageData['full_path'];
        $q['name'] = $imageData['file_name'];

        $this->image_lib->clear(); // Clear any previous image manipulation

        $config['source_image'] = $path;
        $this->image_lib->initialize($config); // Initialize again with new config

        $this->image_lib->resize(); // Resize the image

        // ... continue with other operations or store the resized image name
    }
}

Upvotes: 0

Related Questions