![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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']; or //page.php
$query = "SELECT photo FROM messages WHERE id = $id";
$conn = connect();
$result = $conn->query($query);
while ($row = $result->fetchRow()){$image = $row[0];}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 ?> 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'];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 |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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). |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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.) |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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><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;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"); <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? |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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
|
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
Quote:
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"> 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);page.php
header("Content-type: text/html");
echo("<h1>HI THERE</h1>");
$image = fread(.......);
$type = "image/jpeg";
header("Content-type: $type");
echo($image);page.php
header("Content-type: text/html");
echo("<h1>HI THERE</h1>");
$image = fread(.......);
$type = "image/jpeg";
//header("Content-type: $type");
echo($image);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(); |
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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 ?>" ><?phpI don't know how to put off calling unlink untill after the page has loaded. Anyone know? Still this is a *huge* improvement! |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |