我的脚本可以上传头像/个人资料图片,但无法更改尺寸(高度和宽度)

时间:2014-09-17 13:54:45

标签: php html image

感谢您的支持,非常感谢。

我是PHP的新手,我听说我可以从那些位于Stackoverflow的PHP专家那里找到我的解决方案。

我刚刚买了this脚本,现在制作人停止提供支持。

头像上传表单

http://i.stack.imgur.com/YO7PD.jpg

我的问题

该脚本可以为每个用户上传配置文件,但它不会调整图像大小。 如果用户上传了一个2 MB的图像,那么该脚本会在整个网站上使用2 mb图像,这会使我的网站运行速度变慢。

我希望脚本应该将图像大小调整为<​​/ p>

([width = 100px and height = auto]和

[width = 19px and height = auto])

因此我使用较小的图像(例如~150 kb和~55kb)并让我的网站运行得更快。

这是处理上传

的avatar.php文件
    <?php
// declare variables
$msg = '';
$f_avatar_image = '';
// ------------------------------------------------------------
// UPLOAD AVATAR
// ------------------------------------------------------------
if(isset($_POST['btnUploadAvatar']) && !empty($_FILES['fileUpload']['name']))
{
    // create variables
    $avatar_directory = AVATAR_FILE_DIRECTORY;
    $file_name = $_FILES['fileUpload']['name'];
    $file_type = $_FILES['fileUpload']['type'];
    $file_size = $_FILES['fileUpload']['size'];
    $file_size_limit = AVATAR_FILE_SIZE;
    $calc_kilobites = 1024;
    $file_size_kb = round($file_size / $calc_kilobites, 2);
    $temp_file_name = $_FILES['fileUpload']['tmp_name'];
    $upload_error = $_FILES['fileUpload']['error'];


    // create unique file name
    $unique_file_name = $user_name.'-'.$file_name;
    $avatar_img_url = AVATAR_IMAGE_URL.$user_name.'-'.$file_name;

    // if upload error display error message
    if($upload_error > 0)
    {
        echo 'ERROR:' . $upload_error;
    }

    // if no upload error - check for file types
    if($upload_error == 0 &&
    $file_type == 'image/gif' ||
    $file_type == 'image/jpeg' ||
    $file_type == 'image/png' )
    {
        // if file size is within limits
        if($file_size <= $file_size_limit)
        {
            // move uploaded file to assigned directory
            if(move_uploaded_file($temp_file_name, $avatar_directory . $unique_file_name))

            {
                // get user id
                $get_user_id = mysqli_query($conn, "SELECT UserId FROM users WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);

                // if user id exist
                if(mysqli_num_rows($get_user_id) == 1 )
                {
                    $row = mysqli_fetch_array($get_user_id);
                    $user_id = $row['UserId'];

                    // check if user profile already exist
                    $check_user_profile = mysqli_query($conn, "SELECT UserId FROM profiles WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);

                    // if user profile exist - update
                    if(mysqli_num_rows($check_user_profile) == 1 )
                    {
                        // update profiles
                        $update_profile = mysqli_query($conn, "UPDATE profiles SET AvatarImage = '$avatar_img_url' WHERE UserName = '$user_name'") or die($dataaccess_error);

                        if(mysqli_affected_rows($conn) > 0)
                        {
                            echo 'Upload Success! <br/>';
                            echo 'File Name: '.$file_name.'<br/>';
                            echo 'File Type: '.$file_type.'<br/>';
                            echo 'File Size: '.$file_size_kb.' Kb <br/>';
                            $msg = $profile_update_success;
                        }
                        else
                        {
                            $msg = $profile_update_failed;
                        }
                    }
                    else
                    {
                        // create profile
                        $insert_profile = mysqli_query($conn, "INSERT INTO profiles(UserId,UserName,AvatarImage) VALUES($user_id,'$user_name','$avatar_img_url')") or die($dataaccess_error);

                        if(mysqli_affected_rows($conn) > 0)
                        {
                            echo 'Upload Success! <br/>';
                            echo 'File Name: '.$file_name.'<br/>';
                            echo 'File Type: '.$file_type.'<br/>';
                            echo 'File Size: '.$file_size_kb.' Kb <br/>';
                            $msg = $profile_update_success;
                        }
                        else
                        {
                            $msg = $profile_create_failed;
                        }
                    }
                }
                else
                {
                    // user id not found
                    $msg = $profile_update_failed2;
                }
            }
            else
            {
                $msg = $avatar_upload_failed;
            }
        }
        else
        {
            $msg = $avatar_file_too_large;
        }
    }
    else
    {
        $msg = $avatar_wrong_file_type;
    }
}
elseif(isset($_POST['btnUploadAvatar']) && empty($_FILES['fileUpload']['name']))
{
    $msg = $avatar_empty;
}


// ------------------------------------------------------------
// DISPLAY AVATAR ON PAGE LOAD
// ------------------------------------------------------------
if($user_name)
{
    // get user id
    $get_avatar_image = mysqli_query($conn, "SELECT AvatarImage FROM profiles WHERE UserName = '$user_name' Limit 1") or die($dataaccess_error);

    if(mysqli_num_rows($get_avatar_image) == 1)
    {
        $row = mysqli_fetch_array($get_avatar_image);
        if($row['AvatarImage'] != 'NULL' && $row['AvatarImage'] != '')
        {
            $f_avatar_image = $row['AvatarImage'];
        }
        else
        {
            $f_avatar_image = AVATAR_IMAGE_URL.DEFAULT_AVATAR_IMAGE;
        }
    }
    else
    {
        $f_avatar_image = AVATAR_IMAGE_URL.DEFAULT_AVATAR_IMAGE;
    }
}
?>

这是avatar.html.php文件格式

    <?php require_once(ROOT_PATH.'user/modules/accordion/avatar.php'); ?>
<div class="profileWrap">
  <form name="frmAvatar" method="post" action="" enctype="multipart/form-data" class="htmlForm">
    <div class="infoBanner2">
      <p>REQUIREMENTS:  File Size: <?php echo AVATAR_FILE_SIZE / 1024 ?> kb max. File Type: gif, jpg, png</p>
    </div>
    <!-- error msgs -->
    <ul>
    <?php echo $msg; ?>
    </ul>
    <p><input name="selectFile" type="image" src="<?php echo $f_avatar_image; ?>" class="img"></p>
    <p><label for="fileUpload">Avatar Image:</label><input name="fileUpload" type="file" id="fileUpload" maxlength="255" ></p>
    <input name="btnUploadAvatar" type="submit" value="Upload" class="gvbtn btn" onclick="return confirm('Are You READY to UPLOAD?');"/>
  </form>
</div>

avatar.php文件链接到配置文件(web.config.php)文件

// ------------------------------------------------------------
// 16. AVATAR IMAGE FILE
// ------------------------------------------------------------
define('AVATAR_FILE_SIZE', 2097152); // 50 Kb max. -> 1 kilobyte = 1024 bytes
define('AVATAR_FILE_DIRECTORY', ROOT_PATH.'user/upload/avatars/'); // upload directory
define('AVATAR_IMAGE_URL', SITE_URL.'user/upload/avatars/'); // default avatar url
define('DEFAULT_AVATAR_IMAGE', 'default-avatar.png'); // default avatar image

如果您需要提出任何我准备回答的问题。 让我感谢那个回答它的人

1 个答案:

答案 0 :(得分:1)