Codeigniter上传文件并调整大小

时间:2015-05-18 17:04:28

标签: codeigniter upload resize

我正在尝试调整预先上传的图像的大小,然后使用FTP将它们发送到另一台服务器,但它似乎无法正常工作。上传工作正常,ftp工作正常,但每当我下载图像并检查大小时,它与上传的文件一样。

这是我的控制者:

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

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

            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/devices/'.$image;
            $config['maintain_ratio'] = TRUE;
            $config['width']    = 400;
            $config['height']   = 300;

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

            $localPath = './uploads/devices/'.$image;
            $remotePath = 'webspace/httpdocs/uploads/devices/'.$image;

            $this->load->library('ftp');
            $config['hostname'] = '';
            $config['username'] = '';
            $config['password'] = '';
            $config['port']     = 21;
            $config['passive']  = TRUE;
            $this->ftp->connect($config);
            $this->ftp->upload($localPath, $remotePath);
            $this->ftp->close();
        }

我想要实现的是上传图片,调整大小并替换它,然后上传调整后的图像。

非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

最终编辑:

使用initialize传递配置而不是直接将它们传递给load-> library:

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

        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/devices/'.$image;
        $config['maintain_ratio'] = TRUE;
        $config['width']    = 400;
        $config['height']   = 300;

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

        $this->image_lib->resize();

        $localPath = './uploads/devices/'.$image;
        $remotePath = 'webspace/httpdocs/uploads/devices/'.$image;

        $this->load->library('ftp');
        $config['hostname'] = '';
        $config['username'] = '';
        $config['password'] = '';
        $config['port']     = 21;
        $config['passive']  = TRUE;
        $this->ftp->connect($config);
        $this->ftp->upload($localPath, $remotePath);
        $this->ftp->close();
    }