doupeng5320 2019-01-22 06:24
浏览 44

显示破碎的图像 - PHP

Im trying to display BLOB type image from my database, but its not working. I`m getting broken image showed.

Upload to database code:

 <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));
            }
        }

Display image code:

  <?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条回答 默认 最新

  • doudu5498 2019-01-22 07:08
    关注

    You have encoded image while saving to Database.

    And while fetching it and displaying it, you are again encoding it.

    You should decode it.

    Corrected Code:

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

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等