我使用的是用户发布的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 那是我忽略的。
发布于 2013-03-14 19:30:27
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->此链接匹配您的问题。
发布于 2013-03-14 20:18:19
我在使用您的代码时做了一些细微的更改,它运行得非常完美。
我的变化只是:
如果仍然存在问题,请确保只输出图像内容,并在任何可用的文本编辑器中直接打开脚本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");https://stackoverflow.com/questions/15417191
复制相似问题