51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

imagettftext() doesn't work in wordpress even with the font in the same directory

英文:

imagettftext() doesn't work in wordpress even with the font in the same directory

问题 {#heading}

我已经从我的站点上传下载了字体,并将其放在相同的目录中(我使用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(&#39;Content-type:image/jpeg&#39;);

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(&amp;quot;https://wodwodwod.com/wp-content/uploads/2023/08/RedHatDisplay-Bold.ttf&amp;quot;);
    chmod(file_put_contents(&amp;quot;/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf&amp;quot;, $font), 0755);
    echo file_put_contents(&amp;quot;/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf&amp;quot;, $font);


    $myfont = &amp;#39;./font.ttf&amp;#39;; // I&amp;#39;ve done a million variations of this: font | /font.ttf  | /font etc...

    imagettftext($image, 20, 0, 0, 0, $white, $myfont, &amp;quot;test&amp;quot;);
    //imagestring($image, 5, 5, 20, &amp;#39;test imgstring!&amp;#39;, $white);

    ob_start();
    imagejpeg($image); //, NULL, 100);
    imagedestroy($image);
    $i = ob_get_clean();

    //var_dump(gd_info());

    return &amp;quot;&amp;lt;img src=&amp;#39;data:image/jpeg;base64,&amp;quot; . base64_encode( $i ).&amp;quot;&amp;#39; id=\&amp;quot;instagram_challenge\&amp;quot;&amp;gt;&amp;quot;;




}

`add_shortcode(&#39;image_test&#39;, &#39;generate_image_test&#39;);
`

答案1 {#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, &quot;/srv/htdocs/wp-content/plugins/code-snippets/php/font.ttf&quot;, &#39;test&#39;);

Move it down a bit and it's visible.


赞(1)
未经允许不得转载:工具盒子 » imagettftext() doesn't work in wordpress even with the font in the same directory