6

我有一个制作图像的 PHP 文件。我已将 PHP 配置为在屏幕上报告错误。当我在 Firefox 23 中运行 PHP 文件中的文件时,它返回the Image cannot be displayed because it contains errors.

如何找出图像中的特定错误是什么?

这是代码:

<?php
    $tfont[] = 'cylburn-webfont.ttf';

    $cryptinstall = "./cryptographp.fct.php";
    include $cryptinstall;

    error_reporting(E_ALL ^ E_NOTICE);
    srand((double)microtime()*1000000);

    if ($_GET[$_GET['sn']] == "")
        unset ($_GET['sn']);

    session_start();

    include('getcaptcha.php');
    $question = getcaptcha();
    $questionLen = strlen($question);
    $cryptwidth  = 25 * $questionLen;

    $cryptinstall2 = "./cryptographp.cfg.php";
    include $cryptinstall;

    $cryptwidth = 900;
    $cryptheight = 200;

    // CrÈation du cryptogramme temporaire
    $imgtmp = imagecreatetruecolor($cryptwidth, $cryptheight);
    $blank  = imagecolorallocate($imgtmp, 255, 255, 255);
    $black   = imagecolorallocate($imgtmp, 0, 0, 0);
    imagefill($imgtmp, 0, 0, $blank);

    $word = '';
    $x = 10;
    $pair = rand(0, 1);
    $charnb = rand($charnbmin, $charnbmax);


    for ($i=0; $i<= $questionLen; $i++) {
         $tword[$i]['font'] =  $tfont[array_rand($tfont, 1)];
         $tword[$i]['angle'] = (rand(1, 2) == 1) ? rand(0, $charanglemax) : rand(360-$charanglemax, 360);

         $tword[$i]['element'] = str_split($question)[$i-1];

         $tword[$i]['size'] = rand($charsizemin, $charsizemax);
         $tword[$i]['y'] = ($charup ? ($cryptheight/2) + rand(0, ($cryptheight/5)) : ($cryptheight/1.5));
         $word .= $tword[$i]['element'];

         $lafont = "fonts/" . $tword[$i]['font'];
         imagettftext($imgtmp, $tword[$i]['size'], $tword[$i]['angle'], $x, $tword[$i]['y'], $black, $lafont, $tword[$i]['element']);

         $x += $charspace;
     }

    // Calculate horizontal racadrage temporary cryptogram
    $xbegin = 0;
    $x = 0;
    while (($x<$cryptwidth)and(!$xbegin)) {
        $y = 0;
        while (($y<$cryptheight) and (!$xbegin)) {
            if (imagecolorat($imgtmp, $x, $y) != $blank)
                $xbegin = $x;
            $y++;
        }
        $x++;
    }

    $xend = 0;
    $x = $cryptwidth-1;
    while (($x>0) and (!$xend)) {
        $y = 0;
        while (($y<$cryptheight)and(!$xend)) {
            if (imagecolorat($imgtmp, $x, $y) != $blank)
                $xend = $x;
            $y++;
        }
        $x--;
    }

    $xvariation = round(($cryptwidth/2)-(($xend-$xbegin)/2));
    imagedestroy ($imgtmp);

    // Create the final cryptogram
    // Create the background
    $img = imagecreatetruecolor($cryptwidth, $cryptheight);

    if ($bgimg and is_dir($bgimg)) {
        $dh  = opendir($bgimg);
        while (false !== ($filename = readdir($dh)))
            if(eregi(".[gif|jpg|png]$", $filename))
                $files[] = $filename;
                closedir($dh);
                $bgimg = $bgimg . '/' . $files[array_rand($files, 1)];
    }

    if ($bgimg) {
        list($getwidth, $getheight, $gettype, $getattr) = getimagesize($bgimg);
            $imgread = imagecreatefrompng($bgimg); break;
            imagecopyresized($img, $imgread, 0, 0, 0, 0, $cryptwidth, $cryptheight, $getwidth, $getheight);
            imagedestroy ($imgread);
        }
        else {
            $bg = imagecolorallocate($img, $bgR, $bgG, $bgB);
            imagefill($img, 0, 0, $bg);
            if ($bgclear)
                imagecolortransparent($img, $bg);
        }


    function ecriture()
    {
        // CrÈation de l'Ècriture
        global  $img, $ink, $charR, $charG, $charB, $charclear, $xvariation, $charnb, $charcolorrnd, $charcolorrndlevel, $tword, $charspace;
        $ink = imagecolorallocatealpha($img, $charR, $charG, $charB, $charclear);

        global $question;
        global $questionLen;

        $x = $xvariation;
        for ($i=1; $i<$questionLen; $i++) {

            if ($charcolorrnd) {   // Choisit des couleurs au hasard
                $ok = false;
                do {
                    $rndcolor = 0;
                    switch ($charcolorrndlevel) {
                        case 1 :
                            if ($rndcolor<200)
                                $ok = true; break; // tres sombre

                        default : $ok = true;
                    }
                } while (!$ok);

                $rndink = imagecolorallocatealpha($img, $rndR, $rndG, $rndB, $charclear);
            }

            $lafont = "fonts/" . $tword[$i]['font'];
            imagettftext($img, $tword[$i]['size'], $tword[$i]['angle'], $x, $tword[$i]['y'], $charcolorrnd ? $rndink : $ink, $lafont, $tword[$i]['element']);

            $x += $charspace;
        }
    }


    // Fonction permettant de dÈterminer la couleur du bruit et la forme du pinceau
    function noisecolor()
    {
        global $img, $noisecolorchar, $ink, $bg, $brushsize;
        switch ($noisecolorchar) {
            case 1  : $noisecol = $ink; break;
            case 2  : $noisecol = $bg; break;
            case 3  :
            default : $noisecol = imagecolorallocate ($img, 0, 0, 0); break;
        }

        if ($brushsize and $brushsize>1 and function_exists('imagesetbrush')) {
            $brush = imagecreatetruecolor($brushsize, $brushsize);
            imagefill($brush, 0, 0, $noisecol);
            imagesetbrush($img, $brush);
            $noisecol = IMG_COLOR_BRUSHED;
        }
        return $noisecol;
    }


    //Adding noise: points, lines and circles random
    function bruit()
    {
        global $noisepxmin, $noisepxmax, $noiselinemin, $noiselinemax, $nbcirclemin, $nbcirclemax, $img, $cryptwidth, $cryptheight;
        $nbpx = rand($noisepxmin, $noisepxmax);
        $nbline = rand($noiselinemin, $noiselinemax);
        for ($i=1; $i<$nbpx; $i++)
            imagesetpixel($img, rand(0, $cryptwidth-1), rand(0, $cryptheight-1), noisecolor());
            for ($i=1; $i<=$nbline; $i++)
                imageline($img, rand(0, $cryptwidth-1), rand(0, $cryptheight-1), rand(0, $cryptwidth-1), rand(0, $cryptheight-1), noisecolor());
    }


    if ($noiseup) {
        ecriture();
        bruit();
    }
    else {
        bruit();
        ecriture();
    }


    // Create the frame
    if ($bgframe) {
       $framecol = imagecolorallocate($img, ($bgR*3 + $charR)/4, ($bgG*3 + $charG)/4, ($bgB*3 + $charB)/4);
       imagerectangle($img, 0, 0, $cryptwidth-1, $cryptheight-1, $framecol);
    }


    // Transformations supplÈmentaires: Grayscale et Brouillage
    // VÈrifie si la fonction existe dans la version PHP installÈe
    if ($cryptgrayscal)
        imagefilter($img, IMG_FILTER_GRAYSCALE);
    if ($cryptgaussianblur)
        imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR);

    /* If I comment out these lines, then the file runs fine, but
       of course it does not return an image.
    header("Content-type: image/png"); // This is the only header(...) that is
                                       // included in the file. I am requesting
                                       // the file directly with the URL.
    imagepng($img);

    imagedestroy ($img);
    unset ($word, $tword);
    unset ($_SESSION['cryptreload']); */
?>

如果我注释掉header("content-type: image/png"),它会消除错误,但它会向浏览器返回废话,如下所示:

在此处输入图像描述

(综上所述,我对修复这个特定的图像文件的兴趣不如了解如何调试图像创建文件中的错误/意外行为。我想学习如何自己修复有问题的图像生成文件。)

4

9 回答 9

1

首先确保 header() 命令之前的代码没有任何部分输出任何内容。

如果您在标题之前有任何包含,请确保您没有完成您的 PHP 代码,?>因为有时这会生成一个字节。

最后尝试将您的包含包装在ob_start(); ob_end_clean(); 因为这将确保不会由于任何其他原因将随机字节转换为输出(我曾经发生过这种情况)。

于 2021-05-25T20:56:51.347 回答
0

注释掉,header()所以它只是发回数据而不是image/png

于 2013-09-02T09:14:26.747 回答
0

也许您的 PHP 脚本弄乱了您的图像。逻辑错误是不能报的,要自己找。要打开定期错误报告,请查看此链接:如何在 PHP 中启用错误报告?

启用错误日志 PHP 在哪里存储错误日志?(PHP 5、Apache、FastCGI 和 cPanel)

于 2013-09-02T20:40:18.710 回答
0

阅读您的 PHP 错误日志,您会发现确切的错误。很可能 PHP 正在发出警告。这就是您在浏览器中看到此错误的原因。

于 2013-09-02T14:46:03.247 回答
0

这是一个棘手的问题,不是吗?

您的脚本包含该行

echo('here!'); //prints here to the screen

这会将 ASCII 字符串“here”添加到 PNG 数据流中,从而破坏图像。

提示:如果您下载脚本的结果(使用例如wget,或在 Firefox 中简单地“保存页面”),并在编辑器中打开文件,您将很快看到问题。

于 2013-09-02T21:29:45.857 回答
0

ASP.NET Core应用程序中,您可能想将图片文件放在解决方案下名为Images的文件夹中,但您需要将它们放在wwwroot/images文件夹中:

在此处输入图像描述

于 2019-04-05T03:19:30.093 回答
0

我希望这可以为在 Linux 上对 .NET Core 应用程序进行故障排除的人节省一个小时:

就我而言,这实际上不是图像的问题。问题是服务器端的大写目录/文件名。在 Windows 文件系统文件名中,大小写无关紧要,而 Linux 文件系统区分大小写。

于 2020-02-11T20:03:30.057 回答
0

这是support.mozilla.org上的一个帖子的答案,它可能会帮助遇到这个问题的其他人:

我发现一个非常常见的原因是图像类型与文件扩展名不匹配。这会导致网络服务器报告错误的 MIME 类型。例如,如果您拍摄一张 Photoshop 图像,将其重命名为 .jpg 文件,并将其放在网络服务器上,它不会在 Firefox 中加载。

如果您正在解决此问题,请尝试下载文件并验证文件类型是否与 jpg 匹配。我今天为一个客户解决了这个问题,他获取了一个 .psd 文件,将其重命名为 .jpg,将其上传到网络服务器,并想知道为什么它不会在 Firefox 中加载。

于 2020-03-27T12:40:30.220 回答
0

就我而言,错误是 Visual Studio 的错。我开始通过img标签将图像添加到代码中,我猜是 Visual Studio 用 IntelliSense 引导我完成它。有趣的是,它添加了错误的路径。这是它为我添加的内容:

<img src="~/images/home-couple.jpg" />

它应该在哪里:

<img src="images/home-couple.jpg" />

图片文件夹的位置就在wwwroot下。这是一个 ASP.NET Core Web 应用程序。

于 2020-11-27T18:02:45.173 回答