从ajax读取图像作为数据uri

时间:2016-01-20 22:13:35

标签: php ajax

我有这段代码

            $('.image').css({"background-image": "url('images/ajax-loader2.gif')",'background-size':'25%','background-position':'50%'});
            $.get('php/loadImage.php?id='+imgData+'&d='+(new Date()).getMilliseconds(),function(img){
                $('.user').html('<div style="position: absolute;top: 0;left: 0;right: 0;bottom: 0;background: url(data:image/jpg;base64,'+img+') no-repeat ; background-size: cover;width:100%;" class="image"></div>');
            });

/ php

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 2997 05:00:00 GMT");
header("Content-Type: text/html; charset=utf-8");

$id = $_GET['id'];
$row = $conn->query("SELECT usrImg, imgType FROM login WHERE usr='$id'");
$result = $row->fetch_assoc();
if(!empty($result['usrImg'])){
    header("Content-type: image/".$result['imgType']);
    echo $result['usrImg'];
}else{
    header("Content-type: image/png");
    include '../images/no-img.png';
}

问题是如何使图像可见

0 个答案:

没有答案