草庐IT

php - 当 Firefox 显示 "the Image cannot be displayed because it contains errors"时,我如何找出错误是什么?

coder 2024-04-20 原文

我有一个制作图像的 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"),它会消除错误,但它会向浏览器返回无意义的内容,如下所示:

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

最佳答案

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

如果您的 header 之前有任何包含,请确保您没有使用 ?> 结束您的 PHP 代码,因为有时这会生成一个字节。

最后尝试将您的包含包装在 ob_start();ob_end_clean(); 中,因为这将确保不会因任何原因将随机字节强制转换为输出其他原因(我遇到过一次)。

关于php - 当 Firefox 显示 "the Image cannot be displayed because it contains errors"时,我如何找出错误是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18569495/

有关php - 当 Firefox 显示 "the Image cannot be displayed because it contains errors"时,我如何找出错误是什么?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  5. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  6. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  7. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  8. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  9. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  10. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

随机推荐