View Full Version : Resolved PHP Image not working correctly

12-02-2008, 05:50 AM
I recently switched hosts and my image thing stopped working.


I know my .htaccess works because I use /news/ and /profiles/ in it too. And yes I did CHMOD my directories to 777.

See all that nonsense?

here is the source

include "config.php";

$id = $_GET[id];
$getx = mysql_query("SELECT * FROM `members` WHERE `id` = '$id'") or die(mysql_error()); //get all the
$user = mysql_fetch_array($getx);

// load the image from the file specified:
$im = imagecreatefrompng("images/compactstat.png");
// if there's an error, stop processing the page:

// define some colours to use with the image
$yellow = imagecolorallocate($im, 113, 113, 113);
$black = imagecolorallocate($im, 0, 0, 0);
// get the width and the height of the image
$width = imagesx($im);
$height = imagesy($im);
// draw a black rectangle across the bottom, say, 20 pixels of the image:

// now we want to write in the centre of the rectangle:
$font = 8; // store the int ID of the system font we're using in $font
$text = "$user[username]";
$text2 = "$user[cookies]"; // store the text we're going to write in $text
// calculate the left position of the text:
$leftTextPos = ( $width - imagefontwidth($font)*strlen($text) )/2;

$namecon3 = strlen($user[username]);

// finally, write the string:
imagestring($im, $font, 27, 22, $text, $yellow);
imagestring($im, $font, 70, 43, $text2, $yellow);
// output the image
// tell the browser what we're sending it

header("Content-type: image/png");

imagepng($im, "images/compact2/{$_GET['id']}.png");

header("Content-type: image/png");

// tidy up


what is wrong?

Nvm, I found out the problem myself.
It was this line that caused the problem:


12-02-2008, 06:01 AM
The errors are being recorded to you're screen. Because of the errors, you can't send the proper headers to display you're image. What you see is an ascii representation of the binary data within the file. Pretty neat eh?

Anyway, you're imagestring is one thats killing it. This one:


The other one looks like its trying to write an image to an incorrect directory. Be more specific with it, try using an absolute path.

Once those are fixed, the image should display again. You're last host probably had output buffering enabled, which would still allow you to send you're proper headers after the errors occured.

12-02-2008, 06:39 AM
haha yeah, thanks anyway Fou-Lu. What would the world do with out you? :P