首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图片/GD问题(PHP)

问图片/GD问题(PHP)
EN

Stack Overflow用户
提问于 2013-03-14 18:20:21
回答 2查看 1.3K关注 0票数 3

我使用的是用户发布的php站点上的以下函数。

代码语言:javascript
复制
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中使用了以下内容

代码语言:javascript
复制
  error_reporting(-1); 
  ini_set('display_errors', true);

以及输出图像的页面。

我没有收到任何错误。:(但是,取消标题部分的注释会导致Firefox说“图像包含错误,不会显示”。

图像php文件:

代码语言:javascript
复制
<?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");
?> 

上面写着:

代码语言:javascript
复制
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`� 

我有一页

代码语言:javascript
复制
 <?php echo phpinfo(); ?> 

它说GD是启用的,所有的加载项,包括TTF支持。以前有人遇到过类似的问题吗?我尝试过其他的东西,但在第二个ImageColorAllocate上似乎失败了。当我搜索的时候,我没有在谷歌上看到任何关于它的提及。它应该在调用时呈现一个图像,然后破坏自身,而不是占用文件空间。

在NXT的帮助下解决了问题。我以前有过一个空格

代码语言:javascript
复制
 <?php 

那是我忽略的。

EN

回答 2

Stack Overflow用户

发布于 2013-03-14 19:30:27

代码语言:javascript
复制
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->此链接匹配您的问题。

票数 1
EN

Stack Overflow用户

发布于 2013-03-14 20:18:19

我在使用您的代码时做了一些细微的更改,它运行得非常完美。

我的变化只是:

  • 用我身边的其他.ttf替换字体名
  • 硬编码captcha文本
  • 删除带有调试文本的行(它不能存在,因为它会破坏图像)
  • 调整函数调用
  • 取消标头()行的注释

如果仍然存在问题,请确保只输出图像内容,并在任何可用的文本编辑器中直接打开脚本URL以查看源代码。还要检查输出中没有任何前导和尾随空格。

代码语言:javascript
复制
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");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15417191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档