By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

image into variable or database

P: n/a
hello,

i want to have an image in a variable so i can show it were ever i want the
only catch is that i want the image to be called in the same page, i found
this script but the problem is that you have to have an extra page to call
the image.

look at print_image.php

is there a solution or is it just not possible without having two pages

thanks in advance

G

the script=========================
by Elena Mitovska on February 18 2003, 02:10

Inserting images into database, field for storing image must be BLOB type:
to choose image user:
Upload.php
<form name='MyForm' method=post enctype="multipart/form-data"
action='Dofile.php'><input type=file name='userfile'><input type=submit
value=Upload">

Dofile.php:
$data = "";
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
//get file
$fp = fopen($userfile, "rb");
while(!feof($fp))
{
$data .= fread($fp, 1024);
}
fclose($fp);

$data = addslashes($data);
$data = addcslashes($data, "\0");

//insert image into database
$Query = "INSERT INTO images VALUES (NULL,'$data')";

$res = $db->query($Query);

OutputImages.php

<img src="print_image.php?id=$id>

print_image.php

$MainSQL="SELECT image FROM images where id=$id;

$res = $db->query($MainSQL);
$data = $res[0][0];

header("Content-Type: image/jpeg\n");
header("Content-Transfer-Encoding: binary\n");
header("Content-length: " . strlen($data) . "\n");

//send file contents
print($data);
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Spam sucks wrote:

hello,

i want to have an image in a variable so i can show it were ever i want the
only catch is that i want the image to be called in the same page, i found
this script but the problem is that you have to have an extra page to call
the image.

look at print_image.php

is there a solution or is it just not possible without having two pages


There is no way to do that on one call to a page. If you were so inclined, you
could put all that code into a PHP script that returns HTML or an image
depending on the variables sent to it, though I can't see an advantage to it.
The user's browser would still have to make 2 requests.

Shawn

--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #2

P: n/a

"Shawn Wilson" <sh***@glassgiant.com> schreef in bericht
news:3F***************@glassgiant.com...
Spam sucks wrote:

hello,

i want to have an image in a variable so i can show it were ever i want the only catch is that i want the image to be called in the same page, i found this script but the problem is that you have to have an extra page to call the image.

look at print_image.php

is there a solution or is it just not possible without having two pages
There is no way to do that on one call to a page. If you were so

inclined, you could put all that code into a PHP script that returns HTML or an image
depending on the variables sent to it, though I can't see an advantage to it. The user's browser would still have to make 2 requests.

What i meant was this:

== document test.php ==
<?php
session_start();
session_register('data');

//insert in session is almost the same as an im field
//$filename="./picture.jpg";
//$_SESSION['data'] = fread(fopen($filename, "r"), filesize($filename));

// get the data from the session and converts it into a pic
if ($image)
{
Header("Content-type: image/jpeg");
$ima=$_SESSION['data'];
$im = imagecreatefromstring("$ima");
imagejpeg($im);

}
// calls the image
echo"<img src=\"test.php?image=1\">";
?>
Jul 17 '05 #3

P: n/a
Spam sucks wrote:

"Shawn Wilson" <sh***@glassgiant.com> schreef in bericht
news:3F***************@glassgiant.com...
Spam sucks wrote:

hello,

i want to have an image in a variable so i can show it were ever i want the only catch is that i want the image to be called in the same page, i found this script but the problem is that you have to have an extra page to call the image.

look at print_image.php

is there a solution or is it just not possible without having two pages


There is no way to do that on one call to a page. If you were so

inclined, you
could put all that code into a PHP script that returns HTML or an image
depending on the variables sent to it, though I can't see an advantage to

it.
The user's browser would still have to make 2 requests.

What i meant was this:

== document test.php ==
<?php
session_start();
session_register('data');

//insert in session is almost the same as an im field
//$filename="./picture.jpg";
//$_SESSION['data'] = fread(fopen($filename, "r"), filesize($filename));

// get the data from the session and converts it into a pic
if ($image)
{
Header("Content-type: image/jpeg");
$ima=$_SESSION['data'];
$im = imagecreatefromstring("$ima");
imagejpeg($im);

}
// calls the image
echo"<img src=\"test.php?image=1\">";
?>


I don't see why you couldn't do that, as long as the file was reasonably small
and you put an exit() after the imagejpeg($im) line to prevent the echo from
printing text into the jpeg file. But unless you have a really good reason, I
don't see why you'd save the image like that. You could just open the file and
use fpassthru() as needed.

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #4

P: n/a
Using 2 file is better, I dont see any use to put condition on one file as
it's just more confusing, and perform slower.

Savut
"Shawn Wilson" <sh***@glassgiant.com> wrote in message
news:3F***************@glassgiant.com...
Spam sucks wrote:

"Shawn Wilson" <sh***@glassgiant.com> schreef in bericht
news:3F***************@glassgiant.com...
Spam sucks wrote:
>
> hello,
>
> i want to have an image in a variable so i can show it were ever i want
the
> only catch is that i want the image to be called in the same page, i found
> this script but the problem is that you have to have an extra page
to call
> the image.
>
> look at print_image.php
>
> is there a solution or is it just not possible without having two
pages
There is no way to do that on one call to a page. If you were so

inclined, you
could put all that code into a PHP script that returns HTML or an image depending on the variables sent to it, though I can't see an advantage

to it.
The user's browser would still have to make 2 requests.

What i meant was this:

== document test.php ==
<?php
session_start();
session_register('data');

//insert in session is almost the same as an im field
//$filename="./picture.jpg";
//$_SESSION['data'] = fread(fopen($filename, "r"), filesize($filename));

// get the data from the session and converts it into a pic
if ($image)
{
Header("Content-type: image/jpeg");
$ima=$_SESSION['data'];
$im = imagecreatefromstring("$ima");
imagejpeg($im);

}
// calls the image
echo"<img src=\"test.php?image=1\">";
?>


I don't see why you couldn't do that, as long as the file was reasonably

small and you put an exit() after the imagejpeg($im) line to prevent the echo from printing text into the jpeg file. But unless you have a really good reason, I don't see why you'd save the image like that. You could just open the file and use fpassthru() as needed.

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

Jul 17 '05 #5

P: n/a
Shawn Wilson <sh***@glassgiant.com> writes:
Spam sucks wrote:

i want to have an image in a variable so i can show it were ever i want the
only catch is that i want the image to be called in the same page, i found
this script but the problem is that you have to have an extra page to call
the image.

look at print_image.php

is there a solution or is it just not possible without having two pages


There is no way to do that on one call to a page. If you were so inclined, you
could put all that code into a PHP script that returns HTML or an image
depending on the variables sent to it, though I can't see an advantage to it.
The user's browser would still have to make 2 requests.


It *is* possible to embed image data in an HTML page, although not
all browsers handle it well. See the "data" scheme described in
RFC 2397:

http://www.rfc-editor.org/rfc/rfc2397.txt

Here's an example:

<?php
$img = imagecreate(200, 100);

// Generate the image here.

ob_start();
imagepng($img);
$data = base64_encode(ob_get_contents());
ob_end_clean();

echo "<img width=200 height=100 src=\"data:image/png;base64,$data\">\n";
?>

The "data" scheme has always worked for me with browsers that are
kin to Mozilla. However, I've heard that Internet Exploiter has
problems with it, so it might not be suitable for general use.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #6

P: n/a
Michael Fuhr wrote:

Shawn Wilson <sh***@glassgiant.com> writes:
Spam sucks wrote:

i want to have an image in a variable so i can show it were ever i want the
only catch is that i want the image to be called in the same page, i found
this script but the problem is that you have to have an extra page to call
the image.

look at print_image.php

is there a solution or is it just not possible without having two pages


There is no way to do that on one call to a page. If you were so inclined, you
could put all that code into a PHP script that returns HTML or an image
depending on the variables sent to it, though I can't see an advantage to it.
The user's browser would still have to make 2 requests.


It *is* possible to embed image data in an HTML page, although not
all browsers handle it well. See the "data" scheme described in
RFC 2397:

<snip>

Well, I'll be damned. Thanks for the tip. You learn something new every day
:o)

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.