Page 1 of 1

HELP! IMAGES NOT OPENING IN PHP!

Posted: Thu Jul 10, 2008 9:13 am
by ishant
Please avoid words like HELP! and don't use ALL UPPERCASE in your Subject. Readers of this forum will interpret that to mean that you think that your problem is more important than anyone else's, and many will deliberately not even read your post. Nobody has a priority over everyone else here. Just state your problem.

Also, learn to use the [ php ] and [ /php ] tags around your code, as I have done for you below, to make your code more readable. If you follow the rules here, you will probably get someone to help you.


HI!
I am using the following code for drawing a rectangle:

Code: Select all

 
<html><body>
<?php
    $myImage = @ImageCreate(150, 150);
 
    $black = ImageColorAllocate($myImage, 0, 0, 0);
    $white = ImageColorAllocate($myImage, 255, 255, 255);
    $red = ImageColorAllocate($myImage, 255, 0, 0);
    $gren = ImageColorAllocate($myImage, 0, 255, 0);
    $blue = ImageColorAllocate($myImage, 0, 0, 255);
 
    //draw some rectangles
 
    ImageRectangle($myImage, 15, 15, 40, 55, $red);
 
    ImageRectangle($myImage, 40, 55, 65, 95, $white);
 
 
    //output the image to the browser
 
    header ("Content-type: image/jpeg");
 
    ImageJpeg($myImage);
 
 
    //clean up after yourself
 
    ImageDestroy($myImage);
?>
</body>
</html>
 
and I am getting the following weird output in the browser:
ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC $.' ",#(7),01444'9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀ––.

I am using Apache 2.2.8, PHP 5.2.6 & in my php.ini file, there is the following line:
extension=php_gd2.dll.
But I don't know what is the problem. Anyone who knows how to correct it, please help.

Re: HELP! IMAGES NOT OPENING IN PHP!

Posted: Thu Jul 10, 2008 1:01 pm
by Ollie Saunders
Blank out your file completely and try this:

Code: Select all

<?php
$myImage = ImageCreate(150, 150);
 
$black = ImageColorAllocate($myImage, 0, 0, 0);
$white = ImageColorAllocate($myImage, 255, 255, 255);
$red = ImageColorAllocate($myImage, 255, 0, 0);
$gren = ImageColorAllocate($myImage, 0, 255, 0);
$blue = ImageColorAllocate($myImage, 0, 0, 255);
 
ImageRectangle($myImage, 15, 15, 40, 55, $red);
ImageRectangle($myImage, 40, 55, 65, 95, $white);
 
header ("Content-type: image/jpeg");
ImageJpeg($myImage);

Re: HELP! IMAGES NOT OPENING IN PHP!

Posted: Mon Jul 14, 2008 4:35 am
by ishant
If I use only the code:
<?php
$myImage = ImageCreate(150, 150);

$black = ImageColorAllocate($myImage, 0, 0, 0);
$white = ImageColorAllocate($myImage, 255, 255, 255);
$red = ImageColorAllocate($myImage, 255, 0, 0);
$green = ImageColorAllocate($myImage, 0, 255, 0);
$blue = ImageColorAllocate($myImage, 0, 0, 255);

ImageRectangle($myImage, 15, 15, 40, 55, $red);
ImageRectangle($myImage, 40, 55, 65, 95, $white);

header ("Content-type: image/jpeg");
ImageJpeg($myImage);
?>
, then I get the picture attached as the output in the browser. So, how to solve the problem????

Re: HELP! IMAGES NOT OPENING IN PHP!

Posted: Mon Jul 14, 2008 4:24 pm
by Ollie Saunders
Unfortunately I can't see that picture. Can you either describe it, upload it again or upload it somewhere else and provide a link?

Re: HELP! IMAGES NOT OPENING IN PHP!

Posted: Tue Jul 15, 2008 2:53 am
by Spartan101
It may help you if you view the results in a different browser...

Also I found the following to be helpful:-

Code: Select all

 
ob_start();
ImageJpeg($im);
$jImage = ob_get_contents();
ob_end_clean();
 
Storing the image in an object first can help.

You might want to try storing the image first on your site and then accessing it directly via the browser to see if it's just the code that's at fault or whether it's a browser related issue.