PHP base64图片转换上传

function base64_img($base64_img){

   /*  $base64_string = $base64_img;
	$savename = uniqid().'.jpeg';

	$save_path = $php_path . 'Uploads/';
	$ymd = date("Ymd");
	$save_path .= $ymd . "/";
	$save_url .= $ymd . "/";
	if (!file_exists($save_path)) {
		mkdir($save_path);

	}
	$savepath = $save_path . $savename;
	$image = base64_to_img( $base64_string, $savepath );
	return '/'.$image; */
	
	
	$base64_string = $base64_img;
	$base64_0=explode(',', $base64_string);
	$base64_1=explode('/', $base64_0['0']);
	$base64_2=explode(';', $base64_1['1']);
	$s = base64_decode(str_replace(''.$base64_0['0'].',','',$base64_string));
	$savename = uniqid().time().rand(10000,99999).'.'.$base64_2['0'];
	$save_path = $php_path . 'Uploads/image/';
	$ymd = date("Ymd");
	$save_path .= $ymd . "/";
	$save_url .= $ymd . "/";
	if (!file_exists($save_path)) {

		mkdir($save_path);

	}

	$savepath = $save_path . $savename;

	$image= file_put_contents($savepath, $s);
	return '/'.$savepath;
}

function base64_to_img( $base64_string, $output_file ) {

	$ifp = fopen( $output_file, "wb" ); 

	fwrite( $ifp, base64_decode( $base64_string) ); 

	fclose( $ifp ); 

	return( $output_file ); 

}

猜你喜欢

转载自blog.csdn.net/qq_18105691/article/details/79540292