php 表情选择,php imagettftext和特定的表情符号
我遇到了一個問題,繪制了“雜項符號和象形文字”unicode塊下的表情符號.
這是一個示例代碼:
header('Content-Type: image/png');
$im = imagecreatetruecolor(290, 60);
$grey = imagecolorallocate($im, 200, 200, 200);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 289, 59, $grey);
$font = 'seguisym.ttf';
$font = realpath($font);
//🌀 is the unicode html entity for a cyclone. It is under 'Miscellaneous Symbols And Pictographs'.
$text = "🌀 is a cyclone!";
imagettftext($im, 20, 0, 5, 22, $black, $font, $text);
//? is the unicode html entity for a snowman. It is under 'Miscellaneous Symbols'.
$text = "? is a snowman!";
imagettftext($im, 20, 0, 5, 52, $black, $font, $text);
imagepng($im);
imagedestroy($im);
?>
這是輸出:
這是它應該是什么樣子:
如您所見,這些表情符號都在不同的Unicode塊下.旋風是在“其他符號和象形文字”下,并且繪制HTML實體而不是實際角色,但是雪人在“其他符號”下并且被正確繪制.我已經仔細檢查以確保我使用的字體包含兩個字符.
要清楚,我希望繪制實際字符而不是HTML實體.
呈現為UTF8的相同字符:
解決方法:
我遇到了與阿比蓋爾TTF同樣的問題.經過一些研究,我找到了這個鏈接
其中有這個示例腳本:
$str = "this is a test";
$str = iconv("UCS-2", "UTF-8", preg_replace("/(.)/","\xf0\$1", $str));
$fsize = 32;
$ffile= "C:/wamp/www/gm/fonts/Aztec/101_Aztec SymbolZ.ttf";
$size = ImageTTFBBox($fsize, 0, $ffile, $str);
$txt_width = ($size[2] - $size[0]);
$txt_height = ($size[1] - $size[7]);
$im_width = $txt_width * 1.5;
$im_height = $txt_height * 1.5;
$im = ImageCreateTrueColor($im_width, $im_height);
$black = ImageColorAllocate($im, 0, 0, 0);
$white = ImageColorAllocate($im, 255, 255, 255);
$txt_x = ($im_width - $txt_width) / 2;
$txt_y = ($im_height - $txt_height) / 2 + $txt_height;
ImageFilledRectangle($im, 0, 0, $im_width, $im_height, $white);
imagecolortransparent( $im, $white );
ImageTTFText($im, $fsize, 0, $txt_x, $txt_y, $black, $ffile, $str);
Imagegif($im, "./test.gif");
ImageDestroy($im);
?>
他們的回答是:您必須正確地將您的字符串轉換為Unicdoe.這使得Abigail字體能夠正確顯示.不幸的是,我仍然在研究如何正確顯示非標準的TTF文件.但我認為這只是找到他們放置實際字體的位置(如旋風圖像).
另一個令人沮喪的因素是,GD有時會放入正方形,有時它會使角色留空.我真的更喜歡永遠給出空白字符,因為它返回WIDTH = 0和HEIGHT = 0而廣場可以返回有很多不同的大小.如果GD標準化總是給出一個沒有大小的空白字符,那么你所要做的就是尋找它.否則 – 你必須通過箍來找出一個正方形被退回.
我希望這有幫助!
標簽:php,unicode,gd,imagettftext
來源: https://codeday.me/bug/20191003/1848431.html
總結
以上是生活随笔為你收集整理的php 表情选择,php imagettftext和特定的表情符号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html怎么设计自己的网页,求一份自己设
- 下一篇: bme280 环境传感器开发板_STM3