ARA
ARA

Reputation: 83

How to save uploaded image names in MySQL database

Part of this answered question

I have beneath php code which upload one cropped image to 3 different width and height in

  1. avatar directory with width and height 200*200
  2. avatar1 directory with width and height 500*500
  3. avatar2 directory with width and height 700*700

and renames image with MD5 random.

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

My Question

I want to save the URL of the 3 recent uploaded image inara MySQLdatabase in

  1. Avatar column= URL of image which uploaded to avatar directory
  2. Avatar1 column= URL of image which uploaded to avatar1 directory
  3. Avatar2 column= URL of image which uploaded to avatar2 directory

Database name: ara

Database user: root

Database password:

Database address: 127.0.0.1

Table Name: Profiles

Column Names: avatar, avatar1 and avatar2

Upvotes: 1

Views: 316

Answers (2)

ARA
ARA

Reputation: 83

Thanks to @Banjamin and @Qaisar

You are using $con in the function

uploadImageFile($user_name),

so you should pass the variable $con to this function, something like

uploadImageFile($user_name, $con) of course then you have to change your functions accepted arguments.

Upvotes: 1

Qaisar Satti
Qaisar Satti

Reputation: 2762

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

replace this code after

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

Upvotes: 2

Related Questions