Skip to content

Truncate strings based on actual size of rendered non-monospaced fonts.

Notifications You must be signed in to change notification settings

mrinterweb/php-smart-truncate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project is no longer maintained because all modern browsers support "text-overflow: ellipisis" now.



This is a quick PHP string truncator that truncates based on actual pixels for rendered fonts.

If you have ever tried to truncate strings with non-monospaced fonts, you will know why I cooked up this class.

The following two lines render at aproximately the same width with Arial 10 pixel font:
"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii iiiii"
"MMMMMMMMMMMMMMMMM MMM"

Designed for PHP 5.
This truncator uses PHP's GD Library imagettfbbox to do its magic.
With GD version 1,  the size argument is in PIXELS for imagettfbbox.
With GD version 2+, the size argument is in POINTS for imagettfbbox.

You can ensure GD is installed on your machine by typing
php -m
Look for GD in the result of this command.

I have done a decent amount of benchmarking and performance optimizations to make this run fairly fast despite using GD's imagettfbox.
test.php can help you benchmark and test the class.
system_test.php can help you troubleshoot your environment.

IMPORTANT NOTE!  You will need to use your own TrueType fonts.  I have excluded these fonts because they are released under a different license.


Here are some ways to get msttcorefonts

Ubuntu:
apt-get install msttcorefonts

http://corefonts.sourceforge.net/

All you really need is the font you are trying to truncate with (example: Arial.ttf).


Usage Example:

<?php

require './smart_truncate.php';

$width = 185; // width in pixels
$points = 7.5; // depending on the size of your font convert font size to points. example: 10 pixels == 7.5 points
$font = './Arial.ttf'; // using absolute paths to your font file is preferred

$long_string = "This is a test of a long string that has no real purpose that I am just typing.";

$st = new SmartTruncate($font, $points, $width);
echo $st->truncate($long_string, '&hellip;'); // &hellip; => '...'

?>

More notes to come.

License: MIT

Copyright (c) <2009> <Sean McCleary (Swipht Technologies)>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

About

Truncate strings based on actual size of rendered non-monospaced fonts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages