1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
protected static function autoWrap($fontsize, $ttfpath, $str, $width, $fontangle = 0, $charset = 'utf-8') { $_string = ""; $_width = 0; $temp = self::chararray($str, $charset); foreach ($temp[0] as $v) { $w = self::charWidth($fontsize, $fontangle, $v, $ttfpath); $_width += intval($w); if (($_width > $width) && ($v !== "")) { $_string .= PHP_EOL; $_width = 0; } $_string .= $v; }
return $_string; }
public static function charArray($str, $charset = "utf-8") { $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all($re[$charset], $str, $match);
return $match; }
protected static function charWidth($fontsize, $fontangle, $char, $ttfpath) { $box = @imagettfbbox($fontsize, $fontangle, $ttfpath, $char); $width = max($box[2], $box[4]) - min($box[0], $box[6]);
return $width; }
|