<?php
/**
 * 图片处理类
 *
 * @author user
 */
class Pic_base_model extends MY_Model {
    
    /**
     * 上传文件数组_FILES[$filename]的名称
     * @var type 
     */
    public $filename;
    
    /**
     * 城市名
     * @var type 
     */
    public $city;
    
    /**
     * 返回的数据结果数组
     * @var type 
     */
    public $result = array('result' => '', 'msg' => '', 'data' => '');
    
    /**
     * 
     * @param type $filename
     */
    public function set_filename($filename)
    {
        $this->filename = $filename;
    }
    
    /**
     * 
     * @return type
     */
    public function get_filename()
    {
        return $this->filename;
    }
    
    /**
    * Constructor
    */
    public function __construct() 
    {   
        parent::__construct();
    }
    
    /**
     * 上传时设置session_id
     * 不知道为什么这样做
     * @param type $session_id
     */
    private function set_session_id($session_id)
    {
        if ($session_id)
        {
            session_id($session_id);
        }
    }
    
    /**
     * 获取临时图片的信息
     * @param type $filename
     * @return type
     */
    private function get_temp_pic($filename)
    {
        return $_FILES[$filename]["tmp_name"];
    }
    
    /**
     * 检查是否有图片上传
     * @param type $filename
     * @return boolean
     */
    private function check_pic_isupload($filename) 
    {
        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;
        }
    }
    
    /**
     * 获取图片上传的类型 jpg and png
     * @param type $temp_pic
     * @return type
     */
    private function get_pic_type($temp_pic)
    {
        $img_info = getimagesize($temp_pic);
        return $img_info['mime'] == "image/jpeg" ? "jpg" : "png";
    }
    
    /**
     * 判断图片是否来自设备
     * 1代表是 0代表否
     * @param type $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 type $temp_pic
     * @return type
     */
    private function set_pic_md($temp_pic)
    { 
        $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 upload() 
    {
        //设置文件上传时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;
        }
        //获取上传图片的类型
        //$pic_type = $this->get_pic_type($temp_pic); 
        //获取图片是否来自设备
        $from_mark = $this->get_pic_from_device($temp_pic);
        //对图片进行md5加密
        $file_md = $this->set_pic_md($temp_pic);
        //设置上传图片的属性并且上传
        $this->load->library('UploadFile_class', $filename, 'uf');
		$uf->setFileType("jpg|gif|png|swf|css|doc|xls");
		$uf->setMaxSize("2048");
		$uf->setUploadType("ftp");
		$uf->setSaveDir("/njhouse/");
		$uf->setWatermark(true);
		$uf->setWatermarkType(2);
        $uf->setWatermarkPosition("rb");
        $uf->setWatermarkImage("http://guanli.house365.com/images/sy2.png");//水印
		
		$uf->setImageCut(true);
		$uf->setImageWidth(640);
		$uf->setImageHeight(640);
        $tname = $dbback->GetOne("select truename from broker where uid = '$uid'");
		$truename = $tname != '' ? iconv('gbk','utf-8',$tname) : $_POST["truename"];

		if(!empty($truename)){
			$str1="该图片由";
			$str1=iconv('gbk','utf-8',$str1);
			$str2="提供";
			$str2=iconv('gbk','utf-8',$str2);
			$str = $str1.$truename.$str2;
			//$str = iconv('gbk','utf-8',$str);
			$uf->otherstring = $str;
		}

		$uf->setResizeImage(true);
		$uf->setResizeWidth(130);
		$uf->setResizeHeight(100);

		$uf->setAudit(true);
		$uf->setAuditWidth(200);
		$uf->setAuditHeight(150);
        
        //把图片信息保存到临时表中
        if($uf->upload())
        {
            $picurl =  $uf->getResizeImageURL();//缩略图片地址
            if ($picurl) 
            {
                
            }
        }
        //返回图片的地址
    }
    
    //获取图片保存到哪张表里
    public function get_upload_table_name() 
    {
        
    }
    
    //插入房源图片
    public function insert_house_pic($houseid, $pics) 
    {
        
    }
    
    //获取房源图片
    public function find_house_pic_by($houseid) 
    {
        
    }
    
    //删除房源图片
    public function del_house_pic_by($uid, $houseid)
    {
        
    }
    
    //通过图片编号获取信息
    public function find_by_id($id)
    {
        
    }
    
}
