简体   繁体   English

如何将 imagejpeg() 与 $_FILES["file"]["tmp_name"] 一起使用

[英]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.

相关问题 如何使用批处理和 Python 制作(给定大小的).tmp 文件 - How do I make .tmp files (of a given size) with batch and Python 如何检查目录中已存在的同名文件并使用python重命名新文件保存? - How do I check the existing file with same name in the directory and use python to rename new files to save? 如何获取文件名中包含特定年份的文件? - How do I grab files with specific years in the file name? 如何在 .bat 文件中使用“if __name__ == '__main__':”? - How do I use “if __name__ == '__main__':” in a .bat file? 如何使用文件中的文本作为变量名? - How do I use text from a file as a variable name? 如何使用 /tmp/ 读取 App Engine 上的文件? 我得到 [Errno 2] 没有这样的文件或目录 - How to read files on App Engine using /tmp/? I get [Errno 2] No such file or directory 如何在 Python 中创建 tmp 文件? - How can I create a tmp file in Python? /tmp 的 Android 等价物是什么?我如何获得 Python 脚本来使用它? - What is the Android equivalent of /tmp and how do I get Python scripts to use it? 如果我知道目标网页和文件扩展名但不知道文件名,我该如何使用爬虫? - How do I use crawler if I know the target web-page and file extension but not knowing the file name? 如何根据包含文件夹名称的文件将文件复制到文件夹中? - How do I copy files into folders based on the file containing the folder name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM