GD Text to Image产生奇怪的输出

时间:2013-03-20 05:19:51

标签: php image gd

我有一些GD / PHP代码试图用字符串创建一个图像(保存在PHP变量中)。输出很奇怪..

这是代码:(看起来在开始时有大量的空白,但这是因为我缩进将其插入此处,我仍然不明白如何正确使用Stackoverflow的代码插入功能:/在我的实际中代码没有这样过多的空白。

<?php 
        session_start(); 

        //If user is not logged in
        if(!isset($_SESSION['username']))
        {
            //They aren't meant to be able to access this page. GET THEM OUT OF HERE!
            echo "<script language=\"javascript\">top.location=\"index.php\";</script>";
            echo "<script language=\"javascript\">alert('You need to be logged in to access this page.');</script>";
        }   else
            {

                //Localise.
                $username = $_SESSION['username'];

                //Include important settings and functions.
                include($_SERVER['DOCUMENT_ROOT'] . '/includes/config.php');

                //Connect to MySQL Database.
                include($_SERVER['DOCUMENT_ROOT'] . '/includes/connect.php');

                //Let's get the users id.
                $sql = "SELECT * FROM `users` WHERE `username` = '$username'";
                $result = mysql_query($sql) or die(mysql_error());
                if(mysql_num_rows($result))
                {
                    $userarray = mysql_fetch_array($result) or die(mysql_error());  
                    $userid = $userarray['id'];
                }
            }

        if(!$_GET['i'] || $_GET['id'] != '95dd76e1099af307a37804ecb4d820bd' || $_GET['arid'] != 'b691362f280e16e9a5136d1623ac8156')
        {
            echo "<script language=\"javascript\">alert('There was an error with your request.');</script>";
            echo "<script language=\"javascript\">top.location=\"members.php\";</script>";
        }   else
            {
                //Decode and localise.
                $articletitle = urldecode($_GET['i']);  
            }
    ?>
    <!DOCTYPE html>
    <!--[if IE 7 ]>    <html class="ie7 oldie"> <![endif]-->
    <!--[if IE 8 ]>    <html class="ie8 oldie"> <![endif]-->
    <!--[if IE 9 ]>    <html class="ie9"> <![endif]-->
    <!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->

    <head>

        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <meta charset="utf-8"/>
        <meta name="description" content="">
        <meta name="author" content="">

        <title>5StarWriters</title>

        <link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />

        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="js/jquery-1.6.1.min.js"><\/script>')</script>

        <script src="js/scrollToTop.js"></script>

    </head>

    <body id="top">

    <!--header -->
    <div id="header-wrap"><header>

            <hgroup>
            <h1><a href="index.html">5StarWriters</a></h1>
            <h3>Quality content, fast.</h3>
        </hgroup>

            <?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/navlogged.php'); ?>

            <!-- Login bar can go here -->

    <!--/header--> </header></div>

    <!-- content-wrap -->
    <div id="content-wrap">

        <!-- content -->
        <div id="content" class="clearfix">

            <!-- main -->
            <div id="main">
                <?php
                    $sql = "SELECT `id` FROM `articles` WHERE `title` = '$articletitle'";
                    $result = mysql_query($sql) or die(mysql_error());
                    $array = mysql_fetch_array($result) or die(mysql_error());
                    $articleid = $array['id'];

                    $sql2 = "SELECT * FROM `pendingreview` WHERE `requestid` = '$userid' AND `articleid` = '$articleid'";
                    $result2 = mysql_query($sql2) or die(mysql_error());
                    //unset($array); //Clear variable data.
                    $array = mysql_fetch_array($result2); //Lets reload it with some more useful data.

                    //$sql = "SELECT * FROM `pendingreview` WHERE `requestid` = '$userid' AND ";
                    //$result = mysql_query($sql) or die(mysql_error());
                ?>
                <center><h1>Pending Review<h1></center>
                <br />
                <center>

    <?php
    $text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

    $arrText=explode("\n",wordwrap($text,75,"\n"));//change number 75 here to check the wordwrap

    $im = @imagecreate(700,500); //creates an image (width,height)
    $background_color = imagecolorallocate($im, 0, 0, 0); //sets image background color
    $y=5; //vertical position of text
    foreach($arrText as $arr)
    {
      $white=imagecolorallocate($im,255,255,255); //sets text color
      imagestring($im,5,15,$y,trim($arr),$white); //create the text string for image,added trim() to remove unwanted chars
      $y=$y+15;

    }
    header("Content-type: image/png");
    imagepng($im);
    imagedestroy($im);
    ?>

                </center>
                <br /><br /><center>
                </center>
                <br /><br /><br /><br />
            <!-- /main -->
            </div>

            <!-- sidebar -->
            <div id="sidebar">

                <div class="about-me">
                        <!-- SIDEBAR CONTENT HERE -->
                </div>

                <div class="sidemenu">

                    <!-- Extra sidebar content -->
                </div>

            <!-- /sidebar -->
            </div>

        <!-- content -->
        </div>

    <!-- /content-out -->
    </div>

    <!-- footer -->
    <?php include($_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php'); ?>
    </body>
    </html>

这是输出:

  

PNGIHDR PLTE =IDATx X 44 , BB@ ' K]G 53/Qa - Q]] ;MÆ6lذaÆ6lذaÆ6 S /),zz_ΌH&LT;!3rcw | = M&GT; NNX ?As9uxֈ中号R&安培;ރ, - Ø}}3˚8ϾuMe+ 9' R &amp; / 0j- L_3 zW QN ; f# t x D$Ž &lt; ( # &lt; C yL _p L 6y X X G qC } &amp; ej (#ː j H 5[ xX vT, 1M“E5C%akra4)Z [F#^#xE'7; W)V ӡޛ ! =U SN [ 1ċW m 7 c o &amp; N i7- T ' Q tz x+ R)/ Hݙ߼AWU [“LID [0%N | @f J:�v�P���N�E�&��/��E��"��v4*8�[���4��4�5Q�g����ב+�� $ J9䢺C- 4h Y |ыGI / R' a7( %R l : v )y $I w 1 Iۮy Ǿ/ ͞iW|o . x,��y��6lذa�>u{�bc�G;��6<�(�����Wխ��N�M���cWⓚX���~�x[�:Ļ�q�xW��hkuM�FX��K�k ���/�5vC�^?��? �˚�8Ͼ��DU��E)F����n���������3 �G��JةlPQ�z[�9",w�B�Qu���|�4�%Բr:{��*�����T���l����9'>p���MT�$S�5��K6Vj��r�����X�����\ˎLJ�x���a%6+M�V��(�Qsj�Db�����!$��]a f�,�m���9N���������ר�&�� f o��x>T�aE�����K'nǔ�ɦ��2v1�5��"N"<ŋ��i�1��*b�9�F�\Hx��u�V"��E�j��Y����d�F�o(I滦��� �s���"�(ن��Ju� ��j h?y ^ k ~ “ ݮ ؾ ” HAh k 1y] @]Qu x 7_ ӹ =

有更多的输出,发布时间太长了。

我不知道发生了什么:/ 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

如果您正确地复制了代码,那么您的代码中会占据空白区域:

            <?php
^^^^^^^^^^^^

删除它;否则,空格会成为PNG输出的一部分,从而导致“损坏”的图像。

修改

现在您已经发布了完整的代码:您正在将PHP代码与图像的二进制数据混合;它不起作用。将生成图像的代码移动到单独的文件中,例如article-image.php然后在主HTML中添加一个指向该文件的图像标记,例如:

<img src="article-image.php?id=1">