title: Retrieve image data from mssql with php (utf-8)
body: <p>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. And I want to print it.</p>
  
  <p>I can get the data from MSSQL but I can't print/echo it as a valid image. How can I print/echo/save it?</p>
  
  <pre><code>$db= new PDO('odbc:MYODBC', '***', '***');
  $stmt = $db-&gt;prepare("USE database");
  $stmt-&gt;execute();
  $tsql = "SELECT image 
           FROM Pics 
           WHERE id = 12";
  $stmt = $db-&gt;prepare($tsql);
  $stmt-&gt;execute();
  
  $stmt-&gt;bindColumn(1, $lob, PDO::PARAM_LOB);
  $stmt-&gt;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 ...
  </code></pre>
  
  <p>PHP script encoding: UTF-8.</p>
  
  <p>In /etc/freetds/freetds.conf</p>
  
  <pre><code>[MYODBC]
  host = myhost.com
  client charset = UTF-8
  tds version = 7
  </code></pre>
  
  <p>((With sqlsrv on the server of MSSQL I could use this:</p>
  
  <pre><code>$image = sqlsrv_get_field( $stmt, 0, 
                        SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
  header("Content-Type: image/jpg");
  fpassthru($image);
  </code></pre>
  
  <p>))</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <pre><code>echo base64_decode($lob); //Not an image: γn­τΣ}4ΣM4ΣM4ί­4ΣM4ΫΝ4ΣM4s­...
  </code></pre>