lctv_badges_svg.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Create svg badges.
  4. *
  5. * @package LCTVBadges\BadgesSVG
  6. * @since 0.0.3
  7. */
  8. /** Check if script is accessed directly. */
  9. if ( basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
  10. exit();
  11. }
  12. /**
  13. * Return a badge in svg format.
  14. *
  15. * @since 0.0.1
  16. *
  17. * @param string $left_text Text to display on the left side of the button.
  18. * @param string $right_text Text to display on the right side of the button.
  19. * @param string $color Hexidecimal (or other HTML acceptable color) for
  20. * right side of the button.
  21. * @param string $link URL to link to in svg image.
  22. *
  23. * @return string An svg image.
  24. */
  25. function get_badge_svg( $left_text = '', $right_text = '', $color = '#4c1', $link = '' ) {
  26. $left_text = sanitize_svg_text( $left_text );
  27. $right_text = sanitize_svg_text( $right_text );
  28. $left_text_width = get_text_width( $left_text );
  29. $right_text_width = get_text_width( $right_text );
  30. $width = $left_text_width + $right_text_width + 22;
  31. $right_color_start = $left_text_width + 11;
  32. $right_color_width = $width - $left_text_width - 11;
  33. $left_text_start = ( $left_text_width / 2 ) + 6;
  34. $right_text_start = $width - ( $right_text_width / 2 ) - 6;
  35. return
  36. '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . $width . '" height="20">' .
  37. ( ! empty( $link ) ? '<a xlink:href="' . $link . '">' : '' ) .
  38. '<linearGradient id="b" x2="0" y2="100%">' .
  39. '<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>' .
  40. '<stop offset="1" stop-opacity=".1"/>' .
  41. '</linearGradient>' .
  42. '<mask id="a">' .
  43. '<rect width="' . $width . '" height="20" rx="3" fill="#fff"/>' .
  44. '</mask>' .
  45. '<g mask="url(#a)">' .
  46. '<path fill="#555" d="M0 0h' . ( $width - $right_color_width ) . 'v20H0z"/>' .
  47. '<path fill="' . $color . '" d="M' . ( $width - $right_color_width ) . ' 0h' . $right_color_width . 'v20H' . ( $width - $right_color_width ) . 'z"/>' .
  48. '<path fill="url(#b)" d="M0 0h' . $width . 'v20H0z"/>' .
  49. '</g>' .
  50. '<g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,Geneva,sans-serif" font-size="11">' .
  51. '<text textLength="' . $left_text_width . '" lengthAdjust="spacing" x="' . $left_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $left_text . '</text>' .
  52. '<text textLength="' . $left_text_width . '" lengthAdjust="spacing" x="' . $left_text_start . '" y="14">' . $left_text . '</text>' .
  53. '<text textLength="' . $right_text_width . '" lengthAdjust="spacing" x="' . $right_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $right_text . '</text>' .
  54. '<text textLength="' . $right_text_width . '" lengthAdjust="spacing" x="' . $right_text_start . '" y="14">' . $right_text . '</text>' .
  55. '</g>' .
  56. ( ! empty( $link ) ? '</a>' : '' ) .
  57. '</svg>';
  58. }
  59. /**
  60. * Return a best guess for text width in pixels.
  61. *
  62. * Be sure to pass text through the sanitize_svg_text function first.
  63. *
  64. * @since 0.0.1
  65. *
  66. * @param string $svg_text Text.
  67. *
  68. * @return integer Guesstimate of text width in pixels.
  69. */
  70. function get_text_width( $svg_text ) {
  71. $length = strlen( $svg_text );
  72. if ( $length < 1 ) {
  73. return 0;
  74. }
  75. $char_widths = array( 'a' => 7, 'b' => 7, 'c' => 6, 'd' => 7, 'e' => 7, 'f' => 4, 'g' => 7, 'h' => 7, 'i' => 3, 'j' => 4, 'k' => 7, 'l' => 3, 'm' => 11, 'n' => 7, 'o' => 7, 'p' => 7, 'q' => 7, 'r' => 5, 's' => 6, 't' => 4, 'u' => 7, 'v' => 7, 'w' => 9, 'x' => 7, 'y' => 7, 'z' => 6, 'A' => 8, 'B' => 8, 'C' => 8, 'D' => 8, 'E' => 7, 'F' => 6, 'G' => 9, 'H' => 8, 'I' => 5, 'J' => 5, 'K' => 8, 'L' => 6, 'M' => 9, 'N' => 8, 'O' => 9, 'P' => 7, 'Q' => 9, 'R' => 8, 'S' => 8, 'T' => 7, 'U' => 8, 'V' => 8, 'W' => 11, 'X' => 8, 'Y' => 7, 'Z' => 8, '0' => 7, '1' => 7, '2' => 7, '3' => 7, '4' => 7, '5' => 7, '6' => 7, '7' => 7, '8' => 7, '9' => 7, '+' => 9, '-' => 5, '<' => 9, '>' => 9, '_' => 7, '*' => 7, '@' => 11, '$' => 7, '!' => 4, ';' => 5, ',' => 4, '.' => 4, '?' => 6, '#' => 9, ':' => 5, '=' => 9, '%' => 12, '/' => 5, ' ' => 4, '(' => 5, ')' => 5, '[' => 5, ']' => 5, );
  76. $width = 0;
  77. for ( $i = 0; $i < $length; $i++ ) {
  78. $width += isset( $char_widths[$svg_text[$i]] ) ? $char_widths[$svg_text[$i]] : 5;
  79. }
  80. return $width;
  81. }
  82. /**
  83. * Sanitize text to allow only certain characters.
  84. *
  85. * @since 0.0.4
  86. *
  87. * @param string $svg_text Text.
  88. *
  89. * @return string Sanitized text.
  90. */
  91. function sanitize_svg_text( $svg_text ) {
  92. return preg_replace( "@([^a-zA-Z0-9\+\-\<\>\_\*\@\$\!\;\,\.\?\#\:\=\%\/\(\)\[\]\ ]+)@Ui", "", $svg_text );
  93. }