php实现图片上传、剪切功能

yipeiwu_com6年前PHP代码库

本文实例为大家详细介绍了php实现图片上传、剪切功能的具体代码,供大家参考,具体内容如下

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Index extends MY_Controller {
  function __construct(){
    parent::__construct();
    $this->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);
    }
  }
}

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

相关文章

10款实用的PHP开源工具

10款实用的PHP开源工具

在开发工作中,使用合适的工具可以最大化地提高效率。此外,大量的开源项目也节省了开发者重复“造轮”的时间,使得开发者可以专注于业务的实现。 本文介绍10款非常实用且开源的PHP开发辅助工具...

PHP定时执行计划任务的多种方法小结

PHP定时执行计划任务的多种方法小结

PHP定时执行的三种方式实现 1、windows 的计划任务 2、linux的脚本程序 3、让web浏览器定时刷新 具体实现 windows计划任务 PHP很少在win服务器上跑,具体实...

php实现网站文件批量压缩下载功能

利用php实现将文件批量压缩打包下载,这个过程中将使用到 ZipArchive 这个类,注意使用该类之前,linux需开启zlib,windows需取消php_zip.dll前的注释。下...

php !function_exists(&quot;T7FC56270E7A70FA81A5935B72EACBE29&quot;))代码解密

复制代码 代码如下: < ?php if (!function_exists("T7FC56270E7A70FA81A5935B72EACBE29")) { function T7...

PHP整合七牛实现上传文件

七牛支持抓取远程图片 API,用 access_key + secret_key + url 生成 access_token, 把 access_token 加在 header 里,然后...