php 保存编辑器的截图

DATE: 2015-07-03 / VIEWS: 878

//保存内容中含有截图
function save_local($content) {
	if($content == '<br type="_moz" />') return '';//FireFox
	if($content == '&nbsp;') return '';//Chrome
	if(strpos($content, 'data:image') === false) return $content;
	if(!preg_match_all("/src=(["|']?)([^ "'>]+)1/i", $content, $matches)) return $content;
	$filedir = './'.C('uploadpath').'image/'.date('Ym').'/';
	if(!is_dir($filedir)) @mkdir($filedir,0777);
	$php_url =  .'/'. C('uploadpath').'image/'.date('Ym').'/';
	$urls = $oldpath = $newpath = array();
	foreach($matches[2] as $k=>$url) {
		if(in_array($url, $urls)) continue;
		$urls[$url] = $url;	
		if(strpos($url, 'data:image') === false) continue;
		if(strpos($url, ';base64,') === false) continue;
		$t1 = explode(';base64,', $url);
		$t2 = explode('/', $t1[0]);
		$file_ext = $t2[1];
		if($file_ext == 'jpeg') $file_ext = 'jpg';
		$filename = uniqid().'.'.$file_ext;
		$newfile = $filedir.$filename;
		$fp = @fopen($newfile,"w");
        @fwrite($fp,base64_decode($t1[1]));
        @fclose($fp);
		if(!@getimagesize($newfile)) {
			file_del($newfile);
			continue;
		}
		$oldpath[] = $url;
		$newpath[] = $php_url.$filename;
	}
	unset($matches);
	return str_replace($oldpath, $newpath, $content);
}