如何在MySQL数据库中保存上传的图像名称 [英] How to save uploaded image names in MySQL database

查看:121
本文介绍了如何在MySQL数据库中保存上传的图像名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

部分回答了问题

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


  1. avatar 宽度和高度为200 * 200的目录
  2. avatar1 宽度和高度为500 * 500的目录 li>

并用MD5随机重命名图片。

 <$ c $ ($ _SERVER ['REQUEST_METHOD'] =='POST'){$ php 
函数uploadImageFile(){//注意:这个函数需要GD库


$ iJpgQuality = 100;

if($ _FILES){
//如果没有错误和大小小于250kb
if(!$ _FILES ['image_file'] ['error']&& amp ; $ _FILES ['image_file'] ['size']< 250 * 1024){
if(is_uploaded_file($ _ FILES ['image_file'] ['tmp_name'])){
if(! is_dir('avatar')){
mkdir('avatar');
}
//新的唯一文件名
$ sTempFileName ='avatar /'。 MD5(时间()兰特());
//将上传的文件移动到缓存文件夹中
move_uploaded_file($ _ FILES ['image_file'] ['tmp_name'],$ sTempFileName);

//将文件权限更改为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);

返回$ sResultFileName;
}
}
}
}
}
return false;
}

函数copyImageFile($ dirName,$ originImageName,$ iWidth,$ iHeight,$ iJpgQuality = 90){
if(file_exists($ originImageName)&& ($ originImageName)> 0){
$ aSize = getimagesize($ originImageName); //尝试获取图像信息
if(!$ aSize){
@unlink($ originImageName);
return;
}

//检查图像类型
switch($ aSize [2]){
case IMAGETYPE_JPEG:
$ sExt ='.jpg' ;
$ vImg = @imagecreatefromjpeg($ originImageName);
休息;
/ * case IMAGETYPE_GIF:
$ sExt ='.gif';

//从文件
$ vImg = @imagecreatefromgif($ sTempFileName)创建一个新图像;
break; * /
case IMAGETYPE_PNG:
$ sExt ='.png';
$ vImg = @imagecreatefrompng($ originImageName);
休息;
默认值:
@unlink($ originImageName);
return;
}

//创建一个新的真彩色图像
$ vDstImg = @imagecreatetruecolor($ iWidth,$ iHeight);

//通过重新采样复制和调整图像的一部分
imagecopyresampled($ vDstImg,$ vImg,0,0,(int)$ _ POST ['x1'],(int)$ _POST ['y1'],$ iWidth,$ iHeight,(int)$ _ POST ['w'],(int)$ _ POST ['h']);

//定义结果图像文件名
if(!is_dir($ dirName)){
mkdir($ dirName);
}
$ newImageName = $ dirName。 DIRECTORY_SEPARATOR。 md5(time()。rand())。 $ SEXT;

//输出图像到文件
imagejpeg($ vDstImg,$ newImageName,$ iJpgQuality);
// @ unlink($ sTempFileName);

返回$ newImageName;
}

return false;
}

$ sImage = uploadImageFile();
echo'< img src ='。$ sImage。'/>';
?>

我的问题

我想将最近上传的3张图片的网址保存在 ara MySQL 数据库中


  1. 头像列=上传到头像目录的图片网址 上传到avatar1目录的图片的URL

  2. Avatar2 column =上传到avatar2目录的图片的URL

数据库名称:ara



数据库用户:root



数据库密码:

数据库地址:127.0.0.1



姓名:个人资料



列名:avatar1和avatar2

解决方案

  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']);
$ b $ mysqli_query($ con,更新配置文件SET AvatarImage ='。$ sResultFileName。'',AvatarImageBig ='。$ avatar1。',AvatarImageSmall ='。$ avatar2。'WHERE UserName ='$ user_name');
mysqli_close($ con);


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

解决方案

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

这篇关于如何在MySQL数据库中保存上传的图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆