>  기사  >  백엔드 개발  >  AJax는 BASE64 인코딩을 배경으로 게시합니다.

AJax는 BASE64 인코딩을 배경으로 게시합니다.

WBOY
WBOY원래의
2016-08-25 10:37:281320검색

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>

json 형식 반환은 직접 처리되지 않으며data.results 표시할 수 없습니다

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

위 내용이 동기화되어 html 태그의 li에 넣으면 앞에 data-base64가 보입니다.

다음은 비동기 제출입니다. 먼저 매개변수를 생성하세요

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>
백그라운드 수신 및 처리

php

<code class="php">$img_arr = explode(',', $params['image']);
foreach($result as $key => $val) {
    $ans_data[] = array(
        'image'   => addslashes( rawurldecode( $img_arr[$key]) ),
    );
}
                    </code>
그런 다음 데이터베이스에 직접 삽입합니다. 필드 유형은

, longtext입니다.

여기서 각 단계의 json 출력 이미지를 올바르게 표시할 수 없습니다

답글 내용:

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>
json 형식 반환은 직접 처리되지 않으며

표시할 수 없습니다data.results

<code class="text">data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gMTAwCv/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKP/A....</code>
위 내용이 동기화되어

태그의 html에 넣으면 앞에 li가 보입니다. data-base64다음은 비동기 제출입니다. 먼저 매개변수를 생성하세요

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>
백그라운드 수신 및 처리

php

그런 다음 데이터베이스에 직접 삽입합니다. 필드 유형은
<code class="php">$img_arr = explode(',', $params['image']);
foreach($result as $key => $val) {
    $ans_data[] = array(
        'image'   => addslashes( rawurldecode( $img_arr[$key]) ),
    );
}
                    </code>
,

입니다. longtext

여기서 각 단계의 json 출력 이미지를 올바르게 표시할 수 없습니다

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.