【问题标题】:Does not output to the image file不输出到图像文件
【发布时间】:2014-07-28 23:08:42
【问题描述】:

我有这个来自http://www.hithlonde.com/pi/#filesPHP 程序。该程序应该将 pi 的数字转换为单独的颜色(每个像素一个数字)并将其保存到 image 文件中,如下图所示:http://www.hithlonde.com/pi/Pi1024x768.gif。但是我的代码在调试控制台中的输出是Binary。有点像这样:

¡¢£¤¥¦§¨©ª«¬®¯

代码如下:

 <?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://www.hithlonde.com/pi/pi) 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 = 300;
$y = 300;

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

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

$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++)
    {
        imagesetpixel($gd, $theX, $theY, NumToColor($gd, $theGoodPi[$PiWalker]));
        $PiWalker++;
    }
}

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

?> 

我正在使用 Netbeans IDE 8.0 Xampp 编译器 版本 1.8.3-4 (PHP 版本 5.5.11)。

任何帮助将不胜感激。

【问题讨论】:

    标签: php image hex pi


    【解决方案1】:

    1) 删除开始 php 标记之前的单个空格。 2) 无需创建“$theGoodPi”,只需使用 theTestPi,反正就是复制它。这使得它需要更长的时间。 3)$theTestPi = file_get_contents("http://www.hithlonde.com/pi/pi");
    你可能不需要改变 3 4)imagesetpixel($gd, $theX, $theY, NumToColor($gd, $theTestPi[$PiWalker])); 如果删除上面的 while 循环,则更改为 theTestPi(参见 2)

    您真正需要做的就是删除开始 php 标记之前的单个空格,尽管哈哈。

    【讨论】:

    • 去掉单个空格后是不是不行?我在另一台电脑上,我现在没有代码。稍后再检查。
    • 你用的是什么编译器?
    【解决方案2】:
    <?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://www.hithlonde.com/pi/pi) 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 = 300;
    $y = 300;
    
    $gd = imagecreatetruecolor($x, $y);
    
    //Connect the pi file to variable 
    $theTestPi = file_get_contents("http://www.hithlonde.com/pi/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++)
        {
            imagesetpixel($gd, $theX, $theY, NumToColor($gd, $theGoodPi[$PiWalker]));
            $PiWalker++;
        }
    }
    
    header('Content-Type: image/png');
    imagepng($gd); 
    
    ?> 
    

    【讨论】:

    • gd.jpeg_ignore_warning = 0 这会启用 GD 吗?我去掉了分号。
    • 我想我启用了 GD
    • 没有。错误,我无法复制粘贴,它是一个图像......是它正在寻找一个名称为空的文件。
    • http://www.hithlonde.com/pi/pi heres pi.txt 如果你需要的话
    • 什么不能看?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 2012-01-27
    • 1970-01-01
    • 2018-05-04
    相关资源
    最近更新 更多