Image shows in binary.. with code

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply

Tue Aug 17, 2004 6:25 am


I have a problem!

When I try the code below the picture is shown as binary. The script puts a logo down in the right corner of a picture. The script works by itself...

I also get the "Warning: Cannot modify header information - headers already sent by ..." when I use <html><head> etc...

I have to have this picture down in the page in a table...

Anyone? thanks!

$sql = "select dato,navn,path,undertekst,bruker from bilder where id='$id'";
$result = mysql_query ($sql,$conn);

if (!$result = mysql_query($sql)) { die("En feil har oppstått: ".$mysql_error."<BR>\n"); }
while($myrow = mysql_fetch_row($result)) {
$dato = $myrow[0];
$navn = $myrow[1];
$path = $myrow[2];
$undertekst = $myrow[3];
$bruker = $myrow[4];

print "<center><table width=30% border=0 cellspacing=1 cellpadding=0 bgcolor='$bgcolor1'>";
print "<tr bgcolor='$tableback1'><td>";

//here's the prob

$hovedfil = $path . $navn;


$startlogo_x = $width1 - $width2;
$startlogo_y = $height1 - $height2;

header("Content-type: image/jpeg");

$im1 = imagecreatefromjpeg($hovedfil);
imagealphablending($im1, true); // this has to be before imagecopy();
$im2 = imagecreatefrompng("logo.png");

// stop prob.

print "<tr bgcolor='$tableback2'><td><i><b>$undertekst</b></i><br>Lagt inn av $bruker.</td></tr>";
print "</table>";
print "</center>";

User avatar
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Tue Aug 17, 2004 6:54 am

You're trying to output the image data in the middle of your HTML. You can't do that. Either your PHP script needs to generate an image (and be called in an img tag, e.g. <img src="script.php"/>), or it needs to save the image somewhere and you need to use the name in your img tag. You can't output both HTML and an image in the same script.


Tue Aug 17, 2004 7:17 am

Do you mean that i could do like this:

print "<tr bgcolor='$tableback1'><td><img src='inc/img.php'></td></tr>";


That would be the ideal, but don't know how to make that work...

The other way seems to me like some use of imagecreate() or something similar, but i'd rather use the first way...

thanks for quick response though!

User avatar
php-forum Super User
php-forum Super User
Posts: 1279
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India

Wed Aug 18, 2004 8:23 pm

yes keep the file that generates the images apart from the html file

so what you've done in the second version is correct.
<img src="http://localhost/imgfile.php?fileid=23" />
something like that...

Post Reply