Forum Moderators: coopster & phranque

Message Too Old, No Replies

Strange Result When Outputting Images

         

Robber

3:50 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



Im using Perl to output images and to test I am using 2 125x125 images. The script is triggered on an img src in the html. One of the images displays fine, but the second which is more colourful only outputs a very thin stripe (basically the top of the image) rather than the whole image. Now that seems mighty strange to me. Especially since the browser seems to know the correct dimensions since if I try to highlight the whole thing I get my 125x125 square highlighted.

Anyone had any experience with this?

Knowles

4:10 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



Just out of curiosity is this on a server? If so you sure the second image got uploaded all the way?

jatar_k

4:15 pm on Jul 17, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that would be my first instinct as well, to re upload the image and then call it in the browser explicitly. It seems strange that one would work and the other wouldn't. Would lean me towards the image first.

Robber

4:15 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



Hi Knowles,

Thanks for trying to help me out (again!!).

Its on my local server, so download shouldnt be a problem. But it does seem to be a problem related to not getting the whole image loaded, since if I reduce the colour depth in Paintshop I can get a larger area displayed. However, reducing to 2 colours still only got about half the image. My other image is a brown square, that displays properly. I wondered if my server might be set up funny, I couldnt see anything out of place, but it was a bit like looking for a needle in a haystack.

Cheers

Knowles

4:23 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



That was my best guess! Only other thing I can think is possibly post the code, then one of the experts in perl will take a look at it.

Robber

4:23 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



Hi Jatar,

The image works fine in the browser. Its when I try and use the script to output it. If it has more than one colour it gets really screwed up. File size is a whopping 2k, cant see what the problem is!!

Im sure it shouldnt matter that the script is triggered using img src="blahblah.pl" and then outputting using a gif mime type. But is it possible this is the problem?

Robber

4:27 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



Hmmm, I seem to be calling on you guys a little too often of late, apologies. Heres the code Im using:

open (IMAGE, "c:\\apache\\htdocs\\images\\square.gif");

$size = -s "c:\\apache\\htdocs\\images\\square.gif";

read IMAGE, $data, $size;

close IMAGE;

print "Content-type: image/gif\n\n";
print "$data";

Cheers

bird

4:45 pm on Jul 17, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You probably want to make sure that you open the file in binary mode on Windows, or the data is likely to get corrupted (with the likelyhood increasing with the size of the file).

Knowles

4:52 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



By setting the content type to image/gif wouldnt that automatically set it to pull in binary mode? Or is there a way to set this in perl?

Robber

4:57 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



bird,

Im sure glad you came to join the party. Just when I reached the point of despair (again!!) you came up trumps.

Please accept a very big thank you to every one that helped.

I hope some others are also learning from all my mistakes - I would help clear my conscience a bit!!

Robber

5:09 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



I had thought the same as you Knowles, but that certainly seems to done the trick, just needed to add

binmode IMAGE;

after the open statement and we were in there.

Knowles

5:13 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



So it became:

read IMAGE, $data, $size;
binmode IMAGE;

Right?

Robber

5:18 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



Not quite:

open (IMAGE, "c:\\apache\\htdocs\\images\\square.gif");

binmode IMAGE;

$size = -s "c:\\apache\\htdocs\\images\\square.gif";

read IMAGE, $data, $size;

I put it above the read statement so that it would be treated as binary before the bytes were stored in $data.

Knowles

5:20 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



Ok now I understand, thanks Robber

Robber

5:24 pm on Jul 17, 2002 (gmt 0)

10+ Year Member



No problem, feels strange answering a question the Perl forum! Hopefully it will begin to happen more!

Cheers

SethCall

3:42 pm on Oct 2, 2002 (gmt 0)

10+ Year Member



Yes, I am bringing up an old thread, because my problem is very similiar, though I am using PHP + mySQL

I am doing a very very similiar construct as the perl one, and yet I get garbage on my browser screen.

For instance, i have a
<img src="view.php">

and view.php reads the database with your standard ol' "SELECT"(which has the binary data of an image)
and then outputs it:

$query = "SELECT * FROM background WHERE id = 33";
$result = mysql_query("$query") or die(mysql_Error());

while($row = mysql_fetch_array($result)){
if (!empty($row['image'])){
header("Content-type: image/jpeg");
echo $row['image'];

}
}

?>

I either get alot of garbage, or *nothing*.
Anyone experience there problems? When I upload the file to the server, it do it in "rb" when saving the temp file created... so that should be ok..