如何上传图像元数据到mysql列



这是我试图上传的jpeg文件我有一个PHP代码,将图像上传到MySql数据库,我试图弄清楚我如何添加一个列与元数据,如GPS,相机类型,白天/夜晚等。我需要做的是添加元数据列,并从所有图像中搜索特定的元数据。我有以下PHP代码:'

<?php 
// Include the database configuration file 
include_once 'dbconfig.php'; 

if(isset($_POST['submit'])){ 
// File upload configuration 
$targetDir = "C:\xampp\htdocs\uploaded\"; 
$allowTypes = array('jpg','png','jpeg','gif'); 

$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; 
$fileNames = array_filter($_FILES['files']['name']); 
if(!empty($fileNames)){ 
function getExif( $filename, $key1, $key2)
{
$width = 0;
$allowedFiletypes = array("jpg","jpeg","tif","tiff");
if(in_array(strtolower(substr($filename, -3)),$allowedFiletypes))
{
if(exif_read_data($filename) == true) //this is line 19
{
$exif = exif_read_data($filename, 0, true);
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
if($key === $key1 AND $name === $key2){
$width = $val;
}
}
}
}
return $width;
}
else
{
print "filetype not supported";
}
}
$key1 = "IFD0";
$key2 = "ImageWidth";
foreach($_FILES['files']['name'] as $key=>$val){ 
// File upload path 
$fileName = basename($_FILES['files']['name'][$key]); 
$width = getExif( $fileName, $key1, $key2);

$targetFilePath = $targetDir . $fileName; 
// Check whether file type is valid 
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
if(in_array($fileType, $allowTypes)){ 
// Upload file to server 
if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ 
// Image db insert sql 
$insertValuesSQL .= "('".$fileName."', NOW(), '".$width."'),"; 
}else{ 
$errorUpload .= $_FILES['files']['name'][$key].' | '; 
} 
}else{ 
$errorUploadType .= $_FILES['files']['name'][$key].' | '; 
} 
} 
// Error message 
$errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, '    | '):''; 
$errorMsg =      !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 

if(!empty($insertValuesSQL)){ 
$insertValuesSQL = trim($insertValuesSQL, ','); 
// Insert image file name into database 
$insert = $db->query("INSERT INTO images(file_name, uploaded_on, image_width) VALUES      $insertValuesSQL"); 
if($insert){ 
$statusMsg = "Files are uploaded successfully.".$errorMsg; 
}else{ 
$statusMsg = "Sorry, there was an error uploading your file."; 
} 
}else{ 
$statusMsg = "Upload failed! ".$errorMsg; 
} 
}else{ 
$statusMsg = 'Please select a file to upload.'; 
} 
} 

?>
<form action="upload1.php" method="post" enctype="multipart/form-data">
Select Image Files to Upload:
<input type="file" name="files[]" multiple >
<input type="submit" name="submit" value="UPLOAD">
</form>

上传图像时如何获取图像元数据

可以使用php的exif扩展名。激活它和mbstring扩展。使用下面的代码片段,您可以访问包含所需所有信息的数组。

下面有一个示例["IFD0"]["ImageWidth"]。该值写入$width变量中,用于上传的每个图像。

然后,我扩展了变量$insertValuesSQL的定义,并使用"width"变量。在本例中,值被写入"image_with"您的表格"图像"

另外函数"getExif()"为缺少或不完整的退出信息添加:

<?php
function getExif( $filename, $key1, $key2)
{
$width = 0;
$allowedFiletypes = array("jpg","jpeg","tif","tiff");
$ext = explode('.', $filename);
if(in_array(strtolower($ext[1]),$allowedFiletypes))
{
if(exif_read_data($filename) == true)
{
$exif = exif_read_data($filename, 0, true);
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
if($key === $key1 AND $name === $key2){
$width = $val;
}
}
}
}
return $width;
}
else
{
print "filetype not supported";
}
}
$key1 = "IFD0";
$key2 = "ImageWidth";
foreach($_FILES['files']['name'] as $key=>$val){ 
// File upload path 
$fileName = basename($_FILES['files']['name'][$key]); 
$width = getExif( $fileName, $key1, $key2);

$targetFilePath = $targetDir . $fileName; 
// Check whether file type is valid 
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
if(in_array($fileType, $allowTypes)){ 
// Upload file to server 
if(move_uploaded_file($_FILES["files"]["tmp_name"][$key], $targetFilePath)){ 
// Image db insert sql 
$insertValuesSQL .= "('".$fileName."', NOW(), '".$width."'),"; 
}else{ 
$errorUpload .= $_FILES['files']['name'][$key].' | '; 
} 
}else{ 
$errorUploadType .= $_FILES['files']['name'][$key].' | '; 
} 
} 
// Error message 
$errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
$errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, '    | '):''; 
$errorMsg =      !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 

if(!empty($insertValuesSQL)){ 
$insertValuesSQL = trim($insertValuesSQL, ','); 
// Insert image file name into database 
$insert = $db->query("INSERT INTO images(file_name, uploaded_on, image_width) VALUES      $insertValuesSQL"); 
....
?>

最新更新