<?
include DDROOT."/jian_file/Wechat/Wechat.class.php";


  function local_img($content){

  	  $img_rows=get_imgs($content);
  	  foreach ($img_rows as $r){

			  $img_url=set_img($r);
			  $r = preg_replace("/\//", "\\/", $r);
			  $content = preg_replace("/$r/", $img_url, $content); 
	}

	return $content;
  }

   function get_imgs ($str){
		
		$parrent = "/(href|src)=([\"|']?)([^\"'>]+.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG))/i";
		preg_match_all($parrent,$str,$jian_m);
		return $jian_m[3];
		}


function set_img($url){
		  $ext = strrchr($url,'.');   //获取图片格式
		   $img = file_get_contents($url); 
		   
		    $img_name="upload/bm/".getMillisecond().$ext;
			usleep(100);
			file_put_contents(DDROOT."/".$img_name,$img); 
			return $img_name;
		}
function getMillisecond() { 
	list($s1, $s2) = explode(' ', microtime()); 
	return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); 
} 
function obj(){

	global $webset;
		 $options = array (

		  'token' => $webset["gzh_token"], // 填写你设定的key

        'encodingaeskey' => $webset["gzh_encodingaeskey"], // 填写加密用的EncodingAESKey

        'appid' => $webset["gzh_appid"], // 填写高级调用功能的app id

        'appsecret' => $webset["gzh_appsecret"] // 填写高级调用功能的密钥

		);

		$weObj = new Wechat ($options);

		return $weObj;

	 }

 function tgewm($tjr){
 	global $webset;
    //https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFa8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyUWswSEZSOVBlN0UxMDAwME0wN04AAgRSyCxgAwQAAAAA
     $obj=obj();
     $param=$tjr;
     $result=$obj->getQRCode($param,2);

     $ticket=$result['ticket'];
     if ($ticket){
     	$imgurl=$obj->getQRUrl($ticket);
     
	     // $imgurl="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFa8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyUWswSEZSOVBlN0UxMDAwME0wN04AAgRSyCxgAwQAAAAA";
	     $imgnl=downloadimageweixin($imgurl);
	     $filename=$tjr.".jpg";
	     $path=DDROOT."/upload/ewm/$filename";
	     file_put_contents($path, $imgnl['body']);
	     imagecropper($path,190);

        
		$image = new Javion\Image\Watermark($path);

	
		$a=$image->waterImg(DDROOT."/".$webset['ewmlogo'], 5)->save($path);

		
	    return "upload/ewm/$filename";
     }else{

     	$res['code']=1;
	     $res['msg']="接口异常,ticket为空";
	     echo dd_json_encode($res);exit();
     }
     

}

function downloadimageweixin($url){

	  $ch=curl_init($url);
	  curl_setopt($ch, CURLOPT_HEADER, 0);
	  curl_setopt($ch, CURLOPT_NOBODY, 0);  //只取body头
	  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	  $package=curl_exec($ch);
	  $httpinfo=curl_getinfo($ch);
	  curl_close($ch);
	  return array_merge(array("body"=>$package),array("header"=>$httpinfo));
}
$jian_roles=array(
	array("field"=>"GLY","value"=>12),
	array("field"=>"XMFZR","value"=>17),
	array("field"=>"DGL","value"=>18),
	array("field"=>"YGL","value"=>19),
	array("field"=>"DC","value"=>20),
	array("field"=>"YS","value"=>21),
	array("field"=>"CW","value"=>22),
	array("field"=>"CK","value"=>23),
);
foreach ($jian_roles as $key => $value) {
	define($value['field'],$value['value']);
}

function role_field($role){
	global $jian_roles;
	foreach ($jian_roles as $key => $value) {

		if ($value['value']==$role){
			return $value['field'];
		}
	}
}
// define("GLY",12);
// define("XMFZR",17);
// define("DGL",18);
// define("YGL",19);
// define("DC",20);
// define("YS",21);
// define("CW",22);
// define("CG",23);
function shu($str){
	$rows=explode(",", $str);
    $str="";
    foreach ($rows as $key => $value) {
    	 $str.="|".$value."|";
   }
   return $str;
}


?>