<?php
/**
 * 图片处理类
 *
 * @author sun
 */
class Pic_Wang_Test_model extends MY_Model {
    
    /**
     * 上传文件数组_FILES[$filename]的名称
     * @var string 
     */
    private $_filename = 'Filedata';
    
    /**
     * 上传文件允许的大小 默认3M
     * @var int  3M  
     */
    private $_max_size = 3072;
    
    /**
     * 文件保存的目录
     * @var string 
     */
    private $_save_dir = '/njhouse/';
    
    /**
     * 是否默认加水印
     * @var bollean 默认开启
     */
    private $_water_mark_status = true;
    
    /**
     * 是否缩放原图
     * @var bollean 
     */
    private $_image_cut_status = true;
    
    /**
     * 缩放图片宽度
     * @var int 
     */
    private $_image_width = 640;
    
    /**
     * 缩放图片高度
     * @var int 
     */
    private $_image_height = 480;
    
    /**
     * 是否生成调整图
     * @var bollean 默认开启
     */
    private $_resize_image_status = true;
    
    /**
     * 调整图片宽度
     * @var int 
     */
    private $_resize_width = 130;
    
    /**
     * 调整图片高度
     * @var int 
     */
    private $_resize_height = 100;
    
    /**
     * 后台审核用的缩略图
     * @var bollean 默认开启
     */
    private $_audit_status = true;
    
    /**
     * 缩略图宽度
     * @var int 
     */
    private $_audit_width = 200;
    
    /**
     * 缩略图高度
     * @var int 
     */
    private $_audit_height = 150;
    
    /**
     * 图片上附加内容 例如:此图片由某经纪人提供
     * @var string 
     */
    private $_other_string = '';
    
    /**
     * 上传的图片是否需要插入临时表中
     * @var string 
     */
    public $is_insert_temp_table = false;
    
    /**
     * 插入图片的临时表名
     * @var string 
     */
    private $_insert_temp_tablename = 'house_upload_provisional';
    
    /**
     * 返回的数据结果数组
     * @var array 
     */
    private $result = array('result' => '', 'msg' => '', 'data' => '');


	/**
    * Constructor
    */
    public function __construct() 
    {   
        parent::__construct(1,0,0);
    }
    
    /**
     * 设置$_FILE数组的名称
     * @param string $filename 名称
     */
    public function set_filename($filename)
    {
        $this->_filename = $filename;
    }
    
    /**
     * 获取$_FILE数组的名称
     * @return string
     */
    public function get_filename()
    {
        return $this->_filename;
    }
    
    /**
     * 设置上传文件允许的大小
     * @param int $max_size 文件大小
     */
    public function set_max_size($max_size)
    {
        $this->_max_size = $max_size;
    }
    
    /**
     * 获取上传文件允许的大小
     * @return int
     */
    public function get_max_size()
    {
        return $this->_max_size;
    }
    
    /**
     * 设置上传文件目录
     * @param string $save_dir 目录名称
     */
    public function set_save_dir($save_dir)
    {
        $this->_save_dir = $save_dir;
    }
    
    /**
     * 获取上传文件目录
     * @return string
     */
    public function get_save_dir()
    {
        return $this->_save_dir;
    }
    
    /**
     * 设置图片水印
     * @param string $water_mark_status 水印
     */
    public function set_water_mark($water_mark_status)
    {
        $this->_water_mark_status = $water_mark_status;
    }
    
    /**
     * 获取图片水印
     * @return string
     */
    public function get_water_mark()
    {
        return $this->_water_mark_status;
    }
    
    /**
     * 设置是否开启图片缩放功能
     * @param boolean $image_cut_status true or false
     */
    public function set_image_cut($image_cut_status)
    {
        $this->_image_cut_status = $image_cut_status;
    }

    /**
     * 获取是否开启图片缩放功能
     * @return boolean
     */
    public function get_image_cut()
    {
        return $this->_image_cut_status;
    }
    
    /**
     * 设置缩放图片的宽度
     * @param int $width 宽度
     */
    public function set_image_width($width)
    {
        $this->_image_width = $width;
    }

    /**
     * 获取缩放图片的宽度
     * @return int
     */
    public function get_image_width()
    {
        return $this->_image_width;
    }
    
    /**
     * 设置缩放图片的高度
     * @param int $height
     */
    public function set_image_height($height)
    {
        $this->_image_height = $height;
    }
    
    /**
     * 获取缩放图片的高度
     * @return int
     */
    public function get_image_height()
    {
        return $this->_image_height;
    }
    
    /**
     * 设置是否生成调整图
     * @param boolean $resize_image_status true or false
     */
    public function set_resize_image($resize_image_status)
    {
        $this->_resize_image_status = $resize_image_status; 
    }
    
    /**
     * 获取是否生成调整图
     * @return boolean
     */
    public function get_resize_image()
    {
        return $this->_resize_image_status;
    }
    
    /**
     * 设置生成调整图的宽度
     * @param int $width 宽度
     */
    public function set_resize_width($width)
    {
        $this->_resize_width = $width; 
    }
    
    /**
     * 获取生成调整图的宽度
     * @return int
     */
    public function get_resize_width()
    {
        return $this->_resize_image_status;
    }
    
    /**
     * 设置生成调整图的高度
     * @param int $height 高度
     */
    public function set_resize_height($height)
    {
        $this->_resize_height = $height; 
    }
    
    /**
     * 获取生成调整图的高度
     * @return int
     */
    public function get_resize_height()
    {
        return $this->_resize_height;
    }
    
    /**
     * 设置是否生成后台审核用的缩略图
     * @param boolean $audit_status true or false
     */
    public function set_audit($audit_status)
    {
        $this->_audit_status = $audit_status; 
    }
    
    /**
     * 获取是否生成后台审核用的缩略图
     * @return boolean
     */
    public function get_audit()
    {
        return $this->_audit_status;
    }
    
    /**
     * 设置后台审核用的缩略图的宽度
     * @param int $width 宽度
     */
    public function set_audit_width($width)
    {
        $this->_audit_width = $width; 
    }
    
    /**
     * 获取后台审核用的缩略图的宽度
     * @return int
     */
    public function get_audit_width()
    {
        return $this->_audit_width;
    }

    /**
     * 设置后台审核用的缩略图的高度
     * @param int $height 高度
     */
    public function set_audit_height($height)
    {
        $this->_audit_height = $height; 
    }
    
    /**
     * 获取后台审核用的缩略图的高度
     * @return int
     */
    public function get_audit_height()
    {
        $this->_audit_height;
    }
    
    /**
     * 设置图片上附加内容
     * @param string $other_string 内容
     */
    public function set_other_string($other_string)
    {
        $this->_other_string = $other_string;
    }
    
    /**
     * 获取图片上附加内容
     * @return string
     */
    public function get_other_string()
    {
        return $this->_other_string;
    }
    
   
    
    /**
     * 上传时设置session_id
     * 不知道为什么这样做
     * @param string $session_id
     */
    private function set_session_id($session_id)
    {
        if ($session_id)
        {
            session_id($session_id);
        }
    }
    
    /**
     * 获取临时图片的信息
     * @param string $filename 图片上传时input名称
     * @return string
     */
    private function get_temp_pic($filename)
    {
        return $_FILES[$filename]["tmp_name"];
    }
    
    /**
     * 检查是否有图片上传
     * @param string $filename 图片上传时input名称
     * @return boolean
     */
    private function check_pic_isupload($filename) 
    {
        if(isset($_FILES[$filename]['nocheck']) && 'house365' == $_FILES[$filename]['nocheck'])
        {
            return true;
        }
        else if (!isset($_FILES[$filename]) || !is_uploaded_file($this->get_temp_pic($filename))
                || $_FILES[$filename]["error"] != 0)
        {
            $this->result['result'] = 0;
            $this->result['msg'] = '未知文件，上传失败';
            return false;
        }
        else 
        {
            return true;
        }
    }
    
    /**
     * 获取图片上传的类型
     * @param string $temp_pic 临时文件路径
     * @return string  jpg and png
     */
    private function get_pic_type($temp_pic)
    {
        $img_info = getimagesize($temp_pic);
        return $img_info['mime'] == "image/jpeg" ? "jpg" : "png";
    }
    
    /**
     * 判断图片是否来自设备
     * 1代表是 0代表否
     * @param string $temp_pic 临时文件路径
     * @return int
     */
    private function get_pic_from_device($temp_pic)
    {
        $from_maker = 0;
        
	if(function_exists("exif_read_data"))
        {
            $exif = @exif_read_data($temp_pic, 0, true);
            if($exif)
            {
                foreach ($exif as $key => $section)
                {
                    foreach ($section as $name => $val) 
                    {
                        if($key == 'IFD0' && $name == 'Model')
                        {
                            $from_maker = 1; //来源于设备
                            break;
                        }
                    }
                }
            }
        }
        return $from_maker;
    }
    
    /**
     * 获取图片信息并给起md5加密
     * @param string $temp_pic 临时文件路径
     * @return sting md5加密后的字符串
     */
    private function set_pic_md($temp_pic)
    { 
        $bin ='';
        $f = fopen($temp_pic, "r");
        fseek($f, 0);
        $bin .= fread($f, 100);
        fseek($f, 165, SEEK_CUR);
        $bin = fread($f, 50);
        fseek($f, 365, SEEK_CUR);
        $bin .= fread($f, 50);
        fseek($f, 565, SEEK_CUR);
        $bin .= fread($f, 50);
        fseek($f, 765, SEEK_CUR);
        $bin .= fread($f, 50);
        fseek($f, -100, SEEK_END);
        $bin .= fread($f, 100);
        $file_md5 = md5($bin);
        return $file_md5;
    }
   

    /**
     * 上传图片，并返回上传后的地址
     */
    public function common_upload() 
    { 
        header('Content-Type:text/html;charset=utf-8');

        //设置文件上传时session_id值
        //$this->set_session_id($this->input->post('PHPSESSID'));

        $filename = $this->get_filename();
        //获取临时图片信息
        $temp_pic = $this->get_temp_pic($filename);
        //判断上传有效的图片
        $pic_validate = $this->check_pic_isupload($filename);
        if (!$pic_validate)
        {
            return false;
        }
       
        //设置上传图片的属性并且上传
        $this->load->library('UploadFile', array('filename' => $filename),'uf');
        $this->uf->setFileType("jpg|gif|png");
        $this->uf->setMaxSize($this->_max_size);
        $this->uf->setUploadType("ftp");
        $this->uf->setSaveDir($this->_save_dir);
        /*
        $this->uf->setWatermark($this->_water_mark_status);
        $this->uf->setWatermarkType(2);
        $this->uf->setWatermarkPosition("rb");
        $this->uf->setWatermarkImage("http://guanli.house365.com/images/sy2.png");//水印
         */
        $this->uf->setImageCut($this->_image_cut_status);
        $this->uf->setImageWidth($this->_image_width);
        $this->uf->setImageHeight($this->_image_height);
        $this->uf->setResizeImage($this->_resize_image_status);
        $this->uf->setResizeWidth($this->_resize_width);
        $this->uf->setResizeHeight($this->_resize_height);
        
        if ($this->get_other_string()) 
        {
            $this->uf->otherstring = $this->get_other_string();
        }

        if($this->uf->upload())
        {
            $picurl = $this->uf->getResizeImageURL();//缩略图片地址
        }
        return $picurl;
    }
    
    
    /**
     * 上传图片，并返回上传后的地址
     */
    public function upload() 
    {
        header('Content-Type:text/html;charset=utf-8');
        //设置文件上传时session_id值
        //$this->set_session_id($this->input->post('PHPSESSID'));
        
        $filename = $this->get_filename();
     
        //获取临时图片信息
        $temp_pic = $this->get_temp_pic($filename);
        //判断上传有效的图片
        $pic_validate = $this->check_pic_isupload($filename);
        if (!$pic_validate)
        {
			return "?pid=123456";
            return false;
        }
        //获取上传图片的类型
        //$pic_type = $this->get_pic_type($temp_pic); 
        //设置上传图片的属性并且上传
        $this->load->library('UploadFile', array('filename' => $filename),'uf');
        $this->uf->setFileType("jpg|gif|png");
        $this->uf->setMaxSize($this->_max_size);
        $this->uf->setUploadType("ftp");
        $this->uf->setSaveDir($this->_save_dir);
        $this->uf->setWatermark($this->_water_mark_status);
        $this->uf->setWatermarkType(2);//暂时改成文字水印 add wangzhen time:2015-11-17 17:53
        $this->uf->setWatermarkPosition("rb");
        $logo = "<?=STATIC_SOURCE_URL?>/zsb/images/v1.0/tf_logo_watermark.png";
        $this->uf->setWatermarkImage($logo);//水印	
        $this->uf->setImageCut($this->_image_cut_status);
        $this->uf->setImageWidth($this->_image_width);
        $this->uf->setImageHeight($this->_image_height);
        $this->uf->setResizeImage($this->_resize_image_status);
        $this->uf->setResizeWidth($this->_resize_width);
        $this->uf->setResizeHeight($this->_resize_height);
        
        $this->uf->setAudit($this->_audit_status);
        $this->uf->setAuditWidth($this->_audit_width);
        $this->uf->setAuditHeight($this->_audit_height);
        if ($this->get_other_string()) 
        {
            $this->uf->otherstring = $this->get_other_string();
        }
        $id = '';
        //把图片信息保存到临时表中
        if($this->uf->upload())
        {
            $picurl = $this->uf->getResizeImageURL();//缩略图片地址
            if ($picurl) 
            {
                //获取图片是否来自设备
                $from_mark = $this->get_pic_from_device($temp_pic);
                //对图片进行md5加密
                $file_md = $this->set_pic_md($temp_pic);
                $id = $this->insert_temp_data($picurl, $from_mark, $file_md);
            }
        }
        //返回图片的地址
        return $picurl."?pid=".$id;
    }
    
    /**
     * 插入房源临时表数据
     * @param string $filename 图片路径
     * @param int $from_device 是否来源设备 1是 0否
     * @param string $file_md5 图片md5加密
     * @return int 
     */
    public function insert_temp_data($filename, $from_device, $file_md5)
    {
        $this->db->set('filename', $filename);
        $this->db->set('dateline', date('Y-m-d'));
        $this->db->set('from_device', $from_device);
        $this->db->set('file_md5', $file_md5);
        $this->db->insert($this->_insert_temp_tablename);
        return $this->db->insert_id();
    }
    
    /**
     * 获取图片保存到哪张表里
     * @param type $tbl 表名
     * @param type $id 房源编号
     * @return string
     */
    public function get_upload_table_name($tbl, $id) 
    {
        if($tbl == 'sell' && $id < 23972101) return 'upload0';
        if($tbl == 'rent' && $id < 11907211) return 'upload0';

        if($tbl == 'sell' && ($id >= 23972101 && $id < 27029364)) return 'upload1';
        if($tbl == 'rent' && ($id >= 11907211 && $id < 13224200)) return 'upload1';

        if($tbl == 'sell' && ($id >= 27029364 && $id < 31695721)) return 'upload2';
        if($tbl == 'rent' && ($id >= 13224200 && $id < 14495163)) return 'upload2';

        if($tbl == 'sell' && ($id >= 31695721 && $id < 34963370)) return 'upload3';
        if($tbl == 'rent' && ($id >= 14495163 && $id < 15172611)) return 'upload3';

        if($tbl == 'sell' && ($id >= 34963370 && $id < 38640564)) return 'upload4';
        if($tbl == 'rent' && ($id >= 15172611 && $id < 15871631)) return 'upload4';

        if($tbl == 'sell' && ($id >= 38640564 && $id < 42138930)) return 'upload5';
        if($tbl == 'rent' && ($id >= 15871631 && $id < 16675751)) return 'upload5';

        if($tbl == 'sell' && ($id >= 42138930 && $id < 45947999)) return 'upload';
        if($tbl == 'rent' && ($id >= 16675751 && $id < 43871899)) return 'upload';

        if($tbl == 'sell' && ($id >= 45947999 && $id < 49830000)) return 'upload6';
                                                     //49825505
        if($tbl == 'rent' && ($id >= 43871899 && $id < 45040000)) return 'upload6';
                                                     //45038726
        if($tbl == 'sell' && ($id >= 49830000 && $id < 54705651)) return 'upload7';
                                                 //54703651
	    if($tbl == 'rent' && ($id >= 45040000 && $id < 45788862)) return 'upload7';
                                                 //45787862
	    if($tbl == 'sell' && ($id >= 54705651 && $id < 58020733)) return 'upload8';
                                                 //58018737 
		if($tbl == 'rent' && ($id >= 45788862 && $id < 46230598)) return 'upload8';
													 //46229598
		if($tbl == 'sell' && ($id >= 58020733 && $id < 61223139)) return 'upload9';
                                                 //61221139
		if($tbl == 'rent' && ($id >= 46230598 && $id < 46633747)) return 'upload9';
													 //46633347
		if($tbl == 'sell' && ($id >= 61223139 && $id < 63983200)) return 'upload10';
                                                 //63981991
	    if($tbl == 'rent' && ($id >= 46633747 && $id < 46923000)) return 'upload10';
                                                 //46922654
	    if($tbl == 'sell' && ($id >= 63983200 && $id < 66893902)) return 'upload11';
                                                 //66892902
	    if($tbl == 'rent' && ($id >= 46923000 && $id < 47264784)) return 'upload11';
                                                 //47264284
	    if($tbl == 'sell' && ($id >= 66893902 && $id < 71182067)) return 'upload12';
                                                 //71181067
		if($tbl == 'rent' && ($id >= 47264784 && $id < 47951200)) return 'upload12';
													 //47950659
		if($tbl == 'sell' && ($id >= 71182067 && $id < 76015831)) return 'upload13';
                                                 //76014531
	    if($tbl == 'rent' && ($id >= 47951200 && $id < 48556731)) return 'upload13';                                                                             //48556231
	    return 'upload14';

    }
    
    /**
     * 插入房源图片
     * @param array $insert_data 房源数据数组
     * @return int
     */
    public function insert_house_pic($insert_data, $tbl, $house_id) 
    {
        $uploadtable = $this->get_upload_table_name($tbl, $house_id);
        if ($this->db->insert($uploadtable, $insert_data))
        {
            return $this->db->insert_id();
        }
    }

	/**
     * 插入360全景房源图片
     * @param array $insert_data 房源数据数组
     * @return int
     */
    public function insert_house_panorama_pic($insert_data) 
    {
		$this->db->set('tbl', $insert_data['tbl']);
        $this->db->set('rowid', $insert_data['rowid']);
		$this->db->set('position', $insert_data['position']);
        $this->db->set('filename', $insert_data['filename']);
		$this->db->set('title', $insert_data['title']);
        $this->db->set('sort', $insert_data['title']);
		$this->db->set('createtime', $insert_data['createtime']);
        $this->db->set('flg', $insert_data['flg']);
		$this->db->set('isfrom', $insert_data['isfrom']);
        $this->db->set('isinfotype', $insert_data['isinfotype']);
		$this->db->set('roomtype', $insert_data['roomtype']);
        $this->db->insert('upload360');
$id = $this->db->insert_id();//echo $id.'<br>';
        return $id; 
    }

	/**
     * 更新360全景房源图片
     * @param array $insert_data 房源数据数组
     * @return int
     */
    public function update_house_panorama_pic($data) 
    {	
		if(empty($data))
		{
			return false;
		}

        $this->db->where('id',$data['id']);       
        $this->db->update('upload360',array('position' => $data['position'],'title' => $data['title'],'roomtype' => $data['roomtype']));
    }

	

	/**
     * 获取360全景房源图片
     * @param array $insert_data 房源数据数组
     * @return int
     */
    public function get_house_pic($tbl,$rowid) 
    {
		$this->db->select('id,rowid,position,filename,title,sort,flg,isinfotype,roomtype');
        $this->db->where('tbl', $tbl);
        $this->db->where('rowid', $rowid);
        $this->db->order_by('id', 'ASC');

        return $this->db->get('upload360')->result_array();
    }


	/**
     * 删除多张图片
	 *
	 */
	public function del_house_some_pic($data = array())
    {
		if(empty($data))
		{
			return false;
		}

        $this->db->where_in('id', $data);
        $this->db->delete('upload360');
        return $this->db->affected_rows() > 0 ? true : false;
    }

	
    
    /**
     * 获取某条房源的图片
     * @param string $tbl 表名
     * @param int $house_id 房源编号
     * @param int $sort 图片类型
     * @return array 
     */
    public function find_house_pic_by($tbl, $house_id, $sort = '') 
    {
        $uploadtable = $this->get_upload_table_name($tbl, $house_id);
        $this->db->select('id,filename,title,sort, position');
        $this->db->where('tbl', $tbl);
        $this->db->where('rowid', $house_id);
        $this->db->where_in('flg', array(0, 1));
        $this->db->order_by('id', 'ASC');
        if ($sort !== '')
        {
            $this->db->where('sort', $sort);
        }
        return $this->db->get($uploadtable)->result_array();
    }
    
    /**
     * 删除某条房源图片
     * @param string $tbl 表名
     * @param int $house_id 房源编号
     */
    public function del_house_pic_by($tbl, $house_id)
    {
        $uploadtable = $this->get_upload_table_name($tbl, $house_id);
        $this->db->where('tbl', $tbl);
        $this->db->where('rowid', $house_id);
        $this->db->delete($uploadtable);
        return $this->db->affected_rows() > 0 ? true : false;
    }
    
    /**
     * 获取临时图片表数据
     * @param int $id id编号
     * @return array 一维数组组成的记录
     */
    public function find_temp_by_id($id)
    {
        $this->db->select('from_device, file_md5');
        $this->db->where('id', $id);
        return $this->db->get($this->_insert_temp_tablename)->row_array();
    }
}