>  기사  >  백엔드 개발  >  CodeIgniter를 기반으로 PHP에서 이미지 업로드 및 잘라내기 기능을 구현하는 방법

CodeIgniter를 기반으로 PHP에서 이미지 업로드 및 잘라내기 기능을 구현하는 방법

墨辰丷
墨辰丷원래의
2018-06-02 11:17:371405검색

이 글은 CodeIgniter를 기반으로 한 PHP의 이미지 업로드 및 잘라내기 기능을 위주로 자세하게 소개하고 있으니 관심 있는 분들은 참고하시기 바랍니다.

구체적인 내용은 다음과 같습니다

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

요약: 위 내용은 전체 내용입니다. 이 글이 모든 분들의 공부에 도움이 되었으면 좋겠습니다.

관련 권장 사항:

PHP는 다중 이미지 업로드 및 단일 이미지 업로드 기능을 구현합니다.

PHP 휴대폰 SMS 인증 코드 구현 프로세스에 대한 자세한 설명

phpWeChat 공개 플랫폼 개발 WeChat 그룹 메시징 예제 공유

위 내용은 CodeIgniter를 기반으로 PHP에서 이미지 업로드 및 잘라내기 기능을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.