繁体   English   中英

图像调整大小在CodeIgniter 3中不起作用

[英]Image resize not working in CodeIgniter 3

我正在开发一个Web应用程序。 在我的应用程序中,我将图像上传到服务器,然后调整大小。 我成功上传到服务器。 但是,当我调整图像大小时,它不会调整图像大小。 但它也不会抛出错误。

这是我的图像文件上传模型

class file_helper extends CI_Model{

    function __construct()
    {
        parent::__construct();
        $this->load->library('image_lib');   
    }

    function generateRandomFileName()
    {
        $rand = rand(10000,99999);
        $file_name = time().$rand;
        return time().$rand;
    }

    function uploadImage()
    {
        $name = $this->generateRandomFileName();
        $config['upload_path']          = './'.UPLOAD_FOLDER.'/';
        $config['allowed_types']        = 'gif|jpg|png|jpeg';
        $config['file_name'] = $name;

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

        if ( !$this->upload->do_upload('userfile'))
        {
            return FALSE;
        }
        else
        {
            $data = $this->upload->data();
            $data['virtual_path'] = UPLOAD_FOLDER."/".$name.".".$data['file_ext'];
            return $data;
        }
    }


    function resizeImage($path)
    {
        $config['image_library'] = 'gd2';
        $config['source_image'] = '/'.$path;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']         = 300;
        $config['height']       = 50;

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

        if ( ! $this->image_lib->resize())
        {
            print_r($this->image_lib->display_errors());
        }
        else{
            echo "Ok";
        }
    }

}

正如您在模型中看到的那样,我在成功时打印出“Ok”,而print_r则表示失败时出错。 我通过的路径是这样的“uploads / 23344545.png”。 但是,调整大小功能总是打印出“确定”,但图像不会调整大小。 我的代码出了什么问题?

您需要在上传时调整图像大小

function uploadImage()
{
    $name = $this->generateRandomFileName();
    $config['upload_path'] = './'.UPLOAD_FOLDER.'/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['file_name'] = $name;
    $config['width'] = 300;
    $config['height'] = 50;

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

    if ( !$this->upload->do_upload('userfile'))
    {
        return FALSE;
    }
    else
    {
        $data = $this->upload->data();
        $data['virtual_path'] = UPLOAD_FOLDER."/".$name.".".$data['file_ext'];
        return $data;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM