我试图通过使用HTML表单和PHP从用户获取图像文件,一切都很好但是当请求发送到服务器时,客户端获得http 500错误。我想这与一些代码问题有关但我找不到它。 PHP风暴调试器没有显示任何错误
我的html表单:
<form method="POST" action="img.php" enctype="multipart/form-data">
<textarea style="resize:none" name="data"></textarea>
<div> <input type="file" name="upfile" id="upfile" /></div>
<input name="submit" type="submit" value="encrypt" />
<input name="submit" type="submit" value="decrypt" />
</form>
我的PHP代码:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$file = $_FILES['upfile']['tmp_name'];
if (file_exists($file)) {
$imagesizedata = getimagesize($file);
if ($imagesizedata === FALSE || !testExtension($_FILES['upfile']['name']) || ($_FILES['upfile']['size'] > 50000)) {
throw new Exception("The file is not an image Or to big");
} else {
$img = imagecreatefromjpeg($file);
$data = inputValidation($_POST['data']); //example
$selection = inputValidation($_POST['submit']);
if ($selection == 'encrypt') { //image
if ($q = strlen($data) % 3 != 0) {
$q==1?$data.="--":$data.="-";
}
if ($img != false) {
$j = 0;
for ($i = 0; $i <= (strlen($data) - 3); $i += 3) {
$part = substr($data, $i, 3);
$color = getEncryptedColor($img, $part);
imagesetpixel($img, $j++, 0, $color);
}
}
//display image//+*+*+*+*+*+*+*
header('Content-Type: image/jpeg');
imagejpeg($img);
} else if ($selection == 'decrypt') {
//decrypt from image - remove on release
$dataDec = decryptAllData($img, strlen($data) / 3);
echo "".$dataDec;
}
}
} else {
throw new Exception("The upload is not a file or the file doesn't exist anymore.");
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
}
function inputValidation($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function decryptAllData($img, $datasize) {
$decrypted = "";
for ($i = 0; $i < $datasize; $i++) {
$decrypted.= decryptDataFromPixel($img, $i, 0);
}
return $decrypted;
}
function getEncryptedColor($img, $string) {
return imagecolorallocate($img, ord($string[0]), ord($string[1]), ord($string[2]));
}
function printImageValues($image, $num) {
for ($x = 0; $x < $num; $x++) {
echo "\n".imagecolorat($image, $x, $x);
}
echo "--END--";
}
function decryptDataFromPixel($img, $x, $y) {
$currpixel = imagecolorat($img, $x, $y);
$colors = imagecolorsforindex($img, $currpixel);
$str = "".chr($colors["red"]).chr($colors["green"]).chr($colors["blue"]);
return $str;
}
function testExtension($current_image) {
$extension = substr(strrchr($current_image, '.'), 1);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "gif") && ($extension != "png") && ($extension != "bmp")) {
return false;
}
return true;
}
答案 0 :(得分:0)
请检查三元运算符中的空格
$q == 1 ? $data.= "--" : $data.= "-";
或
$data.= ($q == 1 ? "--" : "-");