Codeigniter图像调整大小并更改任何图像扩展名为.png扩展名


Codeigniter Image resize and change any image extension to .png extension

我正在尝试添加功能,如如果任何用户上传图像两个功能发生

  1. 图像大小调整到上述要求
  2. 任何扩展名更改为。png扩展名的图像。

这是我的控制器文件

    <?php
class Form extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }
    public function index() {
        $this->load->view('Form/form.php');
        $this->load->view('Home/header.php');
    }
    public function do_upload() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('Firstname', 'Firstname', 'required');
        $this->form_validation->set_rules('Middlename', 'Middlename');
        $this->form_validation->set_rules('Lastname', 'Lastname', 'required');
        $this->form_validation->set_rules('Fathername', 'Fathername', 'required');
        $this->form_validation->set_rules('Mothername', 'Mothername', 'required');
        $this->form_validation->set_rules('DOB', 'DOB', 'required');
        $this->form_validation->set_rules('Mobile', 'Mobile', 'required');
        $this->form_validation->set_rules('Postalcode', 'Postalcode', 'required');
        $this->form_validation->set_rules('userfile', 'Image', 'required');
        $this->form_validation->set_rules('Address', 'Address', 'required');
        $this->form_validation->set_rules('Branch', 'Branch', 'required');
        $this->form_validation->set_rules('Email', 'Email', 'required');
        $this->form_validation->set_rules('Classronum', 'ClassRollNumber', 'required');
        $this->form_validation->set_rules('Unironum', 'UniversityRollNumber', 'required');
        $this->form_validation->set_rules('Comment', 'Comment', 'required');
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']='1000';
        $config['max_width']='1024';
        $config['max_height']='1000';
        $this->load->library('upload', $config);
        $this->upload->initialize($config); 
        if ( ! $this->upload->do_upload() && $this->form_validation->run() == FALSE)
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('Form/form', $error);
            $this->load->view('Home/header.php');
        } else {
            $data = $this->upload->data();
            $this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
            $this->load->model('form_model');
            $this->load->view('Form/success', $data);
            $this->load->view('Home/header.php');   
            $this->form_model->form_fill($data['file_name']);
        }
    }
    public function resize($path, $file) {
        $config['image_library'] = 'ImageMagick';
        $config['library_path']='/usr/bin';
        $config['source_image']='uploads/'.$name.'.'.$m[1];
        $config['new_image']='uploads/'.$name.'.'.$png;
        $objImage = new CI_Image_lib($config);
        $config['source_image']=$path;
        $config['create_thumb']=TRUE;
        $config['maintain_ratio']=TRUE;
        $config['width']=150;
        $config['height']=75;
        $config['new_image']='./uploads/'.$file;
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
    }   
}
?>

这是我的模型文件

    <?php
class Form_model extends CI_Model {
    public function form_fill($file) {
        $this->load->database();
        $id = $this->input->post('id');
        $firstname = $this->input->post('Firstname');
        $middlename = $this->input->post('Middlename');
        $lastname = $this->input->post('Lastname');
        $fathername = $this->input->post('Fathername');
        $mothername = $this->input->post('Mothername');
        $dob = $this->input->post('DOB');
        $mobile = $this->input->post('Mobile');
        $postalcode = $this->input->post('Postalcode');
        $address = $this->input->post('Address');
        $photo = $this->input->post('userfile');
        $branch = $this->input->post('Branch');
        $email = $this->input->post('Email'); 
        $classrollno = $this->input->post('Classronum');
        $universityrollno = $this->input->post('Unironum');
        $comment = $this->input->post('Comment');           
        $data = array(
            'firstname' => $this->input->post('Firstname'),
            'middlename' => $this->input->post('Middlename'),
            'lastname' => $this->input->post('Lastname'),
            'fathername' => $this->input->post('Fathername'),
            'mothername' => $this->input->post('Mothername'),
            'dob' => $this->input->post('DOB'),
            'mobile' => $this->input->post('Mobile'),
            'postalcode' => $this->input->post('Postalcode'),
            'address' => $this->input->post('Address'),
            'photo' => $file,
            'branch' => $this->input->post('Branch'),
            'email' => $this->input->post('Email'),
            'classrollno' => $this->input->post('Classronum'),
            'universityrollno' => $this->input->post('Unironum'),
            'comment' => $this->input->post('Comment')
            );
        $this->db->insert('student', $data);
    }
}
?>  

好的,你大致有两个选择…

  • 在上传时转换图像:只需要做一次,转换后的图像保存在服务器上(可能与原始图像一起保存)。然后你就像处理其他图片一样处理它。
  • 图像按需转换:使用更多的Cpu(每次显示图像时运行),但允许更多的灵活性-例如,如果您决定要更大的图像,您不必返回并重新处理

我写了一个非代码点燃PHP缩略图的答案一段时间后。加载图像、调整图像大小并将其作为PNG格式提供所需的所有代码都在这里。它使用GD+库通常是PHP内置的(但不总是)

请注意,您可以根据需要转换它,并将输出保存在缓存中以供下次使用。这相当于在上传时这样做,只是它节省了一个步骤。缺点是需要一种机制在适当的时候清除缓存。