Jump to content

HELP! IMAGES NOT OPENING IN PHP!


smarty

Recommended Posts

HI!

I am using the following code for drawing a rectangle:

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

Link to comment
https://forums.phpfreaks.com/topic/114078-help-images-not-opening-in-php/
Share on other sites

oh yeah, what the above poster said... it must be early, i didn't even notice that, i just naturally copied and pasted the php code into a blank .php page and it worked.

 

yeah, it's going to try and display your html tags as a jpeg! remove all that.

 

then what you can do is in your html code call <img src="image.php" /> and put your image code into image.php... you call image.php like an image since php tells it to output as a jpeg.

I am now getting a small box with a red cross inside as in the attached file...

I think I am missing out on some libraries.. but when I use phpinfo(), I get:

GD Support  enabled 

GD Version  bundled (2.0.34 compatible) 

FreeType Support  enabled 

FreeType Linkage  with freetype 

FreeType Version  2.1.9 

T1Lib Support  enabled 

GIF Read Support  enabled 

GIF Create Support  enabled 

JPG Support  enabled 

PNG Support  enabled 

WBMP Support  enabled 

XBM Support  enabled 

.. Can u plz tell wat's the prob..

Also, what libraries etc. are req.....

thanx

 

[attachment deleted by admin]

remove all html tags and place exit after destorying image....

 

<?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);
   exit;
?>

The script ends execution when the closing ?> tag is reached. There is no point in putting an exit; statement there.

 

smarty, you must post your current code (both showing how you are putting the image onto a web page and how you are producing and outputting the image) to get any help with what it is doing. The box/red-x you are getting means the browser could not fetch and display the image. There could be a dozen different reasons.

 

Also, don't put any @ in code as that will prevent logging of errors that would help point out problems that might be occurring. Remove the @ from in front of the ImageCreate() function call.

 

Add the following line after your first opening <?php tag and then check your web server log file for errors -

 

error_reporting(E_ALL);

i have tried this and it is working fine for me....

 

test.php

<?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);
   exit;
?>

 

and usage:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>

<BODY>
<img src="test.php" border="0" />
</BODY>
</HTML>

Four days ago, slushpuppie pointed out that the php code itself worked and the raw image data that smarty showed in the first post (with extraneous html tags around it that prevented the browser from rendering it) indicates that GD is working. It is what smarty is specifically doing with that code that is causing the problem. Until smarty provides specific information as to what he is doing, there is no point in reconfirming that the code works.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.