如何在php中将longblob转换为图像? [英] How to convert longblob to image in php?
本文介绍了如何在php中将longblob转换为图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用C#编写代码向mysql添加数据,图像结构为longblob。
然后我制作一个php脚本在网上显示它?但我可以将longblob转换成图像。我尝试将二进制转换为图像但不起作用。我怎么能这样做?
这是我的代码:
private void btn_open_Click( object sender,EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = JPG文件(*。jpg)| * .jpg | PNG文件(*。 png)| * .png | ALL文件(*。*)| *。*;
if (dlg.ShowDialog()== DialogResult.OK)
{
string picPath = dlg.FileName.ToString();
txt_path.Text = picPath;
pictureBox1.ImageLocation = picPath;
}
}
私有 void btn_add_Click( object sender,EventArgs e)
{
try
{
< span class =code-comment> // conver to byte
byte [] imageBt = null ;
FileStream fstream = new FileStream( this .txt_path.Text,FileMode.Open,FileAccess。读);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes(( int )fstream.Length);
// kếtnối数据库
string constring = Server = localhost; Database = luan_van; Port = 3306; User ID = root;密码=跨度>;
string Query = INSERT into tt_nhanvien( Card_ID,Ma_nv,Ten,Phong_ban,Chuc_vu,Hinh)值(' + this.txt_ID.Text + ',' + this.txt_ma.Text + ',' + this.txt_ten.Text + ',' + this.txt_pban.Text + ',' + this.txt_chvu.Text + ,@图像);跨度>;
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query,conDataBase);
MySqlDataReader myReader;
尝试
{
conDataBase.Open();
cmdDataBase.Parameters.Add( new MySqlParameter( @image,imageBt));
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show( SAVE);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch
{
}
}
和PHP代码:
<? php
error_reporting(0);
// $ db = mysql_connect(localhost,root,root);
$ db = mysql_connect( localhost, root, 跨度>);
if(!$ db)
{
echo < span class =code-string>KHÔNGTHỂKẾTNỐIDATABASE;
退出;
}
// chon csdl
$ db_selected = mysql_select_db( luan_van,$ db);
if(!$ db_selected)
{
die( Khôngthểsửdụng数据库: .mysql_error());
}
$ Ten = $ _ REQUEST [ 十]; // lấyđượctênnhânviùnmbàngườidùngchọnbằngcáchickickuột
// truyvấncsdl
$ sql = SELECT * FROM tt_nhanvien WHERE十个'$ Ten'; // chỉchọnđúngtên笔记本电脑请求
$ result = mysql_query($ sql);
// xuatdữliệu
if(mysql_num_rows($ result)< > 0)
{
echo < table width ='800'border ='1'align ='center'cellpadding ='0'cellsacing ='0'border ='1'style ='border-collapse:collapse#FF6600'> 跨度>;
while($ row = mysql_fetch_row($ result))
{
$ Card_ID = $ row [0];
$ Ma_nv = $ row [1];
$ Ten = $ row [2];
$ Phong_ban = $ row [3];
$ Chuc_vu = $ row [4];
$ Hinh = nhan_vien /.$row[5];
$ Hinh = < img src ='$ Hinh'alt ='$ Ten'>;
$ image_data = file_get_contents($ Hinh);
$ encoded_image = base64_encode($ image_data);
$ decoding_image = base64_decode($ encoded_image);
echo < tr>< td align ='center'colspan ='2'bgcolor ='#FFEEE6'> $ Ten< / td>< / tr>;
echo < TR>中跨度>;
echo < td align =' center'valign ='middle'> $ decoding_image< / td>;
echo < td>< /跨度>;
echo < table align ='中心'跨度>;
echo < tr>< td> 卡ID: $ Card_ID< / td>< / tr>;
echo < tr>< td> Tên: $ Ten< / td>< / tr>;
echo < tr>< td> Phòng禁令: $ Phong_ban< / td>< / tr>;
echo < tr>< td> Mãnhânviên: $ Ma_nv< / td>< / tr>;
echo < tr>< td> Chứcvụ: $ Chuc_vu< / td>< / tr>;
echo < / table> 跨度>;
echo < / td> 跨度>;
echo < tr align ='对'>< td>< a href ='thong_tin_nv.php'> Quayvề< / a>< / td>< / tr>;
echo < / tr> 跨度>;
}
echo < ; /表>跨度>;
}
?>
解决方案
db = mysql_connect(localhost,root,root);
db = mysql_connect( localhost, root, );
if(!
db)
{
echo KHÔNGTHỂKẾTNỐIDATABASE;
退出;
}
// chon csdl
I write the code add data to mysql using C# with structure of image is longblob.
And then I make a php script to display it on web? But I can convert longblob to image. I try to convert binary to image but ít not work.How can I do this?
Here is my code:
private void btn_open_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
string picPath = dlg.FileName.ToString();
txt_path.Text = picPath;
pictureBox1.ImageLocation = picPath;
}
}
private void btn_add_Click(object sender, EventArgs e)
{
try
{
// conver to byte
byte[] imageBt = null;
FileStream fstream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
//kết nối database
string constring = "Server=localhost;Database=luan_van;Port=3306;User ID=root;Password=";
string Query = "INSERT into tt_nhanvien (Card_ID,Ma_nv,Ten,Phong_ban,Chuc_vu,Hinh) value ('"+this.txt_ID.Text +"','"+this.txt_ma.Text+"','"+this.txt_ten.Text+"','"+this.txt_pban.Text+"','"+this.txt_chvu.Text+"',@image);";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
cmdDataBase.Parameters.Add(new MySqlParameter("@image", imageBt));
myReader = cmdDataBase.ExecuteReader();
MessageBox.Show("SAVE");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch
{
}
}
and PHP code:
<?php
error_reporting(0);
//$db= mysql_connect("localhost","root","root");
$db= mysql_connect("localhost","root","");
if(!$db)
{
echo "KHÔNG THỂ KẾT NỐI DATABASE";
exit;
}
//chon csdl
$db_selected = mysql_select_db("luan_van",$db);
if(!$db_selected)
{
die("Không thể sử dụng DATABASE: ".mysql_error());
}
$Ten=$_REQUEST["Ten"];// lấy được tên nhân viên mà người dùng chọn bằng cách lick chuột
// truy vấn csdl
$sql= "SELECT * FROM tt_nhanvien WHERE Ten LIKE '$Ten'"; // chỉ chọn đúng tên laptop request
$result = mysql_query($sql);
//xuat dữ liệu
if(mysql_num_rows($result)<>0)
{
echo "<table width='800' border='1' align='center'cellpadding='0' cellspacing='0' border='1' style='border-collapse:collapse #FF6600'>";
while($row = mysql_fetch_row($result))
{
$Card_ID= $row[0];
$Ma_nv = $row[1];
$Ten = $row[2];
$Phong_ban = $row[3];
$Chuc_vu = $row[4];
$Hinh = "nhan_vien/".$row[5];
$Hinh = "<img src ='$Hinh' alt='$Ten'>";
$image_data=file_get_contents($Hinh);
$encoded_image=base64_encode($image_data);
$decoded_image=base64_decode($encoded_image);
echo "<tr><td align='center' colspan='2' bgcolor='#FFEEE6' >$Ten</td></tr>";
echo"<tr>";
echo"<td align='center' valign='middle'>$decoded_image</td>";
echo "<td>";
echo "<table align='center'";
echo "<tr><td> Card ID:$Card_ID</td></tr>";
echo "<tr><td> Tên:$Ten</td></tr>";
echo "<tr><td> Phòng ban:$Phong_ban</td></tr>";
echo "<tr><td> Mã nhân viên:$Ma_nv</td></tr>";
echo "<tr><td> Chức vụ:$Chuc_vu</td></tr>";
echo "</table>";
echo "</td>";
echo "<tr align='right'><td><a href='thong_tin_nv.php'> Quay về</a></td></tr>";
echo "</tr>";
}
echo "</table>";
}
?>
解决方案
db= mysql_connect("localhost","root","root");
db= mysql_connect("localhost","root",""); if(!
db) { echo "KHÔNG THỂ KẾT NỐI DATABASE"; exit; } //chon csdl
这篇关于如何在php中将longblob转换为图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文