首頁 >後端開發 >php教程 >php基於CodeIgniter實現圖片上傳、剪下功能的方法

php基於CodeIgniter實現圖片上傳、剪下功能的方法

墨辰丷
墨辰丷原創
2018-06-02 11:17:371479瀏覽

這篇文章主要為大家詳細介紹了php基於CodeIgniter實現圖片上傳、剪切功能,具有參考價值,有興趣的朋友可以參考一下

具體內容如下

<?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手機簡訊驗證碼實作流程詳解

php微信公眾平台開發之微信群發資訊實例分享

以上是php基於CodeIgniter實現圖片上傳、剪下功能的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn