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

时间:2014-07-13 13:28:03

标签: php codeigniter

我试图添加功能,就像任何用户上传图像两个功能一样发生

  1. 图片调整为上述要求
  2. 任何扩展程序的图片都更改为.png扩展名。
  3. 这是我的控制器文件

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

1 个答案:

答案 0 :(得分:0)

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

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

我前一段时间写了一个non-code-igniter PHP thumbnailer的答案。加载图像,调整大小并将其作为PNG提供所需的所有代码都在那里。它使用GD +库,通常内置于PHP中(但并非总是如此)

请注意,您可以按需转换它并将输出保存在缓存中以供下次使用。这相当于在上传时执行,除了它保存了一个步骤。缺点是您需要一种机制来在适当的时间清除缓存。