IMGText is a PHP library that generates a PNG image for each word in a string,
and then generates HTML markup to display those images as if they were text.
This documentation is also available in Korean.
$imgtext = new IMGText;
$imgtext->cache_url_prefix = '.';
$imgtext->cache_local_dir = dirname(__FILE__);
$imgtext->font_dir = '/usr/share/fonts/truetype/nanum';
$imgtext->font_name = 'NanumBrush';
$imgtext->font_size = 32;
$imgtext->color = '#404040';
$imgtext->shadow = true;
$imgtext->shadow_blur = 2;
echo $imgtext->get_html("네이버 ‘나눔손글씨 붓’으로 작성된 제목");
Most of the time, Western text on the Web can be easily stylized with web fonts.
Since the majority of Western writing systems contain only a few dozen glyphs,
web fonts tend to be small – a few dozen KB – and thus quick to download.
This is unfortunately not the case for a lot of Asian scripts, such as Korean,
Japanese, and Chinese (both traditional and simplified). These languages
contain thousands of glyphs, resulting in web fonts that are several MB in size.
As a result, many Asian web designers are stuck with OS default fonts,
or resort to images. (Other options, such as <canvas>, is rarely used because
the affected countries also have a major infestation of outdated browsers.)
The former looks hideous, especially at large font sizes used in headings.
Using images, on the other hand, suffers from several disadvantages:
IMGText is meant to solve these problems, not by addressing the source of
the problems (lack of usable web fonts for Asian scripts), but by acknowledging
the sad reality and automating image generation and web app integration
as much as possible.
<img> tags with the imgtext class.To use IMGText, include the imgtext.php file into your project.
You will also need:
Please see the example above for an overview of how IMGText works.
Here is a list of available methods, properties, and their signatures:
string cache_url_prefix (required):
The web-accessible path of the cache directory.
Generated HTML markup will contain references to this directory.
Do not include a trailing slash.
Example: ./images/cache
string cache_local_dir (required):
The local path that corresponds to the same cache directory.
Generated images and metadate will be stored in this directory.
Do not include a trailing slash.
Example: /srv/www/website_name/htdocs/images/cache
string font_dir (required):
The local path where fonts are stored.
Do not include a trailing slash.
Example: /usr/share/fonts.
string font_ext (optional):
The extension of your font files. The default value is ttf.
Change this if you want to use OTF fonts.
This value is case-sensitive.
string font_name (required):
The name of the font file that you want to use.
This value is case-sensitive.
int font_size (optional):
The size of the font that you want to use, measured in points.
The default value is 32 points.
int image_height (optional):
The height of the generated images, in pixels.
You can use this argument to force a specific height.
By default, the height is automatically determined.
hex color (optional):
The color of the text, in a hexademical notation similar to CSS,
such as #0000ff (blue).
Three-digit shortcuts are also supported, such as #f00 (red).
The default value is #000000 (black).
hex background_color (optional):
The background color. The syntax is the same as $color above.
Use false to indicate a transparent background.
The default value is transparent.
array padding (optional):
The padding around each image, measured in pixels.
Your array should contain 4 integers. The order is the same as in CSS,
i.e. top-right-bottom-left. Padding may be useful if your font
contains glyphs with large swashes that extend outside of images
generated by IMGText. Use CSS with negative margins to cancel out
the extra padding in that case.
The default value is no padding.
bool shadow (optional):
Change this to true to enable text shadows.
The default value is false.
hex shadow_color (optional):
The color of text shadows. The syntax is the same as $color above.
The default value is #000000 (black).
int shadow_opacity (optional):
The opacity of text shadows.
This should be an integer between 0 and 127,
where 0 is fully opaque and 127 is fully transparent.
The default value is 64, half transparent.
array shadow_offset (optional):
The offset of text shadows relative to the text, measured in pixels.
Your array should contain 2 integers:
the horizontal offset and the vertical offset.
Use positive values to produce shadows to the right and/or below,
and negative values to produce shadows to the left and/or above.
The default is (2, 2).
int shadow_blur (optional):
The blur around text shadows, measured in pixels.
The default is no blur.
Arguments:
Return value: A string containing the HTML markup.
<img> tag will have one class, imgtext.<img> tags will be separated by one space character (0x20).IMGText is most beneficial when used in headings. It is not recommended for
body text, which can easily become a mess with hundreds of small images.
IMGText works best when you use font sizes above 20 points.
If you need smaller fonts, consider generating PNG images using large fonts
(2x magnification) and resizing them using CSS height.
This ensures a smooth look, as well as compatibility with high-resolution
(so-called Retina) displays.
IMGText requires PHP 5 and GD. All text must be encoded in UTF-8.
IMGText does not include any font files. Bring your own TTFs!
IMGText is released under the MIT license. It is freely available for
personal, non-profit, commercial, governmental, and any other use.