PHP 图像大小调整
我有一个脚本,可以将文件上传到服务器并将文件名添加到数据库中,但我想要在上传之前限制图像的最大尺寸。因此,如果我上传 1000 x 500 的图像,它将受到限制,但仍保留其尺寸,并将更改为 200 x 100,但 300 x 300 的图像将被限制为 200 x 200
<?php
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("hostname", "username", "password") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
感谢您的帮助
I've got a script that uploads files to the server as well as adds the filename to a database, but what I'd like to do it restrict the maximum dimensions of the image before uploading. So if I upload an image that is 1000 x 500 it will be restricted but still keep it's dimensions and will be changed to 200 x 100, but an image that is 300 x 300 will be restricted to 200 x 200
<?php
//This is the directory where images will be saved
$target = "uploads/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("hostname", "username", "password") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `table` (name, photo) VALUES ('$name','$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您无法在上传图像之前调整图像大小。 (我可能是错的!)但是,当您上传图像时,它会进入临时文件。您可以调整临时图像的大小并将调整大小的图像复制到其最终目的地。
由于(似乎)您希望保持宽度恒定,因此您实际上不需要进行大量比率测试。
更新:
您应该能够简单地使用它来代替原始代码。大部分都没有改变。
To my knowledge, you can’t resize the image before uploading it. (I could be wrong!) However, when you upload the image it goes into a temporary file. You can resize the temporary image and copy the resized image to its final destination.
Since (it seems) you want to keep the width constant, you don’t really need to do a lot of ratio tests.
Update:
You should be able to simply use this in place of your original code. Most of it is unchanged.
我过去使用这个函数来生成适合给定尺寸并保持纵横比的缩略图,也许您可以以某种方式使用它:
I used in the past this function to generate thumbnails that fit in given dimensions keeping aspect ratio, maybe you can use it somehow: