GD2在表单提交后调用函数


GD2 calling a function after form submit

下面的代码本身执行得很好,但当我用表单元素语句包装它时,我会在fliptit.php第11、14或17行(取决于我在表单中选择的单选按钮)中得到错误"对未定义函数flip的调用"

这最终将被纳入文件上传页面,允许上传者在上传时翻转和/或旋转图像。所以它需要用一个表单来运行。

希望有人能看到我哪里错了。

执行的原始代码。。。

<?php
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg';
$new_img = '../../Uploads/Gallery/copy_bg_200.jpg';
$image = imagecreatefromjpeg($src);
$image = flip($image,1,0); // flips horizontal
//$image = flip($image,0,1); // flips vertical
//$image = flip($image,1,1); // flips both
header("Content-type: image/jpeg");
imagejpeg($image, $new_img, 80);
imagedestroy($image);
function flip($i,$h=1,$v=0) {
$width = imagesx($i);
$height = imagesy($i);
$temp = imagecreatetruecolor($width,$height);
imagecopy($temp,$i,0,0,0,0,$width,$height);
if ($h==1) {
for ($x=0 ; $x<$width ; $x++) {
imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height);
}
imagecopy($temp,$i,0,0,0,0,$width,$height);
}
if($v==1) {
for ($x=0; $x<$height ; $x++) {
imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1);
}
}
return $i;
}
header('Location: showme.php'); // page displays the image
?>

添加表单语句后的代码。。。

<?php
$editFormAction = $_SERVER['PHP_SELF'];
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { // if form is submitted
$src = '../../Uploads/Gallery/drafting_site_bg_200.jpg';
$new_img = '../../Uploads/Gallery/copy_bg_200.jpg';
$image = imagecreatefromjpeg($src);
if ((isset($_POST["Flip"])) && ($_POST["Flip"] == "Horizontal")) {
$image = flip($image,1,0); // flips horizontal
}
if ((isset($_POST["Flip"])) && ($_POST["Flip"] == "Vertical")) {
$image = flip($image,0,1); // flips vertical
}
if ((isset($_POST["Flip"])) && ($_POST["Flip"] == "Both")) {
$image = flip($image,1,1); // flips both
}
header("Content-type: image/jpeg");
imagejpeg($image, $new_img, 80);
imagedestroy($image);
function flip($i,$h=1,$v=0) {
$width = imagesx($i);
$height = imagesy($i);
$temp = imagecreatetruecolor($width,$height);
imagecopy($temp,$i,0,0,0,0,$width,$height);
if ($h==1) {
for ($x=0 ; $x<$width ; $x++) {
imagecopy($i, $temp, $width-$x-1, 0, $x, 0, 1, $height);
}
imagecopy($temp,$i,0,0,0,0,$width,$height);
}
if($v==1) {
for ($x=0; $x<$height ; $x++) {
imagecopy($i, $temp, 0, $height-$x-1, 0, $x, $width, 1);
}
}
return $i;
}
header('Location: showme.php'); // page displays the image
}
?>

问题似乎是您的函数定义是第一个if代码块的一部分——请尝试将其放置在条件块之外:

if (…) {
  // …
}
function flip (…) {
  // …
}