如何在MySQL数据库中保存上传的图像名称

时间:2015-05-14 11:03:57

标签: php html mysql image

部分回答question

我有PHP代码,它将一个裁剪的图像上传到3个不同的宽度和高度

  1. 头像 目录,宽度和高度200 * 200
  2. avatar1 目录,宽度和高度500 * 500
  3. avatar2 目录,宽度和高度700 * 700
  4. 并使用MD5随机重命名图像。

    <?php 
        function uploadImageFile() { // Note: GD library is required for this function
    
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $iJpgQuality = 100;
    
            if ($_FILES) {
                // if no errors and size less than 250kb
                if (! $_FILES['image_file']['error'] && $_FILES['image_file']['size'] < 250 * 1024) {
                    if (is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                        if (!is_dir('avatar')) {
                            mkdir('avatar');
                        }
                        // new unique filename
                        $sTempFileName = 'avatar/' . md5(time().rand());
                        // move uploaded file into cache folder
                        move_uploaded_file($_FILES['image_file']['tmp_name'], $sTempFileName);
    
                        // change file permission to 644
                        @chmod($sTempFileName, 0644);
    
                        $sResultFileName = copyImageFile('avatar', $sTempFileName, 200, 200, $iJpgQuality);
                        if ($sResultFileName) {
                            copyImageFile('avatar1', $sTempFileName, 500, 500);
                            copyImageFile('avatar2', $sTempFileName, 700, 700);
                            @unlink($sTempFileName);
    
                            return $sResultFileName;
                        }
                    }
                }
            }
        }
        return false;
    }
    
    function copyImageFile($dirName, $originImageName, $iWidth, $iHeight, $iJpgQuality = 90) {
        if (file_exists($originImageName) && filesize($originImageName) > 0) {        
            $aSize = getimagesize($originImageName); // try to obtain image info
            if (!$aSize) {
                @unlink($originImageName);
                return;
            }
    
            // check for image type
            switch($aSize[2]) {
                case IMAGETYPE_JPEG:
                    $sExt = '.jpg';
                    $vImg = @imagecreatefromjpeg($originImageName);
                    break;
                /*case IMAGETYPE_GIF:
                    $sExt = '.gif';
    
                    // create a new image from file 
                    $vImg = @imagecreatefromgif($sTempFileName);
                    break;*/
                case IMAGETYPE_PNG:
                    $sExt = '.png';
                    $vImg = @imagecreatefrompng($originImageName);
                    break;
                default:
                    @unlink($originImageName);
                    return;
            }
    
            // create a new true color image
            $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );
    
            // copy and resize part of an image with resampling
            imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']);
    
            // define a result image filename        
            if (!is_dir($dirName)) {
                mkdir($dirName);
            }
            $newImageName = $dirName . DIRECTORY_SEPARATOR . md5(time().rand()) . $sExt;
    
            // output image to file
            imagejpeg($vDstImg, $newImageName, $iJpgQuality);
            //@unlink($sTempFileName);
    
            return $newImageName;
        }
    
        return false;
    }
    
    $sImage = uploadImageFile();
    echo '<img src="'.$sImage.'" />';
    ?>
    

    我的问题

    我想在ara MySQL数据库

    中保存最近上传的3张图片的网址
    1. Avatar column =上传到头像目录的图片的网址
    2. Avatar1 column =上传到avatar1目录的图片的网址
    3. Avatar2 column =上传到avatar2目录的图片的网址
    4. 数据库名称:ara

      数据库用户:root

      数据库密码:

      数据库地址:127.0.0.1

      表名:个人资料

      列名:avatar,avatar1和avatar2

2 个答案:

答案 0 :(得分:2)

copyImageFile('avatar1', $sTempFileName, 500, 500);
copyImageFile('avatar2', $sTempFileName, 700, 700);

之后替换此代码
     include('connect/mysql.php');
    $avatar1=copyImageFile('avatar1', $sTempFileName, 500, 500);
    $avatar2=copyImageFile('avatar2', $sTempFileName, 700, 700);
$user_name = mysqli_real_escape_string($con, $_SESSION['UserName']);

       mysqli_query($con,"UPDATE profiles SET AvatarImage='".$sResultFileName."',AvatarImageBig='".$avatar1."',AvatarImageSmall='".$avatar2."' WHERE UserName = '$user_name'");   
        mysqli_close($con);

答案 1 :(得分:0)

感谢@Banjamin和@Qaisar

  

您在函数中使用$ con

uploadImageFile($user_name),
     

所以你应该将变量$con传递给这个函数   喜欢

     

uploadImageFile($user_name, $con)当然你必须改变   你的职能接受了论点。