显示损坏的图像-PHP

时间:2019-01-22 06:24:25

标签: php

我正在尝试从我的数据库中显示BLOB类型的图像,但是它不起作用。我正在显示残破的图像。

上传到数据库代码:

 <form method="post" enctype="multipart/form-data">
            <br/>
            <input type="file" name="image" />
            <br/><br/>
            <input type="submit" name="sumit" value="Upload" />
            <?php  $imagee= file_get_contents(DB::query("SELECT profile_img FROM users WHERE id=:userid", array(':userid'=>$userid)));
            $type= DB::query("SELECT mime FROM users WHERE id=:userid", array(':userid'=>$userid));
                   echo '<img height="300" width="300" src="data:'.$type.';base64,'. base64_encode(stripslashes($imagee)).'"> ';
          ?>
        </form>
        <?php
       if(isset($_POST['sumit']))
        {
            if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
            {
                echo "Choose photo.";
            }
            else
            {
                $image= addslashes($_FILES['image']['tmp_name']);
                $name= addslashes($_FILES['image']['name']);
                $type= addslashes($_FILES['image']['type']);
                $image= file_get_contents($image);
                $image= base64_encode($image);
                DB::query("UPDATE users SET profile_img = :profile_img, mime=:mime WHERE id=:userid", array(':profile_img'=>$image,':mime'=>$type, ':userid'=>$userid));
            }
        }

显示图像代码:

  <?php  $imagee= file_get_contents(DB::query("SELECT profile_img FROM users WHERE id=:userid", array(':userid'=>$userid)));
            $type= DB::query("SELECT mime FROM users WHERE id=:userid", array(':userid'=>$userid));
                   echo '<img height="300" width="300" src="data:'.$type.';base64,'. base64_encode(stripslashes($imagee)).'"> ';
          ?>

1 个答案:

答案 0 :(得分:0)

保存到数据库时已对图像进行了编码。

在获取并显示它的同时,您再次对其进行编码。

您应该对其进行解码。

更正的代码:

$('.pjax-container').on('pjax:send', function(event, xhr, options) {
    // unique data attribute of the pjax container
    xhrName=$(this).attr('data-myXhrName');
    // abort pending requests because we are allowing multiples
    if (window.xhrRef[xhrName]) {
        window.xhrRef[xhrName].abort();
    }
    $.pjax.xhr=null;

    window.xhrRef[xhrName]=xhr;
})
.on('pjax:complete', function(event, xhr, options) {    
        xhrName=$(this).attr('data-myXhrName');
        // we're done with it
        window.xhrRef[xhrName]=false;
})