简体   繁体   English

如何使图像加载更快?

[英]How to make images load faster?

How can I make these images load faster? 如何使这些图像加载更快? I have a loop that displays a profiles pictures and the photos take 1 to 2.5 seconds to load. 我有一个循环,可显示个人资料图片,并且加载照片需要1到2.5秒。 Not one after another but pretty much all at once. 不是一个接一个,而是几乎一个接一个。 I tried re-sizing with PHP but that didn't really change anything. 我尝试使用PHP调整大小,但是并没有真正改变任何东西。 I am not sure how I can pre-load these images with such a loop. 我不确定如何用这样的循环预加载这些图像。 What can I do to increase load performance? 如何提高负载性能?

PHP PHP

$query = "SELECT `photoid` FROM `site`.`photos` WHERE `profileid`='$profileid'";
         try{
    $getphotos = $connect->prepare($query);
    $getphotos->execute();
    while ($array = $getphotos->fetch(PDO::FETCH_ASSOC)){
         echo '<div id="photo"><img src="photoprocess.php?photo='.$array['photoid'].'"></div>';
    }
    } catch (PDOException $e) {
         echo $e->getMessage();
    }

CSS CSS

#photo img {
    max-width:100%; 
    max-height:100%;
}

photoprocess.php photoprocess.php

       $photoid = $_GET['photo'];

    $query = "SELECT `ext` FROM `site`.`photos` WHERE `photoid`='$photoid'";
        try{
            $getphotos = $connect->prepare($query);
            $getphotos->execute();
            $array = $getphotos->fetch(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            echo $e->getMessage();
        }   

         $ext = $array['ext'];

        $image = imagecreatefromjpeg('userphotos/'.$photoid.''.$ext.'');
        $imagearray = imagejpeg($image, null);

        header('Content-type: image/jpeg'); 
            echo $imagearray;

I also have extension checks as "if statements" but those can't be slowing it down this much. 我也将扩展检查作为“ if语句”,但是这些检查不能减慢它的速度。

This part 这部分

    $image = imagecreatefromjpeg('userphotos/'.$photoid.''.$ext.'');
    $imagearray = imagejpeg($image, null);

shouldn't be necessary* and is going to be heavy on the server. 不必要*,并且在服务器上会很繁重。 You're loading (decoding) and saving (re-encoding) the image for no apparent reason. 您没有明显的原因在加载(解码)和保存(重新编码)图像。

Use something like fpasshtru() : 使用类似fpasshtru()东西:

$name = 'userphotos/'.$photoid.''.$ext.'';
$fp = fopen($name, 'rb');

header('Content-type: image/jpeg'); 

fpassthru($fp);

Or just link directly to the image. 或者只是直接链接到图像。 Unless you do some security checks or something, or the images are stored outside the web root, there is no need to go through PHP at all here. 除非进行一些安全检查或其他操作,或者图像存储在Web根目录之外,否则这里根本不需要通过PHP。

* = unless you have a very specific use case like removing EXIF data from the stored images. * =除非您有非常特殊的用例,例如从存储的图像中删除EXIF数据。 In which case you should use some form of caching. 在这种情况下,您应该使用某种形式的缓存。

Currently, you are loading image data from disk into an image buffer, which is validated by PHP. 当前,您正在将图像数据从磁盘加载到图像缓冲区中,该缓冲区已通过PHP验证。 After that, you re-encode the image data to a jpg image buffer again and output it. 之后,您再次将图像数据重新编码为jpg图像缓冲区并输出。 This is useless. 这没用。 You can just load an thruput the file (read about fpassthru ). 您可以只加载一个thruput文件(了解fpassthru )。 This is also much more memory efficient, since the image doesn't need to be entrely loaded in memory entirely at once. 这也大大提高了内存效率,因为不需要一次将映像完全全部加载到内存中。

That will be way, way faster, but it can be faster still, because I think you can use just .htaccess to redirect an url with an image id to the actual image. 这样会更快,但是仍然会更快,因为我认为您可以只使用.htaccess将具有图像ID的url重定向到实际图像。 You don't even need PHP for that. 您甚至不需要PHP。

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

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