php - 图像创建/GD 问题(PHP)

标签 php gd

我正在使用我从用户发布的 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);

以及输出图像的页面。

我没有收到任何错误。 :( 但是,取消注释 header 部分会导致 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- >此链接符合您的问题。

关于php - 图像创建/GD 问题(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15417191/

相关文章:

php - 如何在运行前检查 vsprintf 是否具有正确数量的参数

php - 不阻塞发送邮件 'execution'

PHP 登录脚本结果资源

php - 关闭浏览器选项卡时取消设置 session

PHP GD 静默失败

php - PHP 中的 JavaScript

python - PIL vs Python-GD 用于裁剪和调整大小

php - 带缺陷的圆弧绘制

wordpress - Amazon EC2 - PHP GD 图像库

php - 使用 PHP GD 缩小动画 GIF