【发布时间】:2014-09-04 12:02:47
【问题描述】:
下面的代码可以自行执行,但是当我用表单元素语句包装它时,我收到错误“调用未定义函数翻转在 flipit.php 第 11、14 或 17 行(取决于我在表格)。
这最终将被合并到一个文件上传页面中,允许上传者在上传时翻转和/或旋转他们的图像。所以它需要与表单一起工作。
希望有人能看出我哪里出错了。
执行的原始代码...
<?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代码块的一部分 - 如果条件块,请尝试将其放在外面。并且,请看在你所信仰的任何神灵的份上——正确地缩进你的代码,这样一看到它的哪一部分就变成什么块的一部分就变得很明显了。从现在开始,习惯于用你写的任何代码来做这件事——否则你会遇到你迟早不知道什么是什么的问题。 -
再次感谢@CBroe。将此作为您的答案发布,您将获得我的投票。