简体   繁体   English

加载图像缓慢的laravel jQuery

[英]Loading image slow laravel jQuery

Please check my js code below. 请检查下面的我的js代码。 It is written for getting profile images from folder. 它是为从文件夹获取个人资料图像而编写的。 I have getting profile images and it's displayed correctly. 我有个人资料图片,并且显示正确。

I have a default image, If users have no profile image the default image will display. 我有一个默认图像,如果用户没有个人资料图像,则将显示默认图像。

My problem is when page refresh, If a user has profile image default image is showing initially(for a millisecond) and then profile image shows. 我的问题是页面刷新时,如果用户有个人资料图像,则最初显示图像(一毫秒),然后显示个人资料图像。 How can I solve this. 我该如何解决。 I think it's a loading problem from jQuery. 我认为这是jQuery的加载问题。

test.js test.js

$(function () {

    $.get("/getDetails")
        .done(function (data) {
            $("#profile_image").css('background-image', 'url(/profile.png)');
            if (data.filename == 'Profile') {
                $("#profile_image").css('background-image', 'url(' + data.route + ')');
                $("#deleteProfileImage").show();
            }
    });

});

html page: html页面:

<div id="profile_image" style="background: url(/profile.png) no-repeat center center;">

controller page 控制器页面

public function show()
{
    $id                  = Auth::user()->id;
    $details             = User::select('id', 'created_at')->findOrFail($id);
    $encrypt             = md5($details->id.$details->created_at);
    $directories         = Storage::files($encrypt);                                             // Listout Files
    foreach($directories as $values){
        $split_folder_file = explode('/', $values);           //08d16e9f44699e9334834833c02b7b8e/Profile.png
        $splitted_file     = end($split_folder_file);         //Profile.png
        $explode_filename  = explode('.', $splitted_file);    //explode(Profile.png)
        $explode_name      = $explode_filename[0];            //Profile
        $file_extension    = $explode_filename[1];            //png
        if ($explode_name == 'Profile') {
            switch( $file_extension ) {
                case "gif": $ctype="image/gif"; break;
                case "png": $ctype="image/png"; break;
                case "jpeg":
                case "jpg": $ctype="image/jpeg"; break;
                default:
            }
            $file = Storage::disk('local')->get($encrypt.'/'.$splitted_file);
            return response($file, 200)
                ->header('Content-Type', $ctype);
        }
    }
}

Try this: 尝试这个:

$(function () {

    $("#profile_image").css('background-image', 'url(/profile.png)');
    $.get("/getDetails")
        .done(function (data) {
            if (data.filename == 'Profile') {
                $("#profile_image").css('background-image', 'url(' + data.route + ')');
                $("#deleteProfileImage").show();
            }
    });

});

It was showing foe initial milliseconds because it's written in your code that it displays the default image first and then checks if the image is present for user and then replaces it with the default image. 之所以显示最初的毫秒数是因为它是在您的代码中编写的,它首先显示默认图像,然后检查该图像是否为用户显示,然后将其替换为默认图像。

I moved default image to the else condition so that its only showed when uploaded user image is not avaialble. 我将默认图片移至else条件,以便仅在上传的用户图片不可用时才显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM