0

I'm trying to add functionality like if any user upload image two function occur

  1. Image resizes to mentioned requirement
  2. Image of any extension changed to .png extension.

This is my controller file

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

This is my model file

    <?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);
    }
}
?>  
  • 3
    I can't see any code in there related to loading an image and resizing it, then saving it as a png? – Basic Jul 13 '14 at 13:31
  • What code I need to add to resize image and change to .png extension. Resize function in controller file, is that code of any use? or should I discard it? –  Jul 13 '14 at 13:50
  • Can you please, make changes in my given code. I'm beginner in codeigniter, don't know much about image manipulation techniques. –  Jul 13 '14 at 14:55
  • 1
    Sorry, We're not going to develop your code for you. We'll point you at examples and tutorials but it's up to you to understand and develop your code. – Basic Jul 13 '14 at 20:29
  • This question appears to be off-topic because it asks us to write code for the OP. That makes it 2 for 2 "gimme codez" requests from this new poster on his first day… impressive. – msw Aug 20 '14 at 14:52

1 Answers1

0

Ok, you've broadly got 2 options...

  • Convert the image on upload: Only has to be done once, converted image is saved on the server (possibly with the original too). Then you just serve it normally like any other image.
  • Image is converted on demand: Uses more Cpu (run every time the image is displayed), but allows more flexibility - eg if you decide you want the images bigger, you don't have to go back and reprocess

I wrote a non-code-igniter PHP thumbnailer for an answer a while back. All the code required to load an image, resize it and serve it as a PNG is in there. It uses the GD+ library which is usually built into PHP (but not always)

Note that you could convert it on demand and save the output in a cache to use next time. This is equivalent to doing it on upload, except that it saves a step. The downside is that you need a mechanism to clear the cache at an appropriate time.

Community
  • 1
  • 1
Basic
  • 26,321
  • 24
  • 115
  • 201