Page 1 of 1

Noob image creation question

Posted: Sun Jun 27, 2010 10:13 pm
by markmil2002
So I left asp.net and I am recoding my site in php. Thought this would be easy, I have a problem, I don't think it is code related, but I looked in the phpinfo and it says that the gd2 is enabled. The code is as follows. It simply draws a grid. If I comment out the header it gives me a bunch of jibberish like what I have posted below the code. With all the code I get that logo for a link to a broken link. Also, I am using php 5.3 and apache 2.2

Code: Select all

<?php
  $height = 1000;
  $width = 1000;

  $image = imagecreatetruecolor($width, $height);
  $white = ImageColorAllocate($image, 255, 255, 255);
  $black = ImageColorAllocate($image, 0, 0, 0);

  //imagefill($image, 0, 0, $black);

  //draws lines for x axis
  for ($i = 0; $i <= $width; $i += 10) {
    imageline($image, $i, 0, $i, 1000, $white);
  }

 // draws lines for y axis
  for ($i = 0; $i <= $height; $i += 10) {
    imageline($image, 0, $i, 1000, $i, $white);
  }

  header('Content-type: image/png');
  ImagePNG($image);
  imageDestroy($image);
?>
This is the jibberish
ÿØÿàJFIFÿþ;CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 75 ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀdd"ÿÄ ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚâãäåæçèéêòóôõö÷øùúÿÚ?ùþŠ( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( Š( ÿÙ

Re: Noob image creation question

Posted: Sun Jun 27, 2010 10:47 pm
by requinix
And you're absolutely sure the code you posted is the code that's being executed?

Because I'm not.

Re: Noob image creation question

Posted: Sun Jun 27, 2010 10:54 pm
by markmil2002
yes, I am positive, I have changed it numerous times to try other things, but I copied it after I saved it and that is the code I am executing. I ran it as http://localhost/image.php the same as the filename. I even made sure that the file extension was proper and what not.

Re: Noob image creation question

Posted: Sun Jun 27, 2010 11:06 pm
by markmil2002
I made some more changes to the php code. Still the same thing. Let me know what you think.

Code: Select all


<?
  header("Content-type: image/png");
  $i = 0;
  $height = 1000;
  $width = 1000;

  $im = @imagecreatetruecolor(1000, 1000);
  $white = ImageColorAllocate($im, 255, 255, 255);
  $black = ImageColorAllocate($im, 0, 0, 0);

  imagefill($im, 0, 0, $black);

  //draws lines for x axis
  for ($i = 0; $i <= $width; $i += 10) {
    imageline($im, $i, 0, $i, 1000, $white);
  }

  //draws lines for y axis
  for ($i = 0; $i <= $height; $i += 10) {
    imageline($im, 0, $i, 1000, $i, $white);
  }
  imagepng($im);
  imageDestroy($im);
?>

Re: Noob image creation question

Posted: Mon Jun 28, 2010 4:41 am
by requinix
See, the thing troubling me is that you have

Code: Select all

imagepng($im);
which generates a PNG image, but the "jibberish" you posted is most definitely a JPEG image - not a PNG.

What if you change the header() call to

Code: Select all

header('Content-type: image/jpeg');

Re: Noob image creation question

Posted: Mon Jun 28, 2010 9:16 am
by markmil2002
That has been tried, you missed a key point that I made. The only time I get that jibberish is if I comment out the header. Which makes sense because my understanding was that the header tells the server that an image is being created instead of html. But anytime I have the header uncommented, nothing works at all, it gives me something like a broken link to an image. I have it set right now the way that you told me to set it and I still get the same thing.

Re: Noob image creation question

Posted: Mon Jun 28, 2010 9:28 am
by markmil2002
I fixed the problem. You can't have any blank lines of code before the header.

Re: Noob image creation question

Posted: Mon Jun 28, 2010 3:36 pm
by requinix
markmil2002 wrote:That has been tried, you missed a key point that I made. The only time I get that jibberish is if I comment out the header.
No, I didn't miss anything. The header doesn't change the information the browser receives, only how it gets interpreted.

The information being sent is that of a JPEG image but you tell the browser it's a PNG. Luckily at least Firefox is capable of ignoring what you tell it if it thinks it's inaccurate.