调整上传图像的大小 - PHP
我正在使用 w3schools 网站上的基本上传代码,但我想添加在上传时将图像大小调整为 200 像素宽的功能。这是可以简单地添加到此代码中的东西还是我需要不同的方法来实现这一点?
这是代码:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Big Upload: " . $_FILES["file"]["name"] . "<br />";
echo "<img src='upload/" . $_FILES["file"]["name"] . "' /><br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
I am using the basic upload code from the w3schools website but I would like to add the ability to resize the image on upload to 200px wide. Is this something that can be simply added to this code or do I need a different approach to achieve this?
Here's the code:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Big Upload: " . $_FILES["file"]["name"] . "<br />";
echo "<img src='upload/" . $_FILES["file"]["name"] . "' /><br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要首先使用 phpinfo() 检查可用的图像库 - 您可能有 GD 和/或 ImageMagick。有大量关于如何编写调整大小代码的教程 - 这是一个:
You need to check which image library you have available first, using phpinfo() - you likely have GD and/or ImageMagick. There are tons of tutorials on how to write the resizing code out there - here's one: http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html