Home  >  Article  >  Backend Development  >  How to implement image upload and cut functions in PHP based on CodeIgniter

How to implement image upload and cut functions in PHP based on CodeIgniter

墨辰丷
墨辰丷Original
2018-06-02 11:17:371325browse

This article mainly introduces the image uploading and cutting functions of PHP based on CodeIgniter in detail. It has reference value. Interested friends can refer to it.

The specific content is as follows

load->helper(array('form', 'url'));
  }

  /**
   * 首页
   */
  public function index() {
    $this->load->view('upload_form', array('error' => ' ' ));
  }
  
  
 public function do_upload()
  {
    $config['upload_path']   = './data/uploads/';
    $config['allowed_types']  = 'gif|jpg|png';
    $config['max_size']   = 100;
    $config['max_width']    = 1024;
    $config['max_height']    = 768;

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('userfile'))
    {
      $error = array('error' => $this->upload->display_errors());

      $this->load->view('upload_form', $error);
    }
    else
    {
      $data = array('upload_data' => $this->upload->data());
      
      $this->load->library('image_lib');      
   list($width, $height) = getimagesize($data['upload_data']['full_path']);
   $config['image_library'] = 'gd2';
   $config['source_image'] = $data['upload_data']['full_path'];
   $config['maintain_ratio'] = TRUE;
   if($width >= $height)
   {
     $config['master_dim'] = 'height';
   }else{
     $config['master_dim'] = 'width';
   }
   $config['width'] = 180;
   $config['height'] = 180;
   $this->image_lib->initialize($config);
   $this->image_lib->resize();
 
   $config['maintain_ratio'] = FALSE;
   if($width >= $height)
   {
     $config['x_axis'] = floor(($width * 180 / $height - 180)/2);
   }else{
     $config['y_axis'] = floor(($height * 180 / $width - 180)/2);
   }
   $this->image_lib->initialize($config);
   $this->image_lib->crop();
   
      $this->load->view('upload_success', $data);
    }
  }
}

Summary: The above is the entire content of this article. I hope it will be helpful to everyone's study.

Related recommendations:

PHP implements multiple image upload and single image upload functions

Detailed explanation of the PHP mobile phone SMS verification code implementation process

phpWeChat public platform development WeChat group messaging example sharing

The above is the detailed content of How to implement image upload and cut functions in PHP based on CodeIgniter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn