You are here

function signaturefield_json_to_image in SignatureField 6

Same name and namespace in other branches
  1. 7.2 signaturefield.module \signaturefield_json_to_image()
  2. 7 signaturefield.module \signaturefield_json_to_image()
1 call to signaturefield_json_to_image()
form_type_signaturefield_value in modules/system.inc

File

./signaturefield.module, line 18
Signature Field module.

Code

function signaturefield_json_to_image($element, $json) {
  $src_image = imagecreatetruecolor($element['#width'] * 12, $element['#height'] * 12);
  $color = _signaturefield_hex2rgb($element['#color']);
  $bg = imagecolorallocatealpha($src_image, 0, 0, 0, 127);
  $pen = imagecolorallocate($src_image, $color[0], $color[1], $color[2]);
  imagefill($src_image, 0, 0, $bg);
  $json = is_string($json) ? json_decode($json) : $json;
  foreach ($json as $coord) {
    signaturefield_drawline($src_image, $coord->lx * 12, $coord->ly * 12, $coord->mx * 12, $coord->my * 12, $pen, 2 * (12 / 2));
  }

  // Preserve transparency settings.
  imagealphablending($src_image, TRUE);
  imagesavealpha($src_image, TRUE);
  $dest_image = imagecreatetruecolor($element['#width'], $element['#height']);
  $bg = imagecolorallocatealpha($dest_image, 0, 0, 0, 127);
  imagefill($dest_image, 0, 0, $bg);

  // Preserve transparency settings.
  imagealphablending($dest_image, TRUE);
  imagesavealpha($dest_image, TRUE);
  imagecopyresampled($dest_image, $src_image, 0, 0, 0, 0, $element['#width'], $element['#width'], $element['#width'] * 12, $element['#width'] * 12);
  imagedestroy($src_image);
  $dir = file_directory_path() . '/signaturefield';
  if (file_check_directory($dir, FILE_CREATE_DIRECTORY)) {
    $filepath = "{$dir}/" . time() . rand(1000, 9999) . '.png';
    imagepng($dest_image, $filepath);
    imagedestroy($dest_image);
    return serialize(array(
      'json' => json_encode($json),
      'filepath' => $filepath,
    ));
  }
  return FALSE;
}