Codeigniter新闻图片调整大小

时间:2017-11-03 03:23:55

标签: codeigniter

如何调整新闻图片的大小?我永远不可能。

我的问题[source_image]

我使用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";
        }
    }   
}   

1 个答案:

答案 0 :(得分:0)

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

控制器:

<?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');
        }
    }
}   
?>

我希望它会帮助你。