图像大小调整不工作在codeigniter php

cyej8jka  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(229)

我有一个codeigniter网站,有多个图像上传,我的控制器如下所示:

$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();



               }
               }

图像正在上传,但调整大小没有发生,任何人都可以请告诉我什么是错的,在这里,提前感谢

lb3vh1jj

lb3vh1jj1#

尝试以下更正:

$this->load->library('upload');
$this->load->library('image_lib');

$image = array();
$ImageCount = count($_FILES['pimage']['name']);

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

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

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

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];

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

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

        $this->image_lib->initialize($config);
        if (!$this->image_lib->resize()) {
            echo $this->image_lib->display_errors();
        }

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

相关问题