function fontsize_from_width($width, $font, $text) { //initial font size: $size = 1000; $width10 = get_text_width($size, $font, $text); $newsize = floor($size * $width / $width10); return $newsize; }
/** * Return a badge in svg format. * * @since 0.0.1 * * @param string $left_text Text to display on the left side of the button. * @param string $right_text Text to display on the right side of the button. * @param string $color Hexidecimal (or other HTML acceptable color) for * right side of the button. * @param string $link URL to link to in svg image. * * @return string An svg image. */ function get_badge_svg($left_text = '', $right_text = '', $color = '#4c1', $link = '') { $left_text = sanitize_svg_text($left_text); $right_text = sanitize_svg_text($right_text); $left_text_width = get_text_width($left_text); $right_text_width = get_text_width($right_text); $width = $left_text_width + $right_text_width + 22; $right_color_start = $left_text_width + 11; $right_color_width = $width - $left_text_width - 11; $left_text_start = $left_text_width / 2 + 6; $right_text_start = $width - $right_text_width / 2 - 6; return '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="' . $width . '" height="20">' . (!empty($link) ? '<a xlink:href="' . $link . '">' : '') . '<linearGradient id="b" x2="0" y2="100%">' . '<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>' . '<stop offset="1" stop-opacity=".1"/>' . '</linearGradient>' . '<mask id="a">' . '<rect width="' . $width . '" height="20" rx="3" fill="#fff"/>' . '</mask>' . '<g mask="url(#a)">' . '<path fill="#555" d="M0 0h' . ($width - $right_color_width) . 'v20H0z"/>' . '<path fill="' . $color . '" d="M' . ($width - $right_color_width) . ' 0h' . $right_color_width . 'v20H' . ($width - $right_color_width) . 'z"/>' . '<path fill="url(#b)" d="M0 0h' . $width . 'v20H0z"/>' . '</g>' . '<g fill="#fff" text-anchor="middle" font-family="Verdana,DejaVu Sans,Geneva,sans-serif" font-size="11">' . '<text textLength="' . $left_text_width . '" lengthAdjust="spacing" x="' . $left_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $left_text . '</text>' . '<text textLength="' . $left_text_width . '" lengthAdjust="spacing" x="' . $left_text_start . '" y="14">' . $left_text . '</text>' . '<text textLength="' . $right_text_width . '" lengthAdjust="spacing" x="' . $right_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $right_text . '</text>' . '<text textLength="' . $right_text_width . '" lengthAdjust="spacing" x="' . $right_text_start . '" y="14">' . $right_text . '</text>' . '</g>' . (!empty($link) ? '</a>' : '') . '</svg>'; }
/** * Return a badge in svg format. * * @since 0.0.1 * * @param string $left_text Text to display on the left side of the button. * @param string $right_text Text to display on the right side of the button. * @param string $color Hexidecimal (or other HTML acceptable color) for * right side of the button. * * @return string An svg image. */ function get_badge_svg_v1($left_text = '', $right_text = '', $color = '#4c1') { $left_text = strtolower($left_text); $right_text = strtolower($right_text); $left_text_width = get_text_width($left_text); $right_text_width = get_text_width($right_text); $width = $left_text_width + $right_text_width + 22; $right_color_start = $left_text_width + 11; $right_color_width = $width - $left_text_width - 11; $left_text_start = $left_text_width / 2 + 6; $right_text_start = $width - $right_text_width / 2 - 6; return '<svg xmlns="http://www.w3.org/2000/svg" width="' . $width . '" height="20">' . '<linearGradient id="a" x2="0" y2="100%">' . '<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>' . '<stop offset="1" stop-opacity=".1"/>' . '</linearGradient>' . '<rect rx="3" width="' . $width . '" height="20" fill="#555"/>' . '<rect rx="3" width="' . $right_color_width . '" height="20"' . ' x="' . $right_color_start . '" fill="' . $color . '"/>' . '<path fill="' . $color . '" d="M' . $right_color_start . ' 0h4v20h-4z"/>' . '<rect rx="3" width="' . $width . '" height="20" fill="url(#a)"/>' . '<g fill="#fff" text-anchor="middle" font-family="sans-serif" font-size="12">' . '<text x="' . $left_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $left_text . '</text>' . '<text x="' . $left_text_start . '" y="14">' . $left_text . '</text>' . '<text x="' . $right_text_start . '" y="15" fill="#010101" fill-opacity=".3">' . $right_text . '</text>' . '<text x="' . $right_text_start . '" y="14">' . $right_text . '</text>' . '</g>' . '</svg>'; }