GFPDFHelperFieldsField_Signature

Controls the display and output of a Gravity Form field.

Defined (1)

The class is defined in the following location(s).

/src/helper/fields/Field_Signature.php  
  1. class Field_Signature extends Helper_Abstract_Fields { 
  2.  
  3. /** 
  4. * Display the HTML version of this field 
  5. * @param string $value 
  6. * @param bool $label 
  7. * @return string 
  8. * @since 4.0 
  9. */ 
  10. public function html( $value = '', $label = true ) { 
  11. $value = $this->value(); 
  12.  
  13. $output = ( ! $this->is_empty() ) ? $value['img'] : ''; /** prevents image loading error when non existant */ 
  14.  
  15. return parent::html( $output ); 
  16.  
  17. /** 
  18. * Used to check if the current field has a value 
  19. * @since 4.0 
  20. */ 
  21. public function is_empty() { 
  22. $value = $this->value(); 
  23.  
  24. /** if the path is both a file and is readable then the field is NOT empty */ 
  25. if ( is_file( $value['path'] ) && is_readable( $value['path'] ) ) { 
  26. return false; 
  27.  
  28. return true; 
  29.  
  30. /** 
  31. * Return the HTML form data 
  32. * @return array 
  33. * @since 4.0 
  34. */ 
  35. public function form_data() { 
  36.  
  37. $data = []; 
  38.  
  39. $value = $this->value(); 
  40.  
  41. $data['signature'][] = $value['img']; 
  42. $data['signature_details'][] = $value; 
  43. $data['signature_details_id'][ $this->field->id ] = $value; 
  44.  
  45. return $data; 
  46.  
  47. /** 
  48. * Get the standard GF value of this field 
  49. * @return string|array 
  50. * @since 4.0 
  51. */ 
  52. public function value() { 
  53. if ( $this->has_cache() ) { 
  54. return $this->cache(); 
  55.  
  56. /** Get our signature details */ 
  57. $signature_name = $this->get_value(); 
  58. $signature_upload_url = GFFormsModel::get_upload_url_root() . 'signatures/'; 
  59. $signature_upload_path = GFFormsModel::get_upload_root() . 'signatures/'; 
  60. $signature = $signature_upload_path . $signature_name; 
  61.  
  62. /** Get some sane signature defaults */ 
  63. $width = 75; 
  64. $height = 45; 
  65. $html = '<img src="' . $signature . '" alt="Signature" width="' . $width . '" />'; 
  66.  
  67. /** If we can load in the signature let's optimise the signature size for PDF display */ 
  68. if ( is_file( $signature ) ) { 
  69.  
  70. /** 
  71. * [0] Is the original width 
  72. * [1] Is the original height 
  73. */ 
  74. $signature_details = getimagesize( $signature ); 
  75.  
  76. /** 
  77. * For optimal image resolution at 96dpi we'll divide the original width by 3 
  78. * Add filters to allow the user to change the signature image width in the PDF. 
  79. * @param integer The original image width divided by 3 
  80. * @param integer The original image width 
  81. */ 
  82. $optimised_width = apply_filters( 'gfpdfe_signature_width', $signature_details[0] / 3, $signature_details[0] ); /** backwards compat */ 
  83.  
  84. /** See https://gravitypdf.com/documentation/v4/gfpdf_signature_width/ for more details about this filter */ 
  85. $optimised_width = apply_filters( 'gfpdf_signature_width', $optimised_width, $signature_details[0] ); 
  86.  
  87. $optimised_height = $signature_details[1] / 3; 
  88. $html = str_replace( 'width="' . $width . '"', 'width="' . $optimised_width . '"', $html ); 
  89.  
  90. /** override the default width */ 
  91. $width = $optimised_width; 
  92. $height = $optimised_height; 
  93.  
  94. /** 
  95. * Build our signature array 
  96. */ 
  97. $value = [ 
  98. 'img' => $html,  
  99. 'path' => $signature,  
  100. 'url' => $signature_upload_url . $signature_name,  
  101. 'width' => $width,  
  102. 'height' => $height,  
  103. ]; 
  104.  
  105. $this->cache( $value ); 
  106.  
  107. return $this->cache();