简体   繁体   English

无法使用Varbinary Max使用PHP PDO从Sql Server显示图像

[英]Cannot Display image from Sql Server using PHP PDO using Varbinary Max

I have been racking my brain trying to figure out why an image will not display using Php PDO with it stored in Sql Server 2016 as a Varbinary Max field. 我一直在绞尽脑汁想弄清楚为什么使用Php PDO不能将图像存储在Sql Server 2016中作为Varbinary Max字段显示。 I use Php 7, and have this working on Php 5 using the same code, but with MySql. 我使用的是PHP 7,并使用相同的代码(但与MySql一起使用)在PHP 5上运行。 I want to move to Sql Server instead. 我想改用Sql Server。

When I display it all I get is a broken image, but in the source code it shows the image data. 当我显示它时,我得到的只是一张破碎的图像,但是在源代码中却显示了图像数据。 It is is encoded using base64, and I use a while loop to fetch the records. 它是使用base64编码的,我使用while循环来获取记录。 My code is below. 我的代码如下。

The insert which works fine and I can view the image in the database. 插入效果很好,我可以在数据库中查看图像。

$sql1 = "INSERT INTO weather_stories (filedate, text, imgfile)
                VALUES (:filedate, :text, :imgfile)";
    $preparedStatement = $conn->prepare($sql1);
    $preparedStatement->bindParam(':filedate', $_POST['filedate']);
    $preparedStatement->bindParam(':text', $_POST['text']);
    $preparedStatement->bindParam(':imgfile', $output, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
    $preparedStatement->execute();

Displaying image: 显示图像:

$sql = "SELECT TOP (2) id, filedate, imgfile, text, adddatetime, CONCAT(ROUND(DATALENGTH(imgfile)/1024, 1), 'k') as size
            FROM weather_stories
            ORDER BY adddatetime DESC";
  // use exec() because no results are returned
  $preparedStatement = $conn->prepare($sql);

  $preparedStatement->execute();

    //retrieve records
    while ($row = $preparedStatement->fetch())  {

        //create table
        echo "<tr>";
        echo "<td>" . $row['filedate'] . "</td>";
        echo "<td>" . $row['text'] . "</td>";
        echo "<td>" . $row['adddatetime'] . "</td>";
        echo "<td>" . $row['size'] . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td><a href='viewws.php?id=" . $row['id'] . "'>";
        echo "<img width='300' height='300' src='data:image/png;base64," . base64_encode($row['imgfile'])  . "' />";

        echo "</a></td>";
        echo "<td><a href='#' id=" . $row['id'] . " class='deletews'>Delete</a></td>";
        echo "</tr>";
    }

I do not get why this doesnt work in an img tag. 我不明白为什么这在img标签中不起作用。 All it displays is the broken image icon but when you look at the source code of the page it shows the data after the data: tag in the image. 它显示的只是破损的图像图标,但是当您查看页面的源代码时,它将在图像中的data:标记之后显示数据。

I have looked all over for a solution to this even on here and nothing works. 我一直到处寻找解决方案,即使在这里也没有任何效果。 I suspect it maybe with the fetch but dont know why, as there is limited documentation on the web about this. 我怀疑这可能与获取有关,但不知道为什么,因为网络上有关此的文档有限。

Help would be appreciated. 帮助将不胜感激。 Thanks! 谢谢!

I figured it out by just playing with some ideas Ive found, 我只是通过玩弄我发现的一些想法就知道了,

echo '<img src="data:image/png;base64,' . base64_encode(hex2bin($row['imgfile']))  . '" >';

The image had to be hex2bin 1st then base64 encoded and now it displays the image correctly. 该图像必须首先是hex2bin,然后是base64编码,现在它可以正确显示该图像。 I thought I had tried this before and it didnt work, unless I missed something, but I wish there was an easier way to it. 我以为我以前曾尝试过此方法,但除非有任何遗漏,否则它不会起作用,但我希望有一种更简单的方法。

Also header() method doesnt work it gives a black screen and image cannot be displayed cause of errors on firefox. 另外header()方法不起作用,它会显示黑屏,并且无法显示图像,这是在Firefox上导致错误的原因。 I have tried that before and same problem. 我以前尝试过,同样的问题。 I have no idea on why it works on some and not here. 我不知道为什么它可以在某些地方工作,而不是在这里工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM