Php将居中文本添加到图像

时间:2015-04-25 04:52:33

标签: php image

我已尝试了几次,但我似乎无法在透明背景上的此图片底部获得2条居中的文字行

有什么建议吗?

<?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 );    
?>

1 个答案:

答案 0 :(得分:0)

此过程有效。如果没有您的代码,我只能将手册中的示例反馈给您。 http://php.net/manual/en/function.imagestring.php。但它有效,我使用了imagestring函数。它接受文本的坐标,所以我不明白为什么它对你不起作用。

<?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);
?>