【问题标题】:What is Causing the Error: "Notice: Uninitialized string offset"?是什么导致错误:“注意:未初始化的字符串偏移量”?
【发布时间】:2014-06-06 19:59:33
【问题描述】:

我有这个 PHP 程序来自:http://www.hithlonde.com/pi/#files 该程序假设将 pi 的数字转换为单独的 colors(每个像素一个数字)并将其保存到像这样的图像文件:http://www.hithlonde.com/pi/Pi1024x768.gif

但是当我尝试运行 PHP 代码时,它给出了这个运行时错误:

Notice: Uninitialized string offset: 44000 in C:\Users\User\Documents\NetBeansProjects\ColorDivisors\index.php on line 103

Notice: Uninitialized string offset: 44001 in C:\Users\User\Documents\NetBeansProjects\ColorDivisors\index.php on line 103

Notice: Uninitialized string offset: 44002 in C:\Users\User\Documents\NetBeansProjects\ColorDivisors\index.php on line 103

Notice: Uninitialized string offset: 44003 in C:\Users\User\Documents\NetBeansProjects\ColorDivisors\index.php on line 103

Notice: Uninitialized string offset: 44004 in C:\Users\User\Documents\NetBeansProjects\ColorDivisors\index.php on line 103

这是代码。

    <?php
    //Title:
    //  Pi Graphical Visualization Script
    //Date:
    //  03.14.07
    //Author:
    //  Tim Habersack
    //Descript: 
    //  Reads in the digits of pi, assigns a color to each number 0-9, 
    //  then outputs to an image with one pixel representing a number.
    //Notes:
    //  (http://zenwerx.com/pi.php) is where I obtained the pi file, 
    //  calculated to over 4 million decimal places.  The GD library
    //  in your php config must be enabled for this to function.
    //E-mail:
    //  tim@hithlonde.com
    //Copyright:
    //  Released under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License

    function NumToColor($ImagePassed, $NumPassed)
    {
        $the0 = imagecolorallocate($ImagePassed, 0xFF, 0xFF, 0xFF);
        $the1 = imagecolorallocate($ImagePassed, 0x00, 0xFF, 0xFF);
        $the2 = imagecolorallocate($ImagePassed, 0x00, 0x00, 0xFF);
        $the3 = imagecolorallocate($ImagePassed, 0xFF, 0x00, 0xFF);
        $the4 = imagecolorallocate($ImagePassed, 0x00, 0xFF, 0x00);
        $the5 = imagecolorallocate($ImagePassed, 0xFF, 0xA5, 0x00);
        $the6 = imagecolorallocate($ImagePassed, 0xFF, 0x00, 0x00);
        $the7 = imagecolorallocate($ImagePassed, 0xFF, 0xFF, 0x00);
        $the8 = imagecolorallocate($ImagePassed, 0x80, 0x80, 0x80);
        $the9 = imagecolorallocate($ImagePassed, 0x00, 0x00, 0x00);

        switch ($NumPassed)
        {
        case 0:
            return $the0;
            break;
        case 1:
            return $the1;
            break;
        case 2:
            return $the2;
            break;
        case 3:
            return $the3;
            break;
        case 4:
            return $the4;
            break;
        case 5:
            return $the5;
            break;
        case 6:
            return $the6;
            break;
        case 7:
            return $the7;
            break;
        case 8:
            return $the8;
            break;
        case 9:
            return $the9;
            break;
        }   
    }

    //Set the size of the pi image
    $x = 200;
    $y = 200;

    $gd = imagecreatetruecolor($x, $y);

    //Connect the pi file to variable 
    $theTestPi = file_get_contents("pi");

    $theGoodPi = "";

    $PiWalker = 0;

    //gets the # of digits needed, plus some extra for the \r dropped off, reads into var

    while ($PiWalker < $x * $y * 1.1)
    {
        if ($theTestPi[$PiWalker] == "\r")
        {   
            $PiWalker++;
        }
        else
        {
            $theGoodPi = $theGoodPi . $theTestPi[$PiWalker];
        }
        $PiWalker++;    
    }

    //$PiWalker = 0;

    //walks through the image, painting the pi characters pixel by pixel
    for ($theY = 0; $theY < $y; $theY++)
    {
        for ($theX = 0; $theX < $x; $theX++)
        {

/*This is line 103*/  imagesetpixel($gd, $theX, $theY, NumToColor($gd,$theGoodPi[$PiWalker])); 

            $PiWalker++;
        }
    }

    header('Content-Type: image/png');
    imagepng($gd);

我无法弄清楚到底是什么导致了这个错误。

我正在使用 xampp PHP Compiler 版本 1.8.3-4PHP 版本 5.5.11)。

【问题讨论】:

  • 你的 pi 表示不够长。
  • 您已注释掉 //$PiWalker = 0; 并在上一个循环中将其增加到 44000,现在您将其增加到超过 $theGoodPi 的大小/长度
  • @NiettheDarkAbsol 你是什么意思

标签: php image runtime-error pi


【解决方案1】:

看起来问题出在第 95 行

此行不应被注释:

//$PiWalker = 0;

(改成)

$PiWalker = 0;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多