0

I am using codeigniter 3.1 .
I want to crop image after upload but the crop not working.
After crop how to get the image url ?

if ($_FILES['upload']['size'] > 0) {
       $this->upload->initialize(array( 
                   "upload_path" => $this->upload_path,
                   "encrypt_name" => TRUE,
                   "remove_spaces" => TRUE,
                ));

                $data = $this->upload->data();
                $image = $data['file_name'];

        $this->load->library('image_lib');
        $this->image_lib->initialize(array( 
             "source_image" => $data,
             "new_image" => $this->upload_path. $image,
             "x_axis" => 300,
             "y_axis" => 300
                ));

        $data = $this->upload->data();
        $image = $data['file_name'];

  //Get full path of image

  }
Mehur
  • 131
  • 1
  • 3
  • 11
  • Just a friendly tip, you may want to read over this page: The [How-To-Ask Guide](https://stackoverflow.com/help/how-to-ask) so you can always be sure that your questions are easily answerable and as clear as possible. Be sure to include any efforts you've made to fix the problem you're having, and what happened when you attempted those fixes. Also don't forget to your show code and any error messages! – Matt C Oct 13 '16 at 15:50

1 Answers1

1

Use these functions in your model

<?php 
 class Image_model extends CI_Model {
public function __construct()   {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('upload');
    $this->load->library('image_lib');
}

public function do_resize($filename)
{

    $source_path =  'uploads/' . $filename;
    $target_path =  'uploads/thumb/'.$filename;

    $config_manip = array(

        'image_library' => 'gd2',
        'source_image' => $source_path,
        'new_image' => $target_path,
        'maintain_ratio' => TRUE,
        'width' => 300,
        'height' => 300
    );
    $this->image_lib->initialize($config_manip);
    $this->load->library('image_lib', $config_manip);


    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
        die();
    }
}

public function img_upload()
{
    $config = array(
        'upload_path' => "uploads",
        'allowed_types' => "*",
        'overwrite' => TRUE,
        'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        'max_height' => "3000",
        'max_width' => "3000"
    );
    $this->upload->initialize($config);
    $this->load->library('upload', $config);

    if($this->upload->do_upload('myFile')) { //<input type="file" name="myFile" />
        $response   =    array('upload_data' => $this->upload->data());
        $this->do_resize($response['upload_data']['file_name']);
        //return $response;
    }
    else{
        $error              =   array('error'=>$this->upload->display_errors());
        print_r($error);die(); 

    }
 }
}

Call this function in your controller like this

if(isset($_FILES)){

            $config                 =   $this->image_model->img_upload();
            $file_data              =   $this->upload->data();
            $data['img']            =   $file_data['file_name'];
        }
Muhammad Usman
  • 1,403
  • 13
  • 24