<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Api_upload {

    public function upload_imgs() {
        $img_info = array();
        $imgs_all = array();
        if (!empty($_FILES)) {
            //上传单张图不裁剪，多张图剪为正方形
            if (count($_FILES) === 1) {
                $need_to_cut = 'no';
            } else {
                $need_to_cut = 'yes';
            }
            foreach ($_FILES as $field_name => $file) {
                $ext = substr(strrchr($file['name'], '.'), 1);
                if ($ext === 'imgz') {
                    $ext = 'jpg';
                }
                $img_info = $this->upload_one_img($field_name, $ext, $need_to_cut);
                if (!empty($img_info)) {
                    $imgs_all[] = $img_info;
                }
            }
        }
        return $imgs_all;
    }

    public function upload_one_img($field_name, $ext, $need_to_cut){
        $config = array();
        $this->ci = &get_instance();
        $path = 'uploads/sharecircle/' . date('Ym') . '/' . date('d') . '/';
        $config['upload_path'] = FCPATH . $path;
        if (!is_dir($config['upload_path'])) {
            mkdir($config['upload_path'],0777,true);
        }
        $config['allowed_types']   = '*';
        $this->ci->load->helper('url');
        $base_url = base_url();
        $config['file_name'] = rand(1,1000) . '.' . $ext;
        $this->ci->load->library('upload', $config);
        //保存原图
        if ( ! $this->ci->upload->do_upload($field_name))
        {
            $error = array('error' => $this->ci->upload->display_errors());
            return false;
        } else {
            $this->ci->load->library('image_lib');
            $data = $this->ci->upload->data();
            $url = $base_url.$path.$data['file_name'];
            $img_local = FCPATH.$path.$data['file_name'];
            list($width, $height, $type, $attr) = getimagesize($img_local);
            //生成缩略图，裁剪
            $thumconfig['image_library'] = 'gd2';
//            $thumconfig['create_thumb'] = TRUE;
            $thumconfig['maintain_ratio'] = TRUE;
            $thumconfig['source_image'] = FCPATH.$path.$data['file_name'];
            $thumconfig['new_image'] = $data['file_name'] . '.small.jpg';
            if($width >= $height)
            {
                $thumconfig['master_dim'] = 'height';
            }else{
                $thumconfig['master_dim'] = 'width';
            }
            $thumconfig['width']     = 300;
            $thumconfig['height']   = 300;
//            $this->ci->load->library('image_lib', $thumconfig);
            $this->ci->image_lib->initialize($thumconfig);
            $this->ci->image_lib->resize();
//            echo $data['file_name']."\n";

            if ($need_to_cut === 'yes') {
                $thumconfig['maintain_ratio'] = FALSE;
                $thumconfig['source_image'] = $img_local . '.small.jpg';
                if($width >= $height)
                {
                    $thumconfig['x_axis'] = floor(($width * 300 / $height - 300)/2);
                }else{
                    $thumconfig['y_axis'] = floor(($height * 300 / $width - 300)/2);
                }
                $this->ci->image_lib->initialize($thumconfig);
                $this->ci->image_lib->crop();
            }

            return array(
                'width' => $width,
                'height' => $height,
                'url' => $url
            );
        }
    }

}