Wordpress插件从mysql打印图像

时间:2016-01-08 12:57:56

标签: php mysql wordpress

我正在开发一个新的插件。我在数据库中创建了一个新表。

现在我可以将图像保存在中型blob中,但是我无法恢复到放入网站,只显示二进制代码。

如何编写此二进制代码以显示图像?

这是保存图像的代码。

function f_CALC_DIFF(pThis) {    

 var row_id     = pThis.id.substr(4);    
 var s          = $('#f18_'+row_id).val().replace(/[^\d.-]/g, '');   
 var curr       = $(pThis).val().replace(/[^\d.-]/g, '');   

if(!s){  
var s= 0;  
var prev = s;}    
else{  
var prev = s;}  

  var diff = 0; 
 diff = (curr - prev);
 var n = diff.toFixed(2);
 alert (n);
 $('#f23_'+row_id).val(n);

// Please remove the AJAX call to database.
 /*apex.server.process   
( "CALC_DIFF", { x01: prev, x02: curr },   
{ success: function( pData ) {   
$('#f23_'+row_id).val(pData);}}  
);    
}   */

我尝试使用此代码恢复:

$tipo = $_FILES['vehiculo_inserta_imagen']['type'];
$tamanio = $_FILES['vehiculo_inserta_imagen']['size'];
$imagen_temporal  = $_FILES['vehiculo_inserta_imagen']['tmp_name'];
//leer el archivo temporal en binario
$fp     = fopen($imagen_temporal, 'r+b');
$data = fread($fp, filesize($imagen_temporal));
fclose($fp);        
//escapar los caracteres
$data = mysql_escape_string($data);

1 个答案:

答案 0 :(得分:0)

嗨,也许你的代码有错误 请尝试这个并检查您的代码

上传图片

<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("phppot_examples");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('{$imageProperties['mime']}', '{$imgData}')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}}}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>

并显示图像保存在数据库

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("phppot_examples") or die(mysql_error());
if(isset($_GET['image_id'])) {
$sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["imageData"];
}
mysql_close($conn);
?>

列表图像

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("phppot_examples");
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC"; 
$result = mysql_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysql_fetch_array($result)) {
?>
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>
<?php       
}
mysql_close($conn);
?>
</BODY>
</HTML>

为了向浏览器显示BLOB图像,我们必须创建一个PHP文件来执行以下操作。

从MySQL BLOB获取图像数据和类型 使用PHP header()将content-type设置为image(image / jpg,image / gif,...)。 打印图像内容。

祝你好运并尝试