KriiV
KriiV

Reputation: 2020

GD Text to Image producing weird output

I have some GD/PHP code that is trying to create an image out of a string (held in a PHP variable). The output is weird though..

Here is the code: (It looks like there is heaps of whitespace at the start but that is because I indented to insert it here, I still dont understand how to properly use the code insert function of Stackoverflow :/ In my actual code there is no such excessive whitespace.

<?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>

And here is the output:

�PNG IHDR����PLTE���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������= IDATx��X ��44�,�BB @�����'��K]G��53/Qa�-�Q]]��;MÆ 6lذaÆ 6lذaÆ 6�S�/�����)��,����z�z_�Ό��H�<�3�rc��w!|=�m�>n���n�x��As�9�uxֈ�M?�����R&ރ�,-o�}}��3��˚�8Ͼu��M�e���+�9���'�R����&��/0j-�����L _3�zW�QN�;�f#��t�x����D$Ž�<�(�#�<�C�yL�_p���L�6y�X��X�G�qC ��}��&��ej��(#ː�j��H�5[�� �xX�vT,1�m"�e��5���c%ak��ra4�)��z�[f�#^#xE�'�7�;��W)V����� ӡޛ�!�=U�SN���[�� �1ċW�m��7��c�o��&�N�i7-�T��'���Q�tz�x+�R�)�/�H�ݙ߼��a�W��u�[�"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�~�"�ݮ��ؾ�"�H Ah�k���1y]�@]Qu�x��7_�ӹ�=

There is more of that output, it would just be too long to post.

I have no idea what's going on :/ Any help would be appreciated.

Upvotes: 1

Views: 241

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272106

If you copy-pasted your code correctly then you have leading white-space in your code:

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

Remove it; otherwise the whitespace becomes part of PNG output and therefore results in a "corrupted" image.

Edit

Now that you have posted your full code: You are mixing PHP code with image's binary data; it does not work that way. Move the code which generates image in a separate file e.g. article-image.php then add an image tag in your main HTML which points to that file, e.g.:

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

Upvotes: 2

Related Questions