14

Is this possible with php and a mysql database to Convert a blob into an image file?

josh
  • 163
  • 1
  • 1
  • 4
  • 1
    Please add more detail. What does the BLOB field contain? What are you planning to do? – Pekka May 24 '11 at 06:21
  • 1
    if it's an image stored as a BLOB, yes. – Mitch Wheat May 24 '11 at 06:24
  • did you try setting header and then reading this blob? – k102 May 24 '11 at 06:25
  • If the blob contains the binary data of an image then what you ask would simply be a question of putting the data into a file with the correct file extension, or echoing the content to a page with the header information indicating what image format it is – Hubro May 24 '11 at 06:25
  • I am just planing to convert a blob into an image file... – josh May 24 '11 at 06:26
  • However i want to use php to save it into my server (maybe using file_put_contents() – josh May 24 '11 at 06:27

4 Answers4

11

You can use a few different methods depending on what php image library you have installed. Here's a few examples.

Note, the echo <img> is just a trick I use to display multiple images from the same php script when looping through a MySQL result resource. You could just as well output via header() as @NAVEED had shown.

GD:

$image = imagecreatefromstring($blob); 

ob_start(); //You could also just output the $image via header() and bypass this buffer capture.
imagejpeg($image, null, 80);
$data = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpg;base64,' .  base64_encode($data)  . '" />';

ImageMagick (iMagick):

$image = new Imagick();
$image->readimageblob($blob);
echo '<img src="data:image/png;base64,' .  base64_encode($image->getimageblob())  . '" />';

GraphicsMagick (gMagick):

$image = new Gmagick();
$image->readimageblob($blob);
echo '<img src="data:image/png;base64,' .  base64_encode($image->getimageblob())  . '" />';
6

If the BLOB contains the binary data of an image (in a recognizable format like e.g. tiff, png, jpeg, etc), take the content of the BLOB, write it to a file, and voilà... you got an image.

On some strange operation systems you have to give the output file a correspondig extension, so that the image file can be recognised as such.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
4

In my case i had to use base64_decode to convert blob images correctly into file.

$path = "/tmp/images";
$sql = "SELECT image_name, image_content FROM images";

$result = mysql_query($sql, $db_con);
if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $sql;
   die($message);
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $image = $row["image_contents"];
   $name = $row["image_name"];

   // option 1
   $file = fopen($path."/".$name,"w");
   echo "File name: ".$path."$name\n";
   fwrite($file, base64_decode($image));
   fclose($file);

   // option 2 (oneliner)
   // file_put_contents($path."/".$name, base64_decode($image));
}
bonyiii
  • 2,833
  • 29
  • 25
1

If you are storing images in MySql table Blob field and want to get these images then this article is useful for you:

Look at the following part from above article:

<?php
if(isset($_REQUEST['id']))
{
   // get the file with the id from database
      include "dbconfig.php";
      $dbconn = mysql_connect($dbhost, $dbusr, $dbpass) or die("Error Occurred-".mysql_error());
      mysql_select_db($dbname, $dbconn) or die("Unable to select database");

      $id    = $_ REQUEST ['id'];
      $query = "SELECT `img_name`, `img_type`, `img_size`, `img_data`
                       FROM img_tbl WHERE id = ‘$id’";

      $result = mysql_query($query) or die(mysql_error());
      list($name, $type, $size, $content) = mysql_fetch_array($result);

      header("Content-length: $size");
      header("Content-type: $type");
      print $content;

      mysql_close($dbconn);
}
?>
toto1911
  • 481
  • 3
  • 21
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • 2
    For anyone copying/pasting from this example, please note it uses bad quotation marks – kurdtpage Mar 29 '17 at 09:41
  • 9 years later... "This domain name expired on 3/15/2020 and is pending renewal or deletion." – Arnaud Apr 02 '20 at 13:44
  • Most of these answers are old but mysql functions have been depreciated and are no longer included with PHP. Use mysqli or PDO instead. – DonP May 07 '20 at 16:38