GD2在表单提交后调用函数

GD2在表单提交后调用函数

问题描述:

The code below executes fine on its own, but when I wrap it with form element statements I get the error "call to undefined function flip in flipit.php line 11, 14 or 17 (depending on the radio button I selected in the form).

This will eventually be incorporated into a file upload page allowing the uploader to flip and/or rotate their image on upload. So it needs to function with a form.

Hope someone can see where I went wrong.

Raw code that executes...

<?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
?>

Code after form statements added...

<?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
}
?>

Problems seems to be that your function definition is part of your first if code block here – try placing it outside of the conditional block:

if (…) {
  // …
}

function flip (…) {
  // …
}