note 100194 added to function.imagettftext

From: Date: Thu, 30 Sep 2010 08:12:38 +0000
Subject: note 100194 added to function.imagettftext
Groups: php.notes 
Request: Send a blank email to php-notes+get-172837@lists.php.net to get a copy of this message
You may want to be able to extract the name of the font with the font file. If so you can use this class I've developed. It only works with TTF fonts, not OpenType fonts. It has worked with all fonts that I have tested so far. Not extensively tested. There may be some character set issues but that is up to someone else to fix :) And sorry I had to strip a lot of the comments so that I could post it. <?php /** * TTFReader * Gives you name information of TTF font files. * Thanks to Apple, for their TTF specification. * * @name TTFReader * @author Devin Samarin <eboyjr14@gmail.com> */ class TTFReader { private $handle; private $is_parsed = false; private $info = false; private $name_ids = array( 0 => "copyright", 1 => "font_family", 2 => "font_subfamily", 3 => "unique_id", 4 => "full_name", 5 => "version", 6 => "postscript_name", 7 => "trademark", 8 => "manufacturer", 9 => "designer", 10 => "description", 11 => "vendor_url", 12 => "designer_url", 13 => "license", 14 => "license_url", 16 => "preferred_family", 17 => "preferred_subfamily", 18 => "compatible_full", 19 => "sample_text" ); private function translate_name_id($id) { if( isset($this->name_ids[$id]) ) return $this->name_ids[$id]; return $id; } public function open($filename) { $this->handle = fopen($filename, "r"); } public function close() { if( !$this->handle ) return false; fclose($this->handle); } public function info() { if( $this->is_parsed ) { return $this->info; } $this->is_parsed = true; if( !$this->handle ) return false; $info = array(); $ttf_header = unpack( "nmajor_version/" . "nminor_version/" . "nnumber_of_tables/" . "nsearch_range/" . "nentry_selector/" . "nrange_shift", fread($this->handle, 12)); if( $ttf_header['major_version'] != 1 || $ttf_header['minor_version'] != 0 ) { return false; } $ttf_found = false; for( $i = 0; $i < $ttf_header['number_of_tables']; $i++ ) { $ttf_table_name = fread($this->handle, 4); $ttf_table = unpack("Nchecksum/" . "Noffset/" . "Nlength", fread($this->handle, 12)); if( $ttf_table_name == 'name' ) { $ttf_found = true; break; } } if( $ttf_found ) { fseek($this->handle, $ttf_table['offset']); $ttf_table['header'] = unpack("nformat_selector/" . "nname_records_count/" . "nstorage_offset", fread($this->handle, 6)); $ttf_name_records = array(); for( $i = 0; $i < $ttf_table['header']['name_records_count']; $i++ ) { $ttf_record = unpack("nplatform_id/" . "nencoding_id/" . "nlanguage_id/" . "nname_id/" . "nlength/" . "noffset", fread($this->handle, 12)); if( $ttf_record['length'] <= 0 ) continue; $ttf_record_offset = ftell($this->handle); fseek($this->handle, $ttf_table['offset'] + $ttf_table['header']['storage_offset'] + $ttf_record['offset']); $ttf_value = fread($this->handle, $ttf_record['length']); if( $ttf_record['platform_id'] != 1 ) $ttf_value = @iconv('UTF-16be', 'UTF-8//TRANSLIT', $ttf_value); $info[$this->translate_name_id($ttf_record['name_id'])] = $ttf_value; fseek($this->handle, $ttf_record_offset); } } else { return false; } return $this->info = $info; } } ?> Here an example with Arial, since that's everyone's favorite font: <?php $ttf = new TTFReader(); $ttf->open("arial.ttf"); $info = $ttf->info(); print_r($info); // $info["full_name"] will give you the name of the font, but check if it is set first! $ttf->close(); ?> ---- Server IP: 192.41.42.26 Probable Submitter: 68.104.11.81 ---- Manual Page -- http://www.php.net/manual/en/function.imagettftext.php Edit -- https://master.php.net/note/edit/100194 Del: integrated -- https://master.php.net/note/delete/100194/integrated Del: useless -- https://master.php.net/note/delete/100194/useless Del: bad code -- https://master.php.net/note/delete/100194/bad+code Del: spam -- https://master.php.net/note/delete/100194/spam Del: non-english -- https://master.php.net/note/delete/100194/non-english Del: in docs -- https://master.php.net/note/delete/100194/in+docs Del: other reasons-- https://master.php.net/note/delete/100194 Reject -- https://master.php.net/note/reject/100194 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172837) next »