thomasjbradley的图像签名显示为空图像

时间:2019-09-23 08:44:28

标签: php json signature

我正在运行签名板,它工作正常。我正在将该JSON签名转换为图像显示空白图像。这些是我的代码:
index.php

require_once 'signature-to-image.php';
$json = '[{"lx":40,"ly":79,"mx":40,"my":78},{"lx":331,"ly":90,"mx":331,"my":89},{"lx":348,"ly":78,"mx":331,"my":90},{"lx":358,"ly":69,"mx":348,"my":78},{"lx":372,"ly":59,"mx":358,"my":69},{"lx":392,"ly":48,"mx":372,"my":59},{"lx":412,"ly":38,"mx":392,"my":48}]'; // get the json representing the signature
$img = sigJsonToImage($json);  //get image resource from json


header('Content-Type: image/png');
imagepng($img, null, 0, PNG_NO_FILTER);
imagedestroy($img);

signature-to-image.php

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);
}


当我将Json签名转换为图像时,它显示的是空白图像。 请让我知道我的代码出了什么问题。

请帮助我
预先感谢

0 个答案:

没有答案