多图上传
/**
* base64 多图上传
*/
function base64_all_image_upload($all_image_str){
$img_str_array = explode('|', $all_image_str);
if($img_str_array){
$url = '';
foreach($img_str_array as $v){
$base64_img = trim($v);
//$base64_img = "data:image/jpeg;base64," . $base64_img;
//创建文件夹
$up_dir = 'uploads/image/'.date('Y-m-d');
//看文件夹 有没有权限
if(!is_dir($up_dir)){
mkdir($up_dir,0777,true);
}
//查看base64 是不是符合要求
if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
$type = $result[2];
if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
if($type=='jpeg'){
$type='jpg';
}
//文件名
$image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
//图片文件路径
$new_file = $up_dir.'/'.$image_name;
$base64_1 = str_replace($result[1],'', $base64_img);
//将解码的数据 写入到文件中
if(file_put_contents($new_file, base64_decode($base64_1))){
$pic = '/'.$new_file;
$url = $pic.'|'.$url;
}else{
$data['code'] = 2;
$data['data'] = '上传失败';
return $data;
}
}else{
$data['code'] = 3;
$data['data'] = '上传失败';
return $data;
}
}else{
$data['code'] = 4;
$data['data'] = '上传失败';
return $data;
}
}
if($url){
//去掉字符串最后的|
$url = rtrim($url,'|');
$data['code'] = 1;
$data['data'] = $url;
return $data; //返回用逗号隔开的上传后的图片路径
}
}else{
$data['code'] = 5;
$data['data'] = '参数错误';
return $data;
}
}
单图上传
/**
* base64 单图上传
*/
function base64_image_upload($data_string){
$base64_img = trim($data_string);
//$base64_img = "data:image/jpeg;base64," . $base64_img;
//创建文件夹
$up_dir = 'uploads/image/'.date('Y-m-d');
//看文件夹 有没有权限
if(!is_dir($up_dir)){
mkdir($up_dir,0777,true);
}
//查看base64 是不是符合要求
if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
$type = $result[2];
if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
if($type=='jpeg'){
$type='jpg';
}
//文件名
$image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
//图片文件路径
$new_file = $up_dir.'/'.$image_name;
$base64_1 = str_replace($result[1],'', $base64_img);
//将解码的数据 写入到文件中
if(file_put_contents($new_file, base64_decode($base64_1))){
$url = '/'.$new_file;
$data['code'] = 1;
$data['data'] = $url;
return $data;
}else{
$data['code'] = 2;
$data['data'] = '上传失败';
return $data;
}
}else{
$data['code'] = 3;
$data['data'] = '上传失败';
return $data;
}
}else{
$data['code'] = 4;
$data['data'] = '上传失败';
return $data;
}
}
iOS代码
-(void)base64Image{
NSString *path = [[NSBundle mainBundle] pathForResource:@"ll.png" ofType:nil];
UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:path];
NSData *data =UIImageJPEGRepresentation(savedImage, 1.0);
NSString *encodedImgStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
// NSString *path1 = [[NSBundle mainBundle] pathForResource:@"aa.jpg" ofType:nil];
// UIImage *savedImage1 = [[UIImage alloc] initWithContentsOfFile:path1];
// NSData *data1 =UIImageJPEGRepresentation(savedImage1, 1.0);
// NSString *encodedImgStr1 = [data1 base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *data_url = [NSString stringWithFormat:@"%@",encodedImgStr];
NSLog(@"++++----%@",data_url);
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:10];
[dic setObject:data_url forKey:@"imageString"];
[ZHRequestTools HTTPPostWithUrl:@"/Api/v1/index/test" Parames:dic commplete:^(id result, NSInteger StatusCode) {
NSLog(@"result===%@",result);
} failure:^(NSError *error, NSInteger errorCode) {
NSLog(@"error====%@",error);
}];
}
总结
1、前端将图片转换成base64 是这种格式的data:image/jpeg;base64,asaskjxibasjh(base64字符串)
2、iOS 将图片转换base64 没有 data:image/jpeg;base64, 要额外获取文件的mime 拼接上