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

huangapple go评论106阅读模式
英文:

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

	
	$myfont = &#39;./font.ttf&#39;; // I&#39;ve done a million variations of this: font | /font.ttf  | /font etc...
	
	imagettftext($image, 20, 0, 0, 0, $white, $myfont, &quot;test&quot;);
	//imagestring($image, 5, 5, 20, &#39;test imgstring!&#39;, $white);
	
	ob_start();
 	imagejpeg($image); //, NULL, 100);
 	imagedestroy($image);
 	$i = ob_get_clean();
	
	//var_dump(gd_info());
	
 	return &quot;&lt;img src=&#39;data:image/jpeg;base64,&quot; . base64_encode( $i ).&quot;&#39; id=\&quot;instagram_challenge\&quot;&gt;&quot;;
}

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

答案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.

huangapple
  • 本文由 发表于 2023年8月10日 22:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定