如何使用PHP将任何图像转换为位图图像并存储在MySQL数据库中?

时间:2015-05-27 07:27:04

标签: php mysql bitmap upload

如何使用PHP将任何图像转换为位图图像并存储在MySQL数据库中? 我想将图像存储在数据库中。 如何将任何图像转换为位图图像。

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

这是文件上传代码,但我想转换位图图像并上传到MySQL数据库。

2 个答案:

答案 0 :(得分:0)

$('.example input').on('click', function() {
   $('.example input').not(this).prop('checked', false);  
});

将$ img_for_db保存在数据库中,然后按照以下方式进行检索

$imgData = file_get_contents($filename);
$img_for_db=mysql_real_escape_string($imgData);

答案 1 :(得分:0)

数据库: phppot_examples

为表创建执行此查询

CREATE TABLE IF NOT EXISTS `output_images` (
  `imageId` tinyint(3) NOT NULL AUTO_INCREMENT,
  `imageType` varchar(25) NOT NULL DEFAULT '',
  `imageData` mediumblob NOT NULL,
  PRIMARY KEY (`imageId`)
)

<强> imageUpload.php

<?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>

<强> imageView.php

<?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);
?>

<强> listImages.php

<?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>

更多信息https://downloads.haskell.org/~ghc/7.0.3/docs/html/users_guide/win32-dlls.html 如果您满意,请接受我的回答。