M
M
mrRoss2015-10-19 19:19:24
PHP
mrRoss, 2015-10-19 19:19:24

PHP and MSSQL image type. How to get a picture?

Hello!
Maybe someone faced such a task:
in the MSSQL database there is a field with the image type (image). Is it possible to somehow get this picture using a php script and ideally save it to a folder on the server?
I connect to the MSSQL server using the ODBC driver.
($connection_string = "DRIVER={SQL Server};SERVER=$serverMSSQL;";
$connectionMSSQL = odbc_connect($connection_string,$userMSSQL,$passMSSQL);)
Many thanks in advance for your help! :)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Schoolboy., 2015-10-19
@viphorizon

I once also asked a similar question, but about PostgreSQL. Do not ask such questions here, no one will answer, since it is unlikely that a person will come across who knows well other database management systems other than MySQL (MariaDB)

S
Stalker_RED, 2015-10-19
@Stalker_RED

As far as I understand , the image type in mssql is essentially just a binary blob. Besides, he's already deprecated.
Most likely, there are binary images in the database, sometimes they are also encoded in base64.
Output the first kilobyte as text, look at the headers.

A
Aleksey Ratnikov, 2015-10-19
@mahoho

In general, this is a bad topic - storing images in the database - firstly, it is very expensive in place + the fetch results are too heavy, and, as Stalker_RED noted , this field has the Deprecated status. But, since it happened, then write the picture to a file in binary-safe mode, and then take its mime type:

$imageFile = fopen('image', 'wb');
fwrite($imageFile, $fetchedImageFromDb);
fclose($imageFile);

$mimeType = exif_imagetype('image');
switch ($mimeType){
  case IMAGETYPE_GIF:{
    rename('image', 'image.gif');
    break;
  }
  case IMAGETYPE_BMP:{
    rename('image', 'image.bmp');
    break;
  }
  case IMAGETYPE_JPEG:{
    rename('image', 'image.jpeg');
    break;
  }
  case IMAGETYPE_PNG:{
    rename('image', 'image.png');
    break;
  }
  default:{
    throw new Exception('Unhandled image type');
  }
}

other possible types can be found in the documentation for exif_imagetype(): php.net/manual/en/function.exif-imagetype.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question