简体   繁体   English

使用php(utf-8)从mssql检索图像数据

[英]Retrieve image data from mssql with php (utf-8)

From a php script (PHP 5.3.10-1 on Ubuntu3.6) I connect to an MSSQL Server and I would like to retrive image data from image field type. 从php脚本(Ubuntu3.6上的PHP 5.3.10-1),我连接到MSSQL Server,我想从图像字段类型中检索图像数据。 And I want to print it. 我想打印它。

I can get the data from MSSQL but I can't print/echo it as a valid image. 我可以从MSSQL获取数据,但不能将其作为有效图像打印/回显。 How can I print/echo/save it? 如何打印/回显/保存?

$db= new PDO('odbc:MYODBC', '***', '***');
$stmt = $db->prepare("USE database");
$stmt->execute();
$tsql = "SELECT image 
         FROM Pics 
         WHERE id = 12";
$stmt = $db->prepare($tsql);
$stmt->execute();

$stmt->bindColumn(1, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);

header("Content-Type: image/jpg");

echo($lob); //not an image: 424df630030000000000360000002800 ...

imagecreatefromstring($lob);  // Data is not in a recognized format ...

$lob = fopen('data://text/plain;base64,' . base64_encode($lob), 'r'); //Resource
fpassthru($lob); //not an image: 424df63003000000000036000000280000 ...

PHP script encoding: UTF-8. PHP脚本编码:UTF-8。

In /etc/freetds/freetds.conf 在/etc/freetds/freetds.conf中

[MYODBC]
host = myhost.com
client charset = UTF-8
tds version = 7

((With sqlsrv on the server of MSSQL I could use this: (((通过MSSQL服务器上的sqlsrv,我可以使用以下命令:

$image = sqlsrv_get_field( $stmt, 0, 
                      SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
header("Content-Type: image/jpg");
fpassthru($image);

)) ))

UPDATE : 更新

echo base64_decode($lob); //Not an image: γn­τΣ}4ΣM4ΣM4ί­4ΣM4ΫΝ4ΣM4s­...

Try adding the following headers : 尝试添加以下标头:

  • Content-Disposition 内容配置
  • Content-Transfer-Encoding 内容传输编码
  • Content-Length 内容长度

In PHP code : 在PHP代码中:

header('Content-Type: image/jpg');
header('Content-Disposition:attachment; filename="my_file.jpg"');// Set the filename to your needs
header('Content-Transfer-Encoding: binary');
header('Content-Length: 12345');// Replace 12345 with the actual size of the image in bytes

I was recently fighting with a similar storage issue. 我最近正在解决类似的存储问题。 It turned out that I was quoting my binary image data before insert into the database table. 原来,在插入数据库表之前,我已经引用了二进制图像数据。 So, make sure you are not adding quotes and turning it into a string - like I accidentally did. 因此,请确保您没有添加引号并将其转换为字符串-就像我不小心那样。

Here's the prep work that is done on an existing local file to get the proper data to store into your database. 这是对现有本地文件完成的准备工作,以获取适当的数据以存储到数据库中。 Also, make sure you have bin2hex() available or get a replacement version of the function. 另外,请确保您有bin2hex()可用或获得该函数的替代版本。

function prepareImageDBString($filepath) {

    $out = 'null';
    $handle = @fopen($filepath, 'rb');
    if ($handle) {
        $content = @fread($handle, filesize($filepath));
        // bin2hex() PHP Version >= 5.4 Only!
        $content = bin2hex($content); 
        @fclose($handle);
        $out = "0x" . $content;
    }
    return $out;
}

I hope this helps someone. 我希望这可以帮助别人。

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

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