Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 15th, 2007, 4:36 PM   #1
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
Question passing binary (or long) data (dynamic images)

Let's suppose I capture a bunch of raw data to a variable.

For example:
//page.php
   $photo = fread(fopen($_FILES[$str]['tmp_name'], "r"), $_FILES[$str]['size']);
   $photo_type = $_FILES[$str]['type'];
captures the binary data for a file submitted by a form.

or
//page.php
$query = "SELECT photo FROM messages WHERE id = $id";
$conn = connect();
$result = $conn->query($query);
while ($row = $result->fetchRow()){$image = $row[0];}
will collect data of 'photo' is a "BLOB" field in a $sql database.

Okay, so let's suppose I've got $photo (a huge long string of data) and I've got $photo_type (the mime type for whatever type of image it is).

Now I could display this as an image by doing:
//page.php
   $photo = fread(fopen($_FILES[$str]['tmp_name'], "r"), $_FILES[$str]['size']);
   $photo_type = $_FILES[$str]['type'];


header('Content-Type: $photo_type);
$photo;

That's all fine and good but I don't want page.php to be the image itself. I want it to have the image in it as an <img> tag like:

//page.php
   $photo = fread(fopen($_FILES[$str]['tmp_name'], "r"), $_FILES[$str]['size']);
   $photo_type = $_FILES[$str]['type'];

?>
Hello <?= $name ?><br>
<img src="<? some code to render image ?>
My question is, how can I pass the $photo (and $photo_type) $variables to whatever "?some code to render image ?" is.

I've made the following workaround:
//page.php
   $photo = fread(fopen($_FILES[$str]['tmp_name'], "r"), $_FILES[$str]['size']);
   $photo_type = $_FILES[$str]['type'];

  $_SESSION['binary'] = $photo;
  $_SESSION['type'] = $type;
?>
Hello <?= $name ?><br>
<img src="image.php">

//image.php
get_session();
header("Content-Type: $_SESSION['type']");
$_SESSION['binary'];
but I'd prefer to pass the data directly. (Can I send them with POST if it's not a form response?)

Do I actually have to pass the data? The reason for the HTML code <img src=url> is simply to make an http request and recieve the response. Is there any way I can use PHP to bypass the request entirely and give the response (which would be "Content-Type: $type\n\n$photo") directly?

Any help appreciated,
wooz
woozy is offline   Reply With Quote
Old Mar 15th, 2007, 8:47 PM   #2
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
Are you wanting to do this with two files then, or just one?

If you want to keep within the same file, you can do something like this:
<?php
$file = 'image.jpg';
$types = array('jpg', 'jpeg', 'gif', 'png', 'bmp');

$image = $_GET['img'];
$type = strtolower(substr(strrchr($image, "."), 1));
if (isset($image) && file_exists($image) && in_array($type, $types))
{
  $image = file_get_contents($image);

  header("Content-Type: $type");
  echo $image;
}
else
{
  echo '<img src="test.php?img=' . $file . '">';
}
?>

Otherwise, with two files, it would probably be easier just to query in the image file itself according to whatever parameters you put for GET. When you put an image tag, it's already been processed independent of your script save for whatever you send through GET or whatever you have saved somewhere (file, database, session, cookie).
Styx is offline   Reply With Quote
Old Mar 15th, 2007, 10:43 PM   #3
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
That code doesn't actually work. If there is the $image variable (I'm not dealing with existing image files BTW) then the code renders the page itself as an image file. The "else" clause makes a html tag that feeds a file name to a php page.
woozy is offline   Reply With Quote
Old Mar 15th, 2007, 11:38 PM   #4
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
The $image variable within the condition was an example used for testing. You would replace that with however you get your image content (temporary file while uploading or from database).

If the GET parameter is set, yes, it will render itself as an image. It defaults to the else first so it displays the img tag, which requests itself along with a GET parameter based on your filename. (You would take out file_exists and put maybe !empty since it doesn't exist permanently.)

This was just to show one possible solution of how you could do it, which works in itself. You would need to modify it a little, probably passing whether it was from a temporarily uploaded file ($_FILES) or the database.

(Except with $_FILES, if you didnt want it permanent, I would probably either temporarily put it somewhere and get the info or put it into the database first and then get the info.)
Styx is offline   Reply With Quote
Old Mar 16th, 2007, 1:02 AM   #5
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
I tried it but if I put a header request in a page it overrites the entire page as just the image.

That is:
<html>
<h1>Hi there</h1>
<?php

$image = fread(...bunch of stuff...);


  header("Content-Type: $type");
  echo $image;
?>
Now how was that.
</html>
doesn't render as:
<html>
<h1>Hi there</h1>
<?php

{image}
Now how was that.
</html>
[/code]
as I'd like it too. The header() starts a whole new page so all I get is the image as an image file.

<img src="file"> is "shorthand" (sorta, though not really) for "Make a request indicated by the name and put the returned content here". My problem is I have no source name and I already now the content right here.

So far as I know there is no way to manipulate HTML to give the content tag the content directly.

We can't to <img content="@jfkui$_$mpv...bunch of binary code kilobytes in length"> nor can we use PHP to do something like <img src="<? temp_file(header('content'), content($image))?>"> where temp_file() is some place holder for a non-existant file whose content is "$image".

I tried doing:
first_page.php::
  <image src="image.php?type=<?= $photo_type ?>&content=<?= $image ?>">

image.php::
$image = $_GET['content'];
$type = $_GET['type'];

header("Content-type: $type");
echo $image;
This is impractical as GET variables have limits to length and here we have a GET variable whose value could be megabytes big.

I'm thinking I could do it by POST but am not sure how to call it.

What I'm hoping I can do is something like:
echo "<h1>Hi there</h1>"
echo-image ($image, $type, "left");
where echo-image($content, $image_type, $alignment, ... other img attributes) will somehow cough up the image content into an image on the page.

<img> is an http request. The browser fills in with the content of the response. Is there PHP code that can allow me skip the request but request that a response of my programming (it would be "Content-type: $type\n\n$image") be filled in?
woozy is offline   Reply With Quote
Old Mar 16th, 2007, 3:07 AM   #6
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
In reference to my example, why don't you put your page within the else {} block? In the script, they are acting as two, completely different pages, not like one page within another. That's why both are separated in the first place: the image content has to be all by itself with just its headers to care for it =P
Styx is offline   Reply With Quote
Old Mar 16th, 2007, 3:32 PM   #7
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
Quote:
Originally Posted by Styx View Post
In reference to my example, why don't you put your page within the else {} block? In the script, they are acting as two, completely different pages, not like one page within another. That's why both are separated in the first place: the image content has to be all by itself with just its headers to care for it =P
I guess I don't understand what you are saying. And as we are discussing it an three (at least) levels of coding we are having a hard time communicating.

Level0)At the browser level what I want the user to see is web page that looks something like this:
:::::::::::::::::
HI THERE

:::::::::::::::::
Level1)At the HTML code level that be something like:
<h1>HI THERE</h1>
<img src="http://www.sirentiger.com/fatherbear.jpg">
*except* I don't actually have the a file for the img source, of course.

Now, the page itself (lets call the page itself "page.php") is a http request/response and the <img> is a second http request/response which I say is "within" the page although that could be rather lazy semantics.

(Quite probably, I am badly abusing technical and precise terminology do to a less than thourough understanding http-request/response mechanics. If so, I apologize and I hope to be enlightened.)

Level2) at PHP code I want to one of two things (although maybe neither of these things are possible).

Either I want to write a a second sepearate PHP script (Let's call the second script "image.php") which renders the image:
a--
page.php:::

$image = fread(...get the image content...);
$type = "image/jpeg";

echo "<h1>HI THERE<h1>\n";
echo "<img src='image.php>";

...plus some code to get $image and $type to image.php....
==========
image.php:::

... some code to get $image and $type from page.php...

header("Content-type: $type");
echo($image);
or I was to somehow "inject" my image into page.php:
page.php
header("Content-type: text/html");
echo("<h1>HI THERE</h1>");

$image = fread(.......);
$type = "image/jpeg";

header("Content-type: $type");
echo($image);
*won't* work because the header("Content-type: $type"); line redirects to a new page of just the image.
page.php
header("Content-type: text/html");
echo("<h1>HI THERE</h1>");

$image = fread(.......);
$type = "image/jpeg";

//header("Content-type: $type");
echo($image);
won't work because $image will be spelled out as a long string. What I need is something like:
page.php
header("Content-type: text/html");
echo("<h1>HI THERE</h1>");

$image = fread(.......);
$type = "image/jpeg";

header_but_not_a_redirect_keep_original_request_open("Content-type: $type");
echo($image);
close_the_second_request_but_still_keep_first_open();
Hmmm, I think I've seen something like that done before.
woozy is offline   Reply With Quote
Old Mar 16th, 2007, 4:50 PM   #8
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
A temp solution

Okay, this works (mostly)

<h1>HI THERE</h1>
<?php
$image = fread(........);

$file = tempnam("/dir/", "TEMP");
$handle = fopen($file, "w");
fwrite($handle, $image);
fclose($handle);
?><img src="<?= $file ?>" ><?php
In essence it makes a file of the image and the img tag references. The drawback is that although $file is meant to be temporary it will exist on the server untill deleted by unlink.

I don't know how to put off calling unlink untill after the page has loaded.

Anyone know?

Still this is a *huge* improvement!
woozy is offline   Reply With Quote
Old Mar 17th, 2007, 12:56 AM   #9
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
I perfectly understand what you are trying to do.

What I mean is, the if/else block acts as two completely different pages. You cannot mix the two by putting browser output (HTML, etc) outside of the if/else block

The solution you posted works, except can make the file server messy with a bunch of temporary files. (You can put unlink at the bottom of your script. HTML will load it properly, then unlink will delete it and, since it's already loaded, it wont affect the page display. Just if they try to save it or go to it, it wont exist.)

So, this is a revamped example. This instead takes image data from a database (based on my table at the bottom):
<?php
//
// Lets you use an image tag to specify an image internally
// Displays image from database acting like a new page
//

$types = array('jpg', 'jpeg', 'gif', 'png', 'bmp');

$image = (eregi('^[a-z0-9_-]+\.(' . implode('|', $types) . ')$', $_GET['img'])) ? $_GET['img'] : '';

mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');

if (isset($image) && !empty($image))
{
  // This is an image page based on conditions of $_GET['img']
  $result = mysql_query("SELECT * FROM images WHERE image_name = '$image'");
  $row = mysql_fetch_assoc($result);

  $type = strtolower(substr(strrchr($image, '.'), 1));

  if ($row && in_array($type, $types))
  {
    header("Content-Type: $type");
    echo $row['image_file'];
  }
}
else
{
  // This is the main default page
  echo '<h1>HI THERE</h1>
  <br /><img src="' . $_SERVER['PHP_SELF'] . '?img=image.jpg">';
}

/*
CREATE TABLE images (
image_id INT(3) NOT NULL AUTO_INCREMENT,
image_name VARCHAR(255) NOT NULL,
image_file TEXT,
PRIMARY KEY(image_id)
);
*/
?>

Do you understand what this does?

Edit: Here's an example.

Last edited by Styx; Mar 17th, 2007 at 1:43 AM.
Styx is offline   Reply With Quote
Old Mar 18th, 2007, 3:13 PM   #10
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
Okay, I finally see what you are saying. I'll give it a try but I think it might be complicated (if not impossible) to redo all the programming that I did to get the image. In fact if I get the image from a POST FILE field I think its an IE browser security feature that it can't be be resent.
woozy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
GOTO Sys. Tray Src. (code Snippet) Cipher Show Off Your Open Source Projects 1 Oct 17th, 2006 1:38 PM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM
Help in QBASIC (I think it's similar to VB) phoenix987 Visual Basic 3 May 9th, 2005 12:33 PM
Help with a QBASIC program phoenix987 Other Programming Languages 4 May 5th, 2005 12:27 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC