简体   繁体   English

PHP将居中文本添加到图像

[英]Php add centered text to image

I've tried a few times but I can't seem to get 2 centered lines of text at the bottom of this image on the transparent background 我已经尝试了几次,但在透明背景上此图像的底部似乎看不到两行居中的文本

Any suggestions? 有什么建议么?

<?php
$filePath = "adopt.png";  //full path to your png, including filename and extension

$img = @imagecreatefrompng($filePath);
$width  = imagesx($img);
$height = imagesy($img);

//create new image and fill with background color
$backgroundImg = @imagecreatetruecolor($width, $height);
imagecopy($backgroundImg, $img, 0, 0, 0, 0, 100, 130);
$color = imagecolorallocatealpha($backgroundImg, 0, 0, 0, 127); //fill transparent back

imagefill($backgroundImg, 0, 0, $color);

//save as png

header( "Content-type: image/png" );
imagepng( $backgroundImg );
imagedestroy( $backgroundImg );    
?>

This process works. 此过程有效。 Without your code I can only regurgitate the example from the manual to you. 没有您的代码,我只能将手册中的示例重新给您看。 http://php.net/manual/en/function.imagestring.php . http://php.net/manual/zh/function.imagestring.php But it works, I have used the imagestring function. 但这有效,我已经使用了imagestring函数。 It accepts co-ordinates for the text so I can not see why it would not work for you. 它接受文本的坐标,所以我看不到为什么它对您不起作用。

<?php
// Create a 100*30 image
$im = imagecreate(100, 30);

// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);

// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);

// Output the image
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM