我之前有 python selenium 代码,现在正在迁移到 javascript puppeteer rigth,但是在为任何页面上传我的屏幕截图元素时遇到一些问题。这是我上传图片的 python 代码
file = {'fileToUpload': open('./up/' + emiten + '.png', 'rb')}
resp = requests.post('/upload.php',
files=file)
print(resp.text)
我使用 .php 文件直接接收任何图像
<?php
$target_dir = "upload/";
$watermarkImagePath = 'wm.png';
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Kepentok kah? ";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Maaf gabisa usil.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
$watermarkImg = imagecreatefrompng($watermarkImagePath);
$im = imagecreatefrompng($target_file);
$marge_right = 17;
$sx = imagesx($watermarkImg);
imagecopy($im, $watermarkImg, imagesx($im) - $sx - $marge_right, 0, 0, 0, imagesx($watermarkImg), imagesy($watermarkImg));
imagepng($im, $target_file);
imagedestroy($im);
echo "Upload file ". basename( $_FILES["fileToUpload"]["name"]). " telah berhasil.";
} else {
echo "Maaf gabisa usil.";
}
}
?>
javascript 中有相同的方法吗?已经将 axios 与 formData 和 nodefetch 一起使用,但没有任何效果 我希望在不更改 .php 文件的情况下在 javascript 中有简单的代码