gpt4 book ai didi

php - 将 GD 文件调整大小和重命名添加到 php 图片上传脚本

转载 作者:行者123 更新时间:2023-11-29 01:04:57 24 4
gpt4 key购买 nike

我有这个脚本,它允许上传图片,然后将指针存储在数据库中,指向文件系统上的文件。我从另一个 StackOverflow 问题中得到了这个脚本,除了两件事之外,它几乎完成了我需要的所有事情。我需要的第一件事是能够调整图像的大小,第二部分我刚刚读到的是重命名文件以防止用户错误等。

这里是上传文件,它从用户输入的表单中获取表单数据,并将数据写入数据库,将图片写入图像目录。

 <?php

//This is the directory where images will be saved
$target = "your directory";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];


// Connects to your Database
mysql_connect("yourhost", "username", "password") or die(mysql_error()) ;
mysql_select_db("dbName") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;

//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, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

最佳答案

关于调整大小,这里有一篇文章解释了使用 GD 将图像调整为任意大小。它具有裁剪或信箱选项以适合目标纵横比。

http://www.spotlesswebdesign.com/blog.php?id=1

对于文件名,只需将文件重命名为随机 ID,并确保您尚未使用该 ID。

do {
$filename = uniqid().'jpg';
} while (file_exists('image/path/'.$filename);

关于php - 将 GD 文件调整大小和重命名添加到 php 图片上传脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4727395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com