lctv-badges-svg-v1.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * LiveEdu.tv Badges Custom SVG Pill Generator
  4. *
  5. * @package LctvBadges\Images
  6. * @version 0.0.9
  7. * @since 0.0.1
  8. */
  9. /** Check if script is accessed directly. */
  10. if ( basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
  11. exit();
  12. }
  13. /**
  14. * Return a badge in svg format.
  15. *
  16. * @since 0.0.1
  17. *
  18. * @param string $left_text - Text to display on the left side of the button.
  19. * @param string $right_text - Text to display on the right side of the button.
  20. * @param string $color - Hexidecimal (or other HTML acceptable color) for
  21. * right side of the button.
  22. * @param string $link_url - LCTV channel page to wrap svg image as hyperlink.
  23. *
  24. * @return string An svg image.
  25. */
  26. function make_badge_svg_v1( $left_text = '', $right_text = '', $color = '#4c1', $link_url = '' ) {
  27. $left_text = sanitize_svg_text( $left_text );
  28. $right_text = sanitize_svg_text( $right_text );
  29. $left_text_width = get_text_width( $left_text );
  30. $right_text_width = get_text_width( $right_text );
  31. $width = $left_text_width + $right_text_width + 22;
  32. $right_color_start = $left_text_width + 11;
  33. $right_color_width = $width - $left_text_width - 11;
  34. $left_text_start = ( $left_text_width / 2 ) + 6;
  35. $right_text_start = $width - ( $right_text_width / 2 ) - 6;
  36. $wrap_in_a = ! empty( $link_url );
  37. return
  38. '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . $width . '" height="20">' .
  39. ( ( $wrap_in_a ) ? '<a xlink:href="' . $link_url . '">' : '' ) .
  40. '<linearGradient id="b" x2="0" y2="100%">' .
  41. '<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>' .
  42. '<stop offset="1" stop-opacity=".1"/>' .
  43. '</linearGradient>' .
  44. '<mask id="a">' .
  45. '<rect width="' . $width . '" height="20" rx="3" fill="#fff"/>' .
  46. '</mask>' .
  47. '<g mask="url(#a)">' .
  48. '<path fill="#555" d="M0 0h' . ( $width - $right_color_width ) . 'v20H0z"/>' .
  49. '<path fill="' . $color . '" d="M' . ( $width - $right_color_width ) . ' 0h' . $right_color_width . 'v20H' . ( $width - $right_color_width ) . 'z"/>' .
  50. '<path fill="url(#b)" d="M0 0h' . $width . 'v20H0z"/>' .
  51. '</g>' .
  52. '<g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,Geneva,sans-serif" font-size="11">' .
  53. '<text textLength="' . $left_text_width . '" lengthAdjust="spacing" x="' . $left_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $left_text . '</text>' .
  54. '<text textLength="' . $left_text_width . '" lengthAdjust="spacing" x="' . $left_text_start . '" y="14">' . $left_text . '</text>' .
  55. '<text textLength="' . $right_text_width . '" lengthAdjust="spacing" x="' . $right_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $right_text . '</text>' .
  56. '<text textLength="' . $right_text_width . '" lengthAdjust="spacing" x="' . $right_text_start . '" y="14">' . $right_text . '</text>' .
  57. '</g>' .
  58. ( ( $wrap_in_a ) ? '</a>' : '' ) .
  59. '</svg>';
  60. }
  61. /**
  62. * Return a best guess for text width in pixels.
  63. *
  64. * Be sure to pass text through the sanitize_svg_text function first.
  65. *
  66. * @since 0.0.1
  67. *
  68. * @param string $svg_text Text.
  69. *
  70. * @return integer Guesstimate of text width in pixels.
  71. */
  72. function get_text_width( $svg_text ) {
  73. $length = strlen( $svg_text );
  74. if ( $length < 1 ) {
  75. return 0;
  76. }
  77. $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, );
  78. $width = 0;
  79. for ( $i = 0; $i < $length; $i++ ) {
  80. $width += isset( $char_widths[$svg_text[$i]] ) ? $char_widths[$svg_text[$i]] : 5;
  81. }
  82. return $width;
  83. }
  84. /**
  85. * Sanitize text to allow only certain characters.
  86. *
  87. * @since 0.0.4
  88. *
  89. * @param string $svg_text Text.
  90. *
  91. * @return string Sanitized text.
  92. */
  93. function sanitize_svg_text( $svg_text ) {
  94. return preg_replace( "@([^a-zA-Z0-9\+\-\<\>\_\*\@\$\!\;\,\.\?\#\:\=\%\/\(\)\[\]\ ]+)@Ui", "", $svg_text );
  95. }