Is this possible with php and a mysql database to Convert a blob into an image file?
这可能用php和mysql数据库将blob转换为图像文件吗?
4 个解决方案
#1
4
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.
如果BLOB包含图像的二进制数据(以可识别的格式,例如tiff,png,jpeg等),则获取BLOB的内容,将其写入文件,然后vo ...你得到了一个图像。
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.
在某些奇怪的操作系统上,您必须为输出文件提供相应的扩展名,以便可以像这样识别图像文件。
#2
8
You can use a few different methods depending on what php image library you have installed. Here's a few examples.
您可以使用几种不同的方法,具体取决于您安装的php图像库。这是一些例子。
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.
注意,echo 只是我用来在循环MySQL结果资源时显示来自同一个php脚本的多个图像的技巧。您也可以通过header()输出,如@NAVEED所示。
GD:
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):
ImageMagick(iMagick):
$image = new Imagick();
$image->readimageblob($blob);
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
GraphicsMagick (gMagick):
GraphicsMagick(gMagick):
$image = new Gmagick();
$image->readimageblob($blob);
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
#3
3
In my case i had to use base64_decode to convert blob images correctly into file.
在我的情况下,我不得不使用base64_decode将blob图像正确转换为文件。
$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));
}
#4
1
If you are storing images in MySql table Blob field and want to get these images then this article is useful for you:
如果您要将图像存储在MySql表Blob字段中并想要获取这些图像,那么本文对您有用:
- Tutorial on how to store images in Mysql BLOB field
- 关于如何在Mysql BLOB字段中存储图像的教程
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);
}
?>
#1
4
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.
如果BLOB包含图像的二进制数据(以可识别的格式,例如tiff,png,jpeg等),则获取BLOB的内容,将其写入文件,然后vo ...你得到了一个图像。
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.
在某些奇怪的操作系统上,您必须为输出文件提供相应的扩展名,以便可以像这样识别图像文件。
#2
8
You can use a few different methods depending on what php image library you have installed. Here's a few examples.
您可以使用几种不同的方法,具体取决于您安装的php图像库。这是一些例子。
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.
注意,echo 只是我用来在循环MySQL结果资源时显示来自同一个php脚本的多个图像的技巧。您也可以通过header()输出,如@NAVEED所示。
GD:
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):
ImageMagick(iMagick):
$image = new Imagick();
$image->readimageblob($blob);
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
GraphicsMagick (gMagick):
GraphicsMagick(gMagick):
$image = new Gmagick();
$image->readimageblob($blob);
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
#3
3
In my case i had to use base64_decode to convert blob images correctly into file.
在我的情况下,我不得不使用base64_decode将blob图像正确转换为文件。
$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));
}
#4
1
If you are storing images in MySql table Blob field and want to get these images then this article is useful for you:
如果您要将图像存储在MySql表Blob字段中并想要获取这些图像,那么本文对您有用:
- Tutorial on how to store images in Mysql BLOB field
- 关于如何在Mysql BLOB字段中存储图像的教程
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);
}
?>