上传 PHP 之前调整图像大小
我不知道如何在 PHP 中调整图像大小,我的代码是:
for ($index = 1; $index <= 2; $index++) {
if (!empty($_FILES["pic$index"]["name"])) {
$ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
$dir = "../gallery/$mkdir";
HERE I NEED THE RESIZE OF THE TMP FILE OF IMAGE
move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext");
}
}
$mkdir = 画廊文件夹的名称(有很多画廊)。
$dir = 图片放置的位置。
$ext = 图像类型(png、gif 或 jpg)。
foreach 循环运行两次,因为您可以上传两张图片。
这个脚本运行良好,我只需要调整大小,但我不知道该怎么做。
I have no idea how to resize image in PHP, my code is:
for ($index = 1; $index <= 2; $index++) {
if (!empty($_FILES["pic$index"]["name"])) {
$ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
$dir = "../gallery/$mkdir";
HERE I NEED THE RESIZE OF THE TMP FILE OF IMAGE
move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext");
}
}
$mkdir = the name of the gallery's folder (there are many galleries).
$dir = where the pics will be placed.
$ext = the type of the image (png, gif or jpg).
foreach loop runs two times because you can upload two pics.
This script is working good, I just need to do resize and I dont have an idea how to do it..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我用来调整图像大小的代码。
就我而言,我向函数提供原始文件名,然后提供缩略图文件名。
您可以轻松地根据您的情况进行调整。
返回码只是为了区分不同类型的错误。
通过回顾该代码,我不喜欢“幻数”技巧。我必须改变这一点(例如例外)。
使用 move_uploaded_file 移动它(推荐),然后您可以调整它的大小并将其发送到最终目的地。您甚至可能不需要“.tmp”,您可以使用。
Here is the code I'm using to resize images.
In my case I give to the function the original file name and then the thumbnail file name.
You can adapt it for your case very easily.
The return codes are just here to differentiate between different types of errors.
By looking back at that code, I don't like the "magic number" trick. I'm gonna have to change that (by exceptions for example).
Use move_uploaded_file to move it (recommanded) and then you can resize it and send it to it's final destination. You might not even need the ".tmp", you can use.
请记住,您正在处理的图片已经上传到服务器上。您实际上想在将图片存储在“安全位置”之前调整图片大小。
$_FILES["pic$index"]["tmp_name"]
可能是 /tmp/somepicturesnameKeep in mind that the picture you are dealing with is already uploaded on the server. You actualy want to resize picture before storing it in "safe place".
$_FILES["pic$index"]["tmp_name"]
is probably /tmp/somepicturesname