您好,我正在开发一个网站,用户可以在其中上传他/她的个人资料照片。我的上传页面有一个 img 标签,这样用户就可以在选择这张图片后在上传之前预览他们的图片会是什么样子,事情是图片的名称稍后会从数据库中获取。这是我的代码片段
<img id="imgs" src="http://placehold.it/200x200" alt="your image"/>
我的 uploader 是这样的
if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
{
$info = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file
$temp = explode(".",$_FILES["file"]["name"]);
$filename = rand(1,9999) . '.' .end($temp);
if ( in_array( end($info), $allow) ) // is this file allowed
{
if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . $filename ) )
{
}
}
else
{ }
}
顺便说一句,这个上传脚本来自一个教程,所以是的,你可能会在这里发现一些安全和其他问题..
我遇到的问题是当我上传尺寸为 200x200 的图像时图像看起来很好,因为在我的 css 中我定义了宽度和高度都设置为 200px 的 imgs
但是如果我上传了其他不符合我在我的 css 上设置的宽度和高度的图像,如果它更大,图像就会被拉伸(stretch)
有什么办法可以解决这个问题吗? 我也尝试直接在 img 上定义尺寸,结果显然是一样的 我可以只用 css 解决这个问题吗?
最佳答案
如果您只想依靠 CSS 来调整大小,请不要使用 width
和 height
,而是使用 max-width
和 max -高度
。示例(未经测试):
.my-img {
max-width: 200px;
max-height: 200px;
}
这样您应该能够保持宽/高比。
关于PHP 图片在上传到指定目录之前调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29336663/