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

墨辰丷
Release: 2023-03-29 13:52:02
Original
1466 people have browsed it

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

<?php
defined(&#39;BASEPATH&#39;) OR exit(&#39;No direct script access allowed&#39;);

class Index extends MY_Controller {
 function __construct(){
    parent::__construct();
    $this->load->helper(array(&#39;form&#39;, &#39;url&#39;));
  }

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

    $this->load->library(&#39;upload&#39;, $config);

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

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template