Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php基于CodeIgniter实现图片上传、剪切功能的方法

php基于CodeIgniter实现图片上传、剪切功能的方法

墨辰丷
墨辰丷asal
2018-06-02 11:17:371446semak imbas

这篇文章主要为大家详细介绍了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微信公众平台开发之微信群发信息实例分享

Atas ialah kandungan terperinci php基于CodeIgniter实现图片上传、剪切功能的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn