【问题标题】:Image resize not working in CodeIgniter 3图像调整大小在 CodeIgniter 3 中不起作用
【发布时间】:2016-12-31 02:40:30
【问题描述】:

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

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

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”,在失败时打印出错误。我通过的路径类似于“uploads/23344545.png”。但是该调整大小功能总是打印出“Ok”,但图像没有调整大小。我的代码有什么问题?

【问题讨论】:

标签: php image codeigniter


【解决方案1】:

上传时需要调整图片大小

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

【讨论】:

    猜你喜欢
    • 2014-08-29
    • 2023-03-31
    • 2012-04-11
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2014-05-13
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多