Codeigniter新闻图像大小调整

Codeigniter新闻图像大小调整,codeigniter,Codeigniter,如何调整新闻图片的大小?我永远也做不到 我的问题[来源图片] 我使用Codeigniter 3.1.6 控制器 public function insert_news() { $config['upload_path'] = 'uploads/news/'; $config['allowed_types'] = 'gif|jpg|png'; $config['encrypt_name'] = TRUE; $this->load

如何调整新闻图片的大小?我永远也做不到

我的问题[来源图片]

我使用Codeigniter 3.1.6

控制器

public function insert_news() {
    $config['upload_path']       = 'uploads/news/';
    $config['allowed_types']     = 'gif|jpg|png';
    $config['encrypt_name']      = TRUE;

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

    if ($this->upload->do_upload('news_image'))
    {
        $image      = $this->upload->data();
        $image_url  = $image['file_name'];
        $db_insert  ='upload/news/'.$image_url.'';

        $data=array(
            'news_title'    => $this->input->post('news_title'),
            'news_content'  => $this->input->post('news_content'),
            'news_image'    =>  $db_insert,
            'news_sef'=>sef($this->input->post('news_title'))
        );

        $this->load->model('vt');
        $result = $this->vt->insert_news($data);
        if ($result) {
            echo "yes";
        } else {
            echo "no";
        }
    }   
}   

您可以针对您的问题尝试以下解决方案:

控制器:

<?php
public function insert_news() {

    $config['upload_path']       = 'uploads/news/';
    $config['allowed_types']     = 'gif|jpg|png';
    $config['max_size'] = '20240000245';
    $config['overwrite'] = TRUE;
    $config['encrypt_name'] = TRUE;

    // You can change width & height
    $imgage_width= 60; 
    $imgage_height= 60;

    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if (!$this->upload->do_upload($field)) {
        $error = $this->upload->display_errors();
        // uploading failed. $error will holds the errors.
        $this->form_validation->set_message('error',$error);
        return FALSE;
    } else {
        $fdata = $this->upload->data();
        $configer =  array(
          'image_library'   => 'gd2',
          'source_image'    =>  $fdata['full_path'],
          'maintain_ratio'  =>  TRUE,
          'width'           =>  $imgage_width,
          'height'          =>  $imgage_height,
        );
        // Load Image Lib Library
        $this->load->library('image_lib');
        $this->image_lib->clear();
        $this->image_lib->initialize($configer);
        $this->image_lib->resize();
        $img_data ['path'] = $config['upload_path'] . $fdata['file_name'];

        // uploading successfull, now do your further actions
        $data=array(
            'news_title'    => $this->input->post('news_title'),
            'news_content'  => $this->input->post('news_content'),
            'news_image'    =>  $img_data ['path'],
            'news_sef'=>sef($this->input->post('news_title'))
        );
        $this->load->model('vt');
        $result = $this->vt->insert_news($data);
        if ($result) {
            $this->form_validation->set_message('success',"News has been added successfully.");
            redirect('contorller/action_name');
        } else {
            $this->form_validation->set_message('error',"Error in aading news");
            redirect('contorller/action_name');
        }
    }
}   
?>


我希望它能对您有所帮助。

您可以尝试以下解决方案:

控制器:

<?php
public function insert_news() {

    $config['upload_path']       = 'uploads/news/';
    $config['allowed_types']     = 'gif|jpg|png';
    $config['max_size'] = '20240000245';
    $config['overwrite'] = TRUE;
    $config['encrypt_name'] = TRUE;

    // You can change width & height
    $imgage_width= 60; 
    $imgage_height= 60;

    $this->load->library('upload', $config);
    $this->upload->initialize($config);
    if (!$this->upload->do_upload($field)) {
        $error = $this->upload->display_errors();
        // uploading failed. $error will holds the errors.
        $this->form_validation->set_message('error',$error);
        return FALSE;
    } else {
        $fdata = $this->upload->data();
        $configer =  array(
          'image_library'   => 'gd2',
          'source_image'    =>  $fdata['full_path'],
          'maintain_ratio'  =>  TRUE,
          'width'           =>  $imgage_width,
          'height'          =>  $imgage_height,
        );
        // Load Image Lib Library
        $this->load->library('image_lib');
        $this->image_lib->clear();
        $this->image_lib->initialize($configer);
        $this->image_lib->resize();
        $img_data ['path'] = $config['upload_path'] . $fdata['file_name'];

        // uploading successfull, now do your further actions
        $data=array(
            'news_title'    => $this->input->post('news_title'),
            'news_content'  => $this->input->post('news_content'),
            'news_image'    =>  $img_data ['path'],
            'news_sef'=>sef($this->input->post('news_title'))
        );
        $this->load->model('vt');
        $result = $this->vt->insert_news($data);
        if ($result) {
            $this->form_validation->set_message('success',"News has been added successfully.");
            redirect('contorller/action_name');
        } else {
            $this->form_validation->set_message('error',"Error in aading news");
            redirect('contorller/action_name');
        }
    }
}   
?>

我希望它能帮助你