HELP! IMAGES NOT OPENING IN PHP!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ishant
Forum Newbie
Posts: 2
Joined: Thu Jul 10, 2008 9:06 am

HELP! IMAGES NOT OPENING IN PHP!

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: HELP! IMAGES NOT OPENING IN PHP!

Post 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);
ishant
Forum Newbie
Posts: 2
Joined: Thu Jul 10, 2008 9:06 am

Re: HELP! IMAGES NOT OPENING IN PHP!

Post 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????
Attachments
3.jpg
3.jpg (7.55 KiB) Viewed 469 times
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: HELP! IMAGES NOT OPENING IN PHP!

Post 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?
Spartan101
Forum Newbie
Posts: 12
Joined: Mon Feb 25, 2008 4:56 pm
Location: Manchester (UK)

Re: HELP! IMAGES NOT OPENING IN PHP!

Post 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.
Post Reply