简体   繁体   English

需要有关PHP头像脚本的帮助

[英]Need help on PHP avatar script

I made a simple PHP script to display a users avatar image they uploaded but I was wondering how can I improve the script below. 我制作了一个简单的PHP脚本来显示他们上传的用户头像图像,但我想知道如何改进以下脚本。

Is there something I can do to improve the load time on the users avatar images that are very big? 我可以做些什么来改善非常大的用户头像图像上的加载时间? If so how can I add it to my code? 如果可以,如何将其添加到我的代码中?

Here is the code below: 这是下面的代码:

<?php

list($width, $height) = getimagesize("images/pic.jpg");

if ($width >= 180){
    $width = 180;
    echo '<img src="images/pic.jpg" width="' .$width. '" />';
} else {
    echo '<img src="images/pic.jpg" width="' .$width. '" height="' .height. '" />';
}

?>

The best way to decrease load time would be to resize the image files in your code. 减少加载时间的最佳方法是调整代码中图像文件的大小。 What you're doing above is delivering the full sized image to the client and using HTML to instruct the browser to resize the image. 上面您要做的是将完整大小的图像交付给客户端,并使用HTML指示浏览器调整图像大小。

UPDATE: You can create thumbnails using phpthumb . 更新:您可以使用phpthumb创建缩略图。 Make sure you have GD installed as it is a dependency. 确保已安装GD,因为它是依赖项。

I would require a certain size avatar from the user instead this will keep load times consistent as well as allow you to maintain your same style from your CSS and not have your design varying. 我需要用户提供一定大小的头像,以保持加载时间的一致性,并允许您从CSS保持相同的样式,而不会改变您的设计。

Hope this helps, David 希望这会有所帮助,大卫

我会事先用GD将上传的头像文件调整大小并转换为180x ...或... x180 PNG图片。

Since it looks like you are restricting your users to 180px image width anyway, I would resize the picture on upload. 由于看起来您无论如何都将用户的图片宽度限制为180像素,因此我会在上传时调整图片大小。 Then you can keep the "thumbnail" version for your quick display and only show the "full" version if someone visits a user profile. 然后,您可以保留“缩略图”版本以快速显示,并且仅当有人访问用户个人资料时才显示“完整”版本。

Since you are controlling the upload, you can even make the thumbnail square so that all avatars perfectly match in dimensions. 由于您正在控制上传,因此您甚至可以将缩略图设置为正方形,以便所有头像的尺寸完全匹配。 To do that, you could just crop the full size to a square. 为此,您可以将整个尺寸裁剪为一个正方形。

In either case, I would definitely not simply rely on the browser to resize the image for you. 无论哪种情况,我绝对不会简单地依靠浏览器来为您调整图像大小。 In your example, you are still sending the full image to the browser. 在您的示例中,您仍将完整图像发送到浏览器。

You should consider scaling the image to a max width of 180 pixels on upload and then serve this cached image instead of requesting the size on every serve. 您应该考虑在上传时将图片缩放到最大180像素的宽度,然后投放此缓存的图片,而不是每次请求都提供尺寸。

EDIT: The examples from the PHP docs ( imagecopyresized() ) should give you an idea. 编辑:来自PHP文档( imagecopyresized() )的示例应该给您一个想法。 You could then simply save the resized image as the username / user ID and serve <img src="images/resized/' . USER-ID-OR-NAME-GOES-HERE . '.jpg" alt="Avatar for USERNAME" /> , knowing that the image exists (given that the user is valid). 然后,您可以简单地将调整大小后的图像另存为用户名/用户ID,并提供<img src="images/resized/' . USER-ID-OR-NAME-GOES-HERE . '.jpg" alt="Avatar for USERNAME" /> ,知道该图像存在(假设用户有效)。

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

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