imagesetpixel 和使用 php5 和 gd 2.0(或更高版本,显然)显示图像的问题

发布于 2024-07-16 08:12:32 字数 1952 浏览 4 评论 0原文

出于我潜意识中最清楚的原因,我试图生成一幅雪崩般的画面。

使用 PHP5 和 GD v2.0(或更高版本),我使用以下 php/html:

<?php

            $x = $y = 100;

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

            $w  = imagecolorallocate($gd, 255, 255, 255);
            $b  = imagecolorallocate($gd, 0, 0, 0);


            for ($r=1; $r <= $y; $r++) {

                for ($c=1; $c <= $x; $c++) {


                        if (rand(0,1) == 0) {
                            $rand = $w;
                                    }

                        else {
                            $rand = $b;
                                    }

                    imagesetpixel($gd,$r,$c,$rand);

                            }


                        }


echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />

</head>

<body page="snowcrash2">

    <?php

        echo "<div id=\"snowcrashimg\">";



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



        echo "</div>";


    ?>

</body>

</html>

我试图迭代图像的每个“行”的每个“列”,将像素值设置为 1 或 0反射黑色或白色。

但是,这会引发(有趣的)错误:“警告:无法修改标头信息 - 标头已由 /var/www/play/snowcrash2.php 中的(输出从 /var/www/play/snowcrash2.php:32 开始)发送第 51 行”

将 header(...) 移动到前几行(试图将 header 放在可能及时发送的位置)会导致以下错误(以图像形式):图像“ http://127.0.0.1/play/snowcrash2.php" 无法显示,因为包含错误。”

嗯...帮助?


唯一出现的其他主题是这个 使用 PHP 和 GD 生成的图像被切断,它没有公认的答案,据我所知,与我遇到的问题无关。

I'm trying, for reasons best known to my unconscious mind, to generate a snow-crash-like picture.

Using PHP5 and GD v2.0 (or higher), I'm using the following php/html:

<?php

            $x = $y = 100;

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

            $w  = imagecolorallocate($gd, 255, 255, 255);
            $b  = imagecolorallocate($gd, 0, 0, 0);


            for ($r=1; $r <= $y; $r++) {

                for ($c=1; $c <= $x; $c++) {


                        if (rand(0,1) == 0) {
                            $rand = $w;
                                    }

                        else {
                            $rand = $b;
                                    }

                    imagesetpixel($gd,$r,$c,$rand);

                            }


                        }


echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />

</head>

<body page="snowcrash2">

    <?php

        echo "<div id=\"snowcrashimg\">";



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



        echo "</div>";


    ?>

</body>

</html>

I was trying to iterate over each 'column' of each 'row' of the image, setting the pixel value to either 1 or 0 to reflect either black or white.

However, this throws the (fun) error: "Warning: Cannot modify header information - headers already sent by (output started at /var/www/play/snowcrash2.php:32) in /var/www/play/snowcrash2.php on line 51"

Moving the header(...) to the first couple lines of the (in an attempt to put the header somewhere that it might get sent in time) leads to the following error (in image form): the image "http://127.0.0.1/play/snowcrash2.php" cannot be displayed, because it contains errors."

Umm...help?


The only other topic that came up is this one Generated image using PHP and GD is being cut off, which has no accepted answer and isn't, so far as I can see, relevant to the problem I'm having.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-07-23 08:12:32

当浏览器显示图像时,它会被单独下载并放置在页面上。 因此,您无法在一个请求中发送 HTML 和一张图片。 您需要一个 html/php 页面,其中有一个标签,其中 src 设置为另一个页面,该页面仅发送图像数据。

例如:
(index.php)

<html>
<body>
<image src="pic.php" />
</body>
</html>

现在在另一个名为 (pic.php) 的文件中,您将生成图像并将其发送回响应,其中包含“内容类型”标头,绝对没有其他内容(可能除了其他标头),

例如取自http://www.webdeveloper.com/forum/showpost。 php?p=879552&postcount=2

<?php

$number = "";
for ($i = 1; $i <= $lenght; $i++)
{
     $number .= rand(0,9)."";
}

$width = 11*$lenght;
$height = 30;

$img = ImageCreate($width, $height);
$background = imagecolorallocate($img,255,255,255);
$color_black = imagecolorallocate($img,0,0,0);
$color_grey = imagecolorallocate($img,169,169,169);
imagerectangle($img,0, 0,$width-1,$height-1,$color_grey);
imagestring($img, 5, $lenght, 7, $number, $color_black);
//////// VERY IMPORTANT
header('Content-Type: image/png');
//////// VERY IMPORTANT
imagepng($img);
imagedestroy($img); 

?>

When a browser shows an image, it is downloaded separately and placed on a page. Because of this you cannot send HTML and a picture down in one request. What you need to have is a html/php page which has an tag with the src set to another page which will only send the data for the image.

eg:
(index.php)

<html>
<body>
<image src="pic.php" />
</body>
</html>

now in another file called (pic.php) you would generate the image and send send it back to the response with a header of "content type" and absolutely nothing else (except for other headers maybe)

eg taken from http://www.webdeveloper.com/forum/showpost.php?p=879552&postcount=2

<?php

$number = "";
for ($i = 1; $i <= $lenght; $i++)
{
     $number .= rand(0,9)."";
}

$width = 11*$lenght;
$height = 30;

$img = ImageCreate($width, $height);
$background = imagecolorallocate($img,255,255,255);
$color_black = imagecolorallocate($img,0,0,0);
$color_grey = imagecolorallocate($img,169,169,169);
imagerectangle($img,0, 0,$width-1,$height-1,$color_grey);
imagestring($img, 5, $lenght, 7, $number, $color_black);
//////// VERY IMPORTANT
header('Content-Type: image/png');
//////// VERY IMPORTANT
imagepng($img);
imagedestroy($img); 

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文