GURUs please ...has anyone EVER managed to...

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
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

GURUs please ...has anyone EVER managed to...

Post by Fari »

..send and image stored in a variable to the screen??? The contradiction I see is the requirement of the header() function directly preceeding imagejpeg() function - which it cannot do because the header() has to be sent before output starts!

One way to work around the problem is to write out the image in the variable to a file with imagejpg(), then use the path in an <IMG SRC...> to display it and then delete it .. but what a waste of resources!!!

Anyone... before I go completely dilly ... :)
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

OK - it looks like output buffering

Post by Fari »

..removed the header error ... now
this code still gives me a text output instead of outputting a jpg image!!

Code: Select all

if (($orig_width > 100 ) || ($orig_height > 120 ))
		&#123;
		$ratio = (real)( $orig_height / $orig_width );
		$new_height = 120; $new_width = (int)(120 / $ratio );
		$new_image = imagecreate ( $new_width, $new_height ) ;
		imagecopyresized ( $new_image , $temp_image , 0, 0, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
		header("Content-type: image/jpeg");
		imagejpeg ( $new_image );
		return true;
		&#125;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

do you still try to put the image within the html-document ;)
and your output is like

Code: Select all

<html><body>....
ÿØÿà JFIF  ` `  ÿÛ „ 			
...
?
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

Hi Volka

Post by Fari »

..yes it was .. now i changed it to this:

Code: Select all

echo"<IMG SRC="image_reduce($row&#1111;$i])">";
and now I get a broken JPG link image ... so I'm getting closer .. but still now light at the end of the tunnel... wait.. there is!!!! ouch .. it's the 6.30 express train!!! :lol:
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

the src param of the img tag takes only one kind of value: a string that indicates a path of some sort. you cannot put binary data (i.e. the imagejpeg() function) into this field. it just will not work.
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

..this is exactly what I'm getting...

Post by Fari »

...at least the first bit

Code: Select all

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛC    $.' ",#(7),01444'9=82<.342ÿÛC  2!!22222222222222222222222222222222222222222222222222ÿÀxZ"ÿÄ ÿĵ&#125;!1AQa"q2
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

MyDimension .. your right .. that doesn't work either!

Post by Fari »

8O
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Although I'm not under the impression that I'm doing well I'm not getting tired of explaining this. Someday I might write it in a way that is understandable ;)

http is document-orientated. One request, one (and only one) document.
The data transfered is devided into two main parts, the header and the body.
  • So you have
  • request[request_header, request_body] client-->server
  • response[response_header, response_body] server-->client
request_header contains the path of the requested document, cookie data that the client is willing to offer the server, ....
request_body e.g. contains POST-data

The server now tries to handle this request. In case of a php-script is requested, the server finds that file (hopefully) recognizes it as a script and invokes the php module/cgi.
However the response is produced, as long as it stays http it will be devided in response_header and response_body. Always the header first, then the body (that's the reason for "Warning: Cannot add header information - headers already sent by").
response_header includes the response code (e.g. 200 for ok, 404 for document not found, 500 internal server error, ....), content-type, everything that you set with header(...), cookies to be set (that's why you can't determine wether a cookie is accepted by the client or not on the same page), ...
response_body is e.g. what you see in the source view of your browser (html-document) or any other load-data there may be (a file downloaded, an image, ...)

ok, back to your script.
You're creating a html-document with php (not setting any other content-type via header(), the output is assumed to be text/html).
This document is transfered to the client where it is treated and parsed as html. (meanwhile the php-script has ended, the connection is dead and there's no relationship between the client-data and the script that produced it at this time).
Now your html-document contains like <IMG SRC="image_reduce(picture.jpg)">.
The browser interprets it as "here has to be an image and I can request it here: image_reduce(picture.jpg)". SRC is supposed to contain an URI to the document that contains the image data. This can be either absolute or relative. Your property is handled as relative, so the browser will generate a new request like e.g. http://your.server/path/of/current/docu ... cture.jpg).
request_header now contains path/of/current/document/image_reduce(picture.jpg) as path to requested document and that is certainly something your server can't handle, so it will send back a 404 - document not found.

What you have to do is creating a valid new request for the image's data, e.g.

Code: Select all

<img src="imagereduce.php?image=picture.jpg" />
now the request_header contains /path/of/current/document/imagereduce.php?image=picture.jpg and if there is a imagereduce.php in the appropriate directory it will be invoked and image=picture.jpg will be passed to it as GET-data (for php4.2+ this is $_GET['image']='picture.jpg' by default).
The script now can send a new document, the image data, either original or reduced ;)
Post Reply