1

I have looked at several other similar questions, but unfortunately none of them have helped with the problem I am having.

This is the script.

 <?php
    // returns a PNG graph from the $_GET['per'] variable
    $per = imagecreate(302,7);
    $background = imagecolorallocate($per, 0xFF, 0xFF, 0xFF);
    $foreground = imagecolorallocate($per, 0x00, 0x8A, 0x01);
    $border = imagecolorallocate($per, 0x99, 0x99, 0x99);
    if ($_GET['per'] > 0)
    {
        $grad = imagecreatefrompng("images/grad.png");
        $per2 = imagecopy($per, $grad, 1, 1, 0, 0, ($_GET['per'] * 3), 5);
        imagerectangle($per, 0, 0, 301, 6, $border);
    }

    header("Content-type: image/png");
    imagepng($per,NULL,5);
?>

I have checked GD support is on so I am not sure what the problem is. If I build just a simple bar without use of an image it will work fine, but when I try to build with an image it just shows a picture of a broken image.

kira423
  • 325
  • 1
  • 5
  • 26
  • Any PHP errors showing up in the error_log? – drew010 Nov 07 '12 at 00:07
  • Nope, and I have error reporting set to all, I ma not getting any notices either. – kira423 Nov 07 '12 at 00:12
  • Have you verified that the imagecopy is working by checking that $per2 is true? – ernie Nov 07 '12 at 00:12
  • To test, comment out the `header` function call and save the png to a file and verify no php errors are showing up when you call the script and see what the output of imagepng is. – drew010 Nov 07 '12 at 00:15
  • @drew010 I am not 100% sure how to do this I commented the header out and changed `imagepng` to `imagepng($per, "images/pic.png");` is that the correct way? – kira423 Nov 07 '12 at 00:21
  • As you're not seeing any errors, and the code works fine w/o the if block, check the items in the if block. Verify that neither `$grad` or `$per2` are false, and also verify that `imagerectangle()` is returning TRUE – ernie Nov 07 '12 at 00:25
  • @ernie I added `return $per2;` after the if statement and now it just produces a blank page, I am not sure what this means as I do not work with image creations very much. – kira423 Nov 07 '12 at 00:26
  • @kira423 - don't return, just echo it out (you'll probably want to comment out the `header` and `imagepng` lines) – ernie Nov 07 '12 at 00:28
  • @ernie `Resource id #4` is what it says for `$grad` `$per2` echo's the number 1 and it is the same for `imagerectangle` so the error is in $grad correct. – kira423 Nov 07 '12 at 00:50
  • @kira423 nope . . . $grad should be an object if things went correctly, as it's the image link resource, and everything else is returning TRUE, meaning they succeeded. I'm stumped. – ernie Nov 07 '12 at 01:02
  • It must just be Xampp, I will try on my actual host and report back with the results – kira423 Nov 07 '12 at 01:03
  • @ernie @Sheac sooo the problem was a space infront of ` – kira423 Nov 07 '12 at 01:17

1 Answers1

1

you need to make sure that per exists, otherwise this will throw an undefined index error

if ($_GET['per'] > 0)

should be this

if (isset($_GET['per']) && $_GET['per'] > 0)

or if you want to make sure its not empty and exists, you can do

if (!empty($_GET['per']) && $_GET['per'] > 0)

since the empty() function also checks if the variable exists before checking if its empty.

Sheac
  • 438
  • 2
  • 6
  • OP's comments suggest that it's not an undefined index error as there's nothing in the logs . . . – ernie Nov 07 '12 at 00:23
  • I understand but just because the log is empty doesn't mean there isn't an error happening. I took his source and ran it and that was the only error I got. once that was fixed his script worked just fine for me. even if he isn't getting that error it is always best practice to check if your variable & index exists before attempting to use it. – Sheac Nov 07 '12 at 00:34
  • Even using this it still doesn't work, if it isn't an error with the code it must be an error with Xampp – kira423 Nov 07 '12 at 00:55
  • hmm. it could be an error with your grad.png image maybe? I am using xampp to test this and seems to work, the only thing i don't have to test is your exact grad image, so i created my own png in photoshop to test. maybe try upgrading your xampp? (backup your htdocs folder first) I'm running xampp-win32-1.8.1-VC9-installer. also, try commenting out these two lines and run the script to make sure nothing is being output like an error before the headers are getting sent. `header("Content-type: image/png");` and `imagepng($per,NULL,5);` – Sheac Nov 07 '12 at 01:15