Php 为forreach()提供的参数无效

Php 为forreach()提供的参数无效,php,signaturepad,Php,Signaturepad,我正在试用签名板,我正在尝试对图像进行签名 每次我按submit,我都会被这个错误击中 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\CareMed\signature-to-image.php on line 58 下面是signature-to-image.php文件 <?php /** * Signature to Image: A supplemental

我正在试用签名板,我正在尝试对图像进行签名

每次我按submit,我都会被这个错误击中

  Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\CareMed\signature-to-image.php on line 58
下面是signature-to-image.php文件

     <?php
    /**
     *  Signature to Image: A supplemental script for Signature Pad that
      *  generates an image of the signature’s JSON output server-side using PHP.
        *
        *  @project ca.thomasjbradley.applications.signaturetoimage
        *  @author Thomas J Bradley <hey@thomasjbradley.ca>
 *  @link http://thomasjbradley.ca/lab/signature-to-image
 *  @link http://github.com/thomasjbradley/signature-to-image
 *  @copyright Copyright MMXI–, Thomas J Bradley
 *  @

    license New BSD License
     *  @version 1.1.0
     */

/**
 *  Accepts a signature created by signature pad in Json format
 *  Converts it to an image resource
 *  The image resource can then be changed into png, jpg whatever PHP GD supports
 *
 *  To create a nicely anti-aliased graphic the signature is drawn 12 times it's original size then shrunken
 *
 *  @param string|array $json
 *  @param array $options OPTIONAL; the options for image creation
 *    imageSize => array(width, height)
 *    bgColour => array(red, green, blue) | transparent
 *    penWidth => int
 *    penColour => array(red, green, blue)
 *    drawMultiplier => int
 *
 *  @return object
 */
function sigJsonToImage ($json, $options = array()) {
  $defaultOptions = array(
    'imageSize' => array(198, 55)
    ,'bgColour' => array(0xff, 0xff, 0xff)
    ,'penWidth' => 2
    ,'penColour' => array(0x14, 0x53, 0x94)
    ,'drawMultiplier'=> 12
  );

  $options = array_merge($defaultOptions, $options);

  $img = imagecreatetruecolor($options['imageSize'][0] * $options['drawMultiplier'], $options['imageSize'][1] * $options['drawMultiplier']);

  if ($options['bgColour'] == 'transparent') {
    imagesavealpha($img, true);
    $bg = imagecolorallocatealpha($img, 0, 0, 0, 127);
  } else {
    $bg = imagecolorallocate($img, $options['bgColour'][0], $options['bgColour'][1], $options['bgColour'][2]);
  }

  $pen = imagecolorallocate($img, $options['penColour'][0], $options['penColour'][1], $options['penColour'][2]);
  imagefill($img, 0, 0, $bg);

  if (is_string($json))
    $json = json_decode(stripslashes($json));

  foreach ($json as $v)
    drawThickLine($img, $v->lx * $options['drawMultiplier'], $v->ly * $options['drawMultiplier'], $v->mx * $options['drawMultiplier'], $v->my * $options['drawMultiplier'], $pen, $options['penWidth'] * ($options['drawMultiplier'] / 2));

  $imgDest = imagecreatetruecolor($options['imageSize'][0], $options['imageSize'][1]);

  if ($options['bgColour'] == 'transparent') {
    imagealphablending($imgDest, false);
    imagesavealpha($imgDest, true);
  }

  imagecopyresampled($imgDest, $img, 0, 0, 0, 0, $options['imageSize'][0], $options['imageSize'][0], $options['imageSize'][0] * $options['drawMultiplier'], $options['imageSize'][0] * $options['drawMultiplier']);
  imagedestroy($img);

  return $imgDest;
}

/**
 *  Draws a thick line
 *  Changing the thickness of a line using imagesetthickness doesn't produce as nice of result
 *
 *  @param object $img
 *  @param int $startX
 *  @param int $startY
 *  @param int $endX
 *  @param int $endY
 *  @param object $colour
 *  @param int $thickness
 *
 *  @return void
 */
function drawThickLine ($img, $startX, $startY, $endX, $endY, $colour, $thickness) {
  $angle = (atan2(($startY - $endY), ($endX - $startX)));

  $dist_x = $thickness * (sin($angle));
  $dist_y = $thickness * (cos($angle));

  $p1x = ceil(($startX + $dist_x));
  $p1y = ceil(($startY + $dist_y));
  $p2x = ceil(($endX + $dist_x));
  $p2y = ceil(($endY + $dist_y));
  $p3x = ceil(($endX - $dist_x));
  $p3y = ceil(($endY - $dist_y));
  $p4x = ceil(($startX - $dist_x));
  $p4y = ceil(($startY - $dist_y));

  $array = array(0=>$p1x, $p1y, $p2x, $p2y, $p3x, $p3y, $p4x, $p4y);
  imagefilledpolygon($img, $array, (count($array)/2), $colour);
}
电子数据交换**********

下面是将输出传输到img的php文件

<?php
session_start();
require_once 'signature-to-image.php';
$json = $_POST['output'];
$img = sigJsonToImage($json);
imagepng($img, 'signature.png');

?>

下面是包含画布和签名板的主html文件

<html><head>
  <meta charset="utf-8">
  <title>Full-Window · Signature Pad</title>
  <style>
    * {
        -webkit-user-select: none;
        -moz-user-select: none;
        user-select: none;
    }
    html, body {
        min-height: 100%;
        height: 100%;
        max-width: 100%;
        width: 100%;
        overflow: hidden;
    }
    html, body, form {
        margin: 0px;
    }
    html, form {
        padding: 0px;
    }
    html, body, fieldset {
        background: #aaa;
    }
    fieldset {
        position: absolute;
        border: 5px solid #aaa;
        background: #aaa;
        right: 0px;
        bottom: 0px;
    }
    canvas {
        outline: 5px solid #aaa;
        background: #fff;
    }
    input[type=submit], input[type=reset] {
        font-size: larger;
    }
  </style>
  <link rel="stylesheet" href="assets/jquery.signaturepad.css">
  <!--[if lt IE 9]><script src="../assets/flashcanvas.js"></script><![endif]-->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
</head>
<body>
    <form method="post" action="finaladd.php" class="sigPad">

  <div class="sig sigWrapper">
    <div class="typed"></div>
    <canvas class="pad" width="198" height="55"></canvas>
    <input type="hidden" name="output" class="output">
    <fieldset>
            <input type="reset" value="clear" />
            <input type='submit' value='submit'/>
        </fieldset>
  </div>
  <button type="submit">I accept the terms of this agreement.</button>
</form>



  <script src="assets/numeric-1.2.6.min.js"></script>
  <script src="assets/bezier.js"></script>
  <script src="jquery.signaturepad.js"></script>
  <script>
  (function(window) {
    var $canvas,
        onResize = function(event) {
          $canvas.attr({
            height: window.innerHeight,
            width: window.innerWidth
          });
        };

    $(document).ready(function() {
      $canvas = $('canvas');
      window.addEventListener('orientationchange', onResize, false);
      window.addEventListener('resize', onResize, false);
      onResize();

      $('form').signaturePad({
        drawBezierCurves: true,
        variableStrokeWidth:true,
        drawOnly: true,
        defaultAction: 'drawIt',
        validateFields: false,
        lineWidth: 0,
        output: null,
        sigNav: null,
        name: null,
        typed: null,
        clear: 'input[type=reset]',
        typeIt: null,
        drawIt: null,
        typeItDesc: null,
        drawItDesc: null
      });
    });
  }(this));
  </script>
  <script src="assets/json2.min.js"></script>

</body></html>

全窗口·签名板
* {
-webkit用户选择:无;
-moz用户选择:无;
用户选择:无;
}
html,正文{
最小高度:100%;
身高:100%;
最大宽度:100%;
宽度:100%;
溢出:隐藏;
}
html、正文、表单{
边际:0px;
}
html,表单{
填充:0px;
}
html、正文、字段集{
背景:#aaa;
}
字段集{
位置:绝对位置;
边框:5px实心#aaa;
背景:#aaa;
右:0px;
底部:0px;
}
帆布{
外形:5px实心#aaa;
背景:#fff;
}
输入[类型=提交],输入[类型=重置]{
字体大小:较大;
}
我接受本协议的条款。
(功能(窗口){
var$canvas,
onResize=函数(事件){
$canvas.attr({
高度:window.innerHeight,
宽度:window.innerWidth
});
};
$(文档).ready(函数(){
$canvas=$('canvas');
addEventListener('orientationchange',onResize,false);
addEventListener('resize',onResize,false);
onResize();
$('form')。签名Pad({
Drawbezier曲线:对,
variableStrokeWidth:true,
drawOnly:是的,
defaultAction:'drawIt',
validateFields:false,
线宽:0,
输出:null,
sigNav:null,
名称:空,
类型:null,
清除:“输入[类型=重置]”,
typeIt:null,
drawIt:null,
typeItDesc:null,
drawItDesc:null
});
});
}(本);;

可能$json变量为空

if (is_string($json)) {
    $json = json_decode(stripslashes($json));
}

if (!is_object($json)) {
    $json = new stdClass();
}

传递给函数的
$json
字符串是否为有效的json?以下是用于转换为图像的php请回送
$\u POST['output']
(您可以编辑您的问题)。回送输出时,我没有收到错误,但我的图像也不会显示。。。只是一张空白页。
if (is_string($json)) {
    $json = json_decode(stripslashes($json));
}

if (!is_object($json)) {
    $json = new stdClass();
}