[英]How do I use imagejpeg() with $_FILES["file"]["tmp_name"]
My TensorFlow project only accepts JPEG because that is what I trained it on.我的 TensorFlow 项目只接受 JPEG,因为这是我对其进行训练的。 I use a PHP image upload form to post the image.我使用 PHP 图片上传表单来发布图片。 Here is how the image is received by the PHP file and is then used in my Python script.以下是 PHP 文件接收图像然后在我的 Python 脚本中使用的方式。
$uploaded_file = $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/classify-img");
$output = shell_exec("/usr/bin/python3 /var/www/html/classify.py /tmp/classify-img");
I have used imagejpeg()
in the past to convert any picture file extension to .jpeg我过去曾使用imagejpeg()
将任何图片文件扩展名转换为 .jpeg
When I try this it just won't work though.当我尝试这个时,它只是行不通。
$uploaded_file = imagejpeg($_FILES["file"]["tmp_name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/classify-img");
Good job helping me.帮我做的很好。 Thank you.谢谢你。
Updated & Working Code更新和工作代码
#move the uploaded file somewhere temporary
$uploaded_file = $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/classify-img");
#Is WEBP image?
if (exif_imagetype("/tmp/classify-img".$_FILES["tmp_name"]) == IMAGETYPE_WEBP) {
//create from webp temp image to be converted
$im = imagecreatefromwebp("/tmp/classify-img".$_FILES["tmp_name"]);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im, "/tmp/classify-img".$_FILES["tmp_name"], 100);
}
$output = shell_exec("/usr/bin/python3 /var/www/html/classify.py /tmp/classify-img");
The problem was as mentioned below in the comments when you use $_FILES["tmp_name"]
it gives you the file temporary file name when you use $_FILES["file"]["tmp_name"]
you get the tmp/
part added to your files path.问题如下文所述,当您使用$_FILES["tmp_name"]
时,它会为您提供文件临时文件名,当您使用$_FILES["file"]["tmp_name"]
时,您会将tmp/
部分添加到你的文件路径。
Thank you for the help everyone.谢谢大家的帮助。 Future readers I hope this helps explain how temporary files work with $_FILES and how to check for pictures and how to convert it to a JPEG without saving it to disk.未来的读者我希望这有助于解释临时文件如何使用 $_FILES 以及如何检查图片以及如何在不将其保存到磁盘的情况下将其转换为 JPEG。
Because a common way to handle temporary files is to create the file, open it, and then unlink/delete it without closing the handle.因为处理临时文件的常用方法是创建文件,打开它,然后在不关闭句柄的情况下取消链接/删除它。 This gives you the ability to use disk while ensuring that the files are deleted/unlinked when the process ends.这使您能够使用磁盘,同时确保在进程结束时删除/取消链接文件。 That is what is happening, why move_uploaded_file()
exists at all, and why you must use it before any other operation on that file.这就是正在发生的事情,为什么move_uploaded_file()
存在,以及为什么必须在对该文件进行任何其他操作之前使用它。
There is also the additional layer of "do you actually want this file to really exist?"还有额外的一层“你真的希望这个文件真的存在吗?” because otherwise anyone can POST to any PHP script and have files created in the filesystem.因为否则任何人都可以发布到任何 PHP 脚本并在文件系统中创建文件。
TLDR: You must use move_uploaded_file()
before attempting to use the file. TLDR:在尝试使用文件之前,您必须使用move_uploaded_file()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.