我使用的是下面的函数,我从一位用户发布的php站点中稍微调整了一下。
function createImgText ($string=NULL, $fontsize=0, $marginX=0, $imgH=0, $fontfile=NULL, $imgColorHex=NULL, $txtColorHex=NULL){
if($string != ""){
//header("Content-type: image/png");
//
$spacing = 0;
$line = array("linespacing" => $spacing);
if (file_exists($fontfile)) $box = @imageftbbox($fontsize,0,$fontfile,$string,$line) or die('Box command error');
else die("ERROR - font");
$tw=$box[4]-$box[0]; //image width
$marginY = $imgH - (($imgH - $fontsize) / 2);
$imgWidth = $tw + (2*$marginX);
$im = ImageCreate($imgWidth, $imgH);
$int = hexdec($imgColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$black = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
$int = hexdec($txtColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$white = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
ImageFtText($im, $fontsize, 0, $marginX, $marginY, $white, $fontfile, $string, array());
ImagePng($im);
ImageDestroy($im);
}else{
echo "ERROR - no string";
}
}
我在我的index.php 中使用以下内容
error_reporting(-1);
ini_set('display_errors', true);
以及输出图像的页面。
我没有收到任何错误。:(但是,取消对标题部分的注释会导致Firefox说"图像包含错误,将不会显示"。
图像php文件:
<?php
error_reporting(-1);
ini_set('display_errors', true);
$code = $sys->core->generateRandomString($sys->settings['captchal']);
echo 'Debug: Using string: ' . $code . ' font: fonts/' . $sys->settings['captchaf'] . '<br>';
$sys->core->createImgText($code, 9, 10, 18, 'fonts/' . $sys->settings['captchaf'], "000000", "FFFFFF");
?>
上面写着:
Debug: Using string: nrcujP font: fonts/airstream.ttf
�PNG IHDR0�H@�PLTE������������???___uV#�pIDAT�c` �E�D�x���*�s�bP �@fR�SVV``�F�0t4UTu6uuh3rl@�+4h3k6 �),6F�!�P�LHPTD8� �c�p�,�8�( �8�Q+|�Q��IEND�B`�
我有一个页面
<?php echo phpinfo(); ?>
它说GD已经启用,所有插件,包括TTF支持。以前有人遇到过类似的问题吗?我尝试过其他方法,但在第二个ImageColorAllocate上似乎失败了。当我搜索时,我在谷歌上看不到任何提及它的内容。它假设在调用时渲染图像,然后销毁自身,而不占用文件空间。
在NXT的帮助下解决了问题。之前我有一个空白
<?php
我忽略了。
imagepng($im,"new.png",0);
imagedestroy($im);
试试这个,我希望它能起作用。。。。。。。。https://stackoverflow.com/questions/13619834/upload-png-image-with-transparency-code-but-it-not-working-and-also-showing-the/13621103#13621103->此链接与您的问题相匹配。
我在使用您的代码时稍作修改,它运行得很好。
我的改变只是:
- 用其他.ttf替换字体名称
- 对captcha文本进行硬编码
- 删除了带有调试文本的行(它不可能在那里,因为它会破坏图像(
- 调整了函数调用
- 取消注释header((行
如果您仍然有问题,请确保只输出图像内容,并直接在任何可用的文本编辑器中打开脚本URL以查看源代码。还要检查输出中是否没有任何前导空格和尾随空格。
function createImgText($string=NULL, $fontsize=0, $marginX=0, $imgH=0, $fontfile=NULL, $imgColorHex=NULL, $txtColorHex=NULL){
if($string != ""){
header("Content-type: image/png");
$spacing = 0;
$line = array("linespacing" => $spacing);
if (file_exists($fontfile)) $box = @imageftbbox($fontsize,0,$fontfile,$string,$line) or die('Box command error');
else die("ERROR - font");
$tw=$box[4]-$box[0]; //image width
$marginY = $imgH - (($imgH - $fontsize) / 2);
$imgWidth = $tw + (2*$marginX);
$im = ImageCreate($imgWidth, $imgH);
$int = hexdec($imgColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$black = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
$int = hexdec($txtColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$white = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
ImageFtText($im, $fontsize, 0, $marginX, $marginY, $white, $fontfile, $string, array());
ImagePng($im);
ImageDestroy($im);
}else{
echo "ERROR - no string";
}
}
error_reporting(-1);
ini_set('display_errors', true);
$code = 'abcdef';
createImgText($code, 9, 10, 18, 'MyFont.ttf', "000000", "FFFFFF");