英文:
imagettftext() doesn't work in wordpress even with the font in the same directory
问题
我已经从我的站点上传下载了字体,并将其放在相同的目录中(我使用sftp进行了检查)。
我还尝试过在其他位置引用精确的文件位置,使用了与保存它的相同文件目录。两者都不起作用。
依然生成的图像只是一个黑色矩形。
帮助
注意:imagestring() 正常工作。
注意2:当我 var_dumped imagettftext() 时,它返回 bool(false)。
header('Content-type:image/jpeg');
function generate_image_test() {
$image = imagecreate(500, 90);
$black = imagecolorallocate($image,0,0,0);
$grey_shade = imagecolorallocate($image,40,40,40);
$white = imagecolorallocate($image,255,255,255);
// 本地字体文件,相对于脚本
$font = file_get_contents("https://wodwodwod.com/wp-content/uploads/2023/08/RedHatDisplay-Bold.ttf");
chmod(file_put_contents("/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf", $font), 0755);
echo file_put_contents("/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf", $font);
$myfont = './font.ttf'; // 我已经尝试了许多变体:font | /font.ttf | /font 等...
imagettftext($image, 20, 0, 0, 0, $white, $myfont, "test");
//imagestring($image, 5, 5, 20, 'test imgstring!', $white);
ob_start();
imagejpeg($image); //, NULL, 100);
imagedestroy($image);
$i = ob_get_clean();
//var_dump(gd_info());
return "<img src='data:image/jpeg;base64," . base64_encode( $i )."' id=\"instagram_challenge\">";
}
add_shortcode('image_test', 'generate_image_test');
英文:
I have downloaded the font from my sites uploads and placed it in the SAME directory (I checked with sftp).
I also have tried referencing the exact file location when placed somewhere else using the same file directory I used to save it there. Neither work.
Still my generated image is just a black rectangle.
Help
NOTE: imagestring() works just fine.
NOTE2: when I var_dumped imagettftext() it returns bool(false)
header('Content-type:image/jpeg');
function generate_image_test() {
$image = imagecreate(500, 90);
$black = imagecolorallocate($image,0,0,0);
$grey_shade = imagecolorallocate($image,40,40,40);
$white = imagecolorallocate($image,255,255,255);
// Local font files, relative to script
$font = file_get_contents("https://wodwodwod.com/wp-content/uploads/2023/08/RedHatDisplay-Bold.ttf");
chmod(file_put_contents("/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf", $font), 0755);
echo file_put_contents("/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf", $font);
$myfont = './font.ttf'; // I've done a million variations of this: font | /font.ttf | /font etc...
imagettftext($image, 20, 0, 0, 0, $white, $myfont, "test");
//imagestring($image, 5, 5, 20, 'test imgstring!', $white);
ob_start();
imagejpeg($image); //, NULL, 100);
imagedestroy($image);
$i = ob_get_clean();
//var_dump(gd_info());
return "<img src='data:image/jpeg;base64," . base64_encode( $i )."' id=\"instagram_challenge\">";
}
add_shortcode('image_test', 'generate_image_test');
答案1
得分: 0
由x和y给出的坐标将定义第一个字符的基准点(大致位于字符的左下角)。
意味着如果是0,0,文本将位于图像上方。
将其向下移动一点,它就可见了。
imagettftext($image, 20, 0, 20, 20, $white, "/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf", 'test');
英文:
The coordinates given by x and y will define the basepoint of the first character (roughly the lower-left corner of the character).
Meaning if it's 0,0 the text will be above the image.
imagettftext($image, 20, 0, 20, 20, $white, "/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf", 'test');
Move it down a bit and it's visible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论