-1

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
}
?>
Kuya
  • 7,280
  • 4
  • 19
  • 31
  • Problems seems to be that your function definition is part of your first `if` code block here – try placing it outside if the conditional block. And, please for the love of whatever deity you believe in – _indent_ your code properly, so that it becomes obvious straight away from looking at it what part of it is part of what block. Get used to doing that with whatever piece of code you write from now on – otherwise you _will_ run into problems where you don’t know what’s what any more sooner or later. – CBroe Sep 04 '14 at 14:39
  • Thanks again @CBroe. Post this as your answer and you got my vote. – Kuya Sep 04 '14 at 14:45

1 Answers1

0

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 (…) {
  // …
}
CBroe
  • 91,630
  • 14
  • 92
  • 150