Trouble Displaying Images using PHP Script

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

slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Trouble Displaying Images using PHP Script

Post by slade27 »

Greetings,


I am a php newbie, but I have successfully created two different kinds of scripts so far: one that generates links with info pulled from a database, and one that creates an image "on the fly" using info pulled from a database.

My problem is, I am now trying to include the two operations in one script, and whenever I do, the image portion of the script just creates a lot of gobbledy-gook text.

I thought at first that it was a header problem, since I was originally getting header error messages, but the header error messages, at least, stopped after I added the following html code to the very top of my php script:


<meta http-equiv="content-type" content="multipart/mixed; charset=utf-8" /></meta>


Nevertheless, my browser still returns garbage when it reaches the part where it should be generating an "on the fly" image for me.


Below you will find what I think are the relevant portions of the script. Thanks in advance for any ideas on how to get it to work!

Brian
Alexandria, Virginia


Code: Select all

<html>
<head>
<meta http-equiv="content-type" content="multipart/mixed; charset=utf-8" /></meta>
</head>
<?php 

$result=mysql_query ("SELECT * FROM `Articles`
ORDER BY  `Title`");

while ($row = mysql_fetch_assoc($result)) {

echo $row['Title'];



//And now the image part that I can get to work on its own, but not here:


$Title = $row['Title'];



$im     = imagecreatefromjpeg("myimage.jpg");
$orange = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

imagestring($im, 5, 90, 115, $Title, $orange);

$x1= "77";
$y1="43";
$size="10";

$x2 = $x1+$size;
$y2 = $y1+$size;

imagefilledrectangle ( $im, $x1, $y1, $x2, $y2, $black );
imagejpeg($im);
imagedestroy($im);

}
?>
forum.samir
Forum Newbie
Posts: 7
Joined: Sat Aug 11, 2007 12:55 pm

Post by forum.samir »

I think this is a header problem. Please just remove the following lines from the script

<html>
<head>
<meta http-equiv="content-type" content="multipart/mixed; charset=utf-8" /></meta>
</head>
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

When I remove that html text, I still get stuff like this at the point where the on-the-fly image should appear:




�����JFIF���������>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ���C� $.' ",#(7),01444'9=82<.342���C 2!!22222222222222222222222222222222222222222222222222�������"�������������� �������}�!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz����������������������������������������������������������������������������������� ������w�!1AQaq"2�B���� #3R�br�
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

you need to send the appropriate header() to inform the browser that you're sending it an image.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

You are outputing titles, so header becomes "text/html" i think, that's why you see garbage, which in-fact is image (string of bytes).
This page is a simple html page or will it be an email?
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

I added the following immediately before the php portion, but I still get junk when I reach the portion where the on-the-fly image is referenced.

<html>
<head>
<meta http-equiv="content-type" content="html/text; charset=utf-8" /></meta>
</head>

I am not working with e-mail. I am hoping to use this script eventually as an include to generate a list of links alongside a list of related on-the-fly images, both links and images to be created from data pulled from the same database.

The scripts, as I say, both work on their own, but the text comes out garbage when they are combined.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Did you read the manual page for the header() function? I'm nearly 100% sure it will contain the solution you are looking for...

"Give a man a fish..." versus "Teach a man to fish..."
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

I'll give it a shot. I've read dozens of pages about appropriate header usage, however, over the last three hours -- MIME sounds like what I need, but everyone talks about it only in the context of e-mail.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

I think you're confusing <head></head>'ers with true http headers...

A simple google search could help. 'http image headers'

http://www.google.ca/search?q=http+image+headers
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

I'll keep trying then, but I'm obviously missing something basic and perhaps rather simple, so if anybody can "pick it out" I'd appreciate it, as I've been working on this problem all morning and have already searched the recommended topics extensively.

Don't get me wrong: I'm not saying that the info isn't out there, even easily so, but I'm somehow managing either to miss it or to misunderstandi it. So again, if somebody has any more specific ideas, I'd welcome it.

In the meantime, I'll go back to the Google drawing board as suggested.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

slade27 wrote:I'll keep trying then, but I'm obviously missing something basic and perhaps rather simple, so if anybody can "pick it out" I'd appreciate it, as I've been working on this problem all morning and have already searched the recommended topics extensively.
Extensively? Really?

Code: Select all

header('Content-Type: ????');
Headers for files are NOT HTML. Headers in files define the type of file, the size of the file, etc. You only NEED to tell the browser the type of the file, and it'll decipher the rest.

Headers. No HTML, just headers.
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

I don't have any witnesses: I'm sorry you find it so hard to believe that I'm not as smart as you.

If you don't want to answer my question, DON'T.

JESUS CHRIST!
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

ADIEU to this FORUM

GROW UP
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

Sorry. I though this was a forum to find help. Seems it's just a forum to have one's integrity impugned and then condescendingly told to beat it.


MOROn!@J@JN:KLE'cb,./ xcvdb ZXVçΩ
slade27
Forum Newbie
Posts: 21
Joined: Tue Apr 24, 2007 3:49 am

Post by slade27 »

Maybe the fact that nobody can help me out shows that it's not the easiest question in the world -- why don't you rant against the people who couldn't help me with this easy problem
Post Reply