html 如何上传图片元数据到mysql列

jgwigjjp  于 2022-12-16  发布在  Mysql
关注(0)|答案(1)|浏览(145)

this is the jpeg file im trying to upload我有一个PHP代码,上传图像到MySql数据库,我想弄清楚我如何可以添加一个像GPS,相机类型,白天/黑夜等元数据列。我需要做的是添加一个元数据列,并从所有图像搜索特定的元数据。'

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

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

a1o7rhls

a1o7rhls1#

你可以使用php的exif扩展名,激活它和mbstring扩展名,通过下面的代码片段你可以访问一个包含你需要的所有信息的数组。
下面是[“IFD 0”][“ImageWidth”]的示例。对于上传的每个图像,该值写入变量$width中。
然后,我扩展了变量$insertValuesSQL的定义,并使用“width”变量扩展了insert语句。在本例中,值被写入表“images”的列“image_with”中。
此外,添加了函数“getExif()”,以解决exif信息缺失或不完整的问题:

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

相关问题