允许用户上传个人资料照片
奇怪的是,我找不到有关此主题的任何信息:用户个人资料图片。
有没有针对以下问题的 PHP 解决方案?
- 允许用户从硬盘上传图片以存储在我们的服务器上
- 如果图片太大,则缩小图片;如果它太小(高度/宽度),则扩展
它 自己构建容易吗?或者已经有一个图书馆了?
Oddly enough, I can't find any info on this topic: user profile pictures.
Is there a PHP solution for the following?
- Allows users to upload a pic from their hard drive for storage on our server
- Shrinks the image if it's too big; expands it if it's too small (height/width)
Is this easy to build yourself? Or is there already a library for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要上传和存储图像,我建议为图片生成随机文件名,而不是保留原始文件名。这将防止冲突并增加安全措施。我会建议一个很长的名字;只是随机数字和字母。
然后,将随机文件名与您的用户信息一起存储在数据库记录中。这样,您就不必担心文件名和用户名不同步,而且,正如我之前所说,有人将无法猜测 Joe Schmoe 的个人资料图片存储为 JoeSchmoe.jpg。
当您到达图像调整大小部分时,请使用下面我修改的函数(来自 PHP 用户评论)。在您的情况下,
“images/smallerpicture.jpeg”
可能会被“images/<这里是一些随机名称>.jpeg”
替换。示例:
功能:
支持gif、jpg、png。
To upload and store the image, I would recommend generating a random filename for the picture, instead of keeping the original file name. This will prevent conflicts and also add a measure of security. I would suggest a pretty long name; just random numbers and letters.
Then, store the random file name in the database record along with your user info. This way, you won't have to worry about file names and user names getting out of sync, and, as I said earlier, someone won't be able to guess that Joe Schmoe's profile picture is stored as JoeSchmoe.jpg.
For when you get to the image-resizing part, use this function below I modified (from the PHP user comments). In your case,
"images/smallerpicture.jpeg"
would probably be replaced by"images/<some random name here>.jpeg"
.Example:
Function:
It supports gifs, jpgs, and pngs.
也许这个基本的图解可以帮助你:
构建你自己的教程 CMS
要使用 php 操作图像,非常简单:
PHP :GD
1
祝你好运
Maybe this basic tut can hep you:
Building your own Tutorial CMS
To manipulate images with php, it is very easy with:
PHP: GD
1
Good luck
很难回答这个问题,因为“简单”是一个非常相对的术语。对于有经验的 php 编码人员来说这很容易,但对于初学者来说就不那么容易了。是的,有一些用于调整图像大小的库。您可能需要自己处理上传处理,这非常简单
要调整大小,您可以查看pear 库
It's hard to answer this because "easy" is very relative term. For an experiences php coder it's easy, for a beginner it's not so easy. Yes, there are libraries for image resizing. You probably will have to handle processing of upload yourself, it's really easy
For resizing you can take a look at pear library here