Home > Backend Development > PHP Tutorial > AJax post BASE64 encoding to background - Stack Overflow

AJax post BASE64 encoding to background - Stack Overflow

WBOY
Release: 2016-08-25 10:37:28
Original
1391 people have browsed it

php convert & output BASE64

<code class="php">$img = str_replace(FILE_DOMAIN , FILE_PATH, 'http://img.demo.com/2016/08/21/12121.jpg');
if(! file_exists($img)) {
    $ret = array('err_no'=>-1,'err_msg' => '商品图片不存在');
    break;
}

$img_info = getimagesize($img);
$img_base64 = "data:{$img_info['mime']};base64,".chunk_split(base64_encode(file_get_contents($img)));
$result['base64'] = $img_base64;

$ret = array('err_no' => 0, 'err_msg'=> 'succes', 'results'=> $result);

echo json_encode($ret, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE  ); exit;</code>
Copy after login
Copy after login

json format is returned and is not processed directly. The data obtained from data.results cannot be displayed

<code class="text">data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/A....</code>
Copy after login
Copy after login

The above is synchronized. Put it in the html tag of li’s data-base64 for you

The following is asynchronous submission, first generate parameters

js

<code class="js">var idArr = [];
var imgArr =[];
$(".list-group").children('li').each(function(index){
    var imgBase64 = $(this).data('base64');
    var pId = $(this).data('item');
    idArr.push(pId); imgArr.push( encodeURIComponent(imgBase64));
});

//生成参数
var _params = $.param({
    pid: idArr.join(','),
    image: imgArr.join(','),
});</code>
Copy after login
Copy after login

Background reception and processing

php

<code class="php">$img_arr = explode(',', $params['image']);
foreach($result as $key => $val) {
    $ans_data[] = array(
        'image'   => addslashes( rawurldecode( $img_arr[$key]) ),
    );
}
                    </code>
Copy after login
Copy after login

Then insert it directly into the database. The field type is longtext,

The json output images at each step here cannot be displayed correctly

Reply content:

php convert & output BASE64

<code class="php">$img = str_replace(FILE_DOMAIN , FILE_PATH, 'http://img.demo.com/2016/08/21/12121.jpg');
if(! file_exists($img)) {
    $ret = array('err_no'=>-1,'err_msg' => '商品图片不存在');
    break;
}

$img_info = getimagesize($img);
$img_base64 = "data:{$img_info['mime']};base64,".chunk_split(base64_encode(file_get_contents($img)));
$result['base64'] = $img_base64;

$ret = array('err_no' => 0, 'err_msg'=> 'succes', 'results'=> $result);

echo json_encode($ret, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE  ); exit;</code>
Copy after login
Copy after login

json format is returned and is not processed directly. The data obtained from data.results cannot be displayed

<code class="text">data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/A....</code>
Copy after login
Copy after login

The above is synchronized. Put it in the html tag of li’s data-base64 for you

The following is asynchronous submission, first generate parameters

js

<code class="js">var idArr = [];
var imgArr =[];
$(".list-group").children('li').each(function(index){
    var imgBase64 = $(this).data('base64');
    var pId = $(this).data('item');
    idArr.push(pId); imgArr.push( encodeURIComponent(imgBase64));
});

//生成参数
var _params = $.param({
    pid: idArr.join(','),
    image: imgArr.join(','),
});</code>
Copy after login
Copy after login

Background reception and processing

php

<code class="php">$img_arr = explode(',', $params['image']);
foreach($result as $key => $val) {
    $ans_data[] = array(
        'image'   => addslashes( rawurldecode( $img_arr[$key]) ),
    );
}
                    </code>
Copy after login
Copy after login

Then insert it directly into the database. The field type is longtext,

The json output images at each step here cannot be displayed correctly

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template