473,372 Members | 990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

image into variable or database

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
6 15994
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

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dav | last post by:
Hi all, I am using these code to save Image to database: Dim fs As FileStream = New FileStream(filename, _ FileMode.OpenOrCreate, FileAccess.Read) Dim rawData() As Byte = New Byte(fs.Length) {}...
3
by: den 2005 | last post by:
Hi everyone, Here is code working on..Trying to insert record with a column with Image or VarBinary datatype in sql database from a existing jpeg image file, then retrieve this image from...
0
by: santybanty20 | last post by:
i have some problem on ilplementing the code for the insertion of image into database that didnot get how to use the code for image storege.so can u help me in the use for the code by which i want to...
3
by: vivekgoyal | last post by:
hello guys, i need your help... i face problem in storing image in database... can any one send me the complete coding of storing image in database... please i need ur help... plz try to send me...
3
by: yashumrhsn | last post by:
Hi. Can anyone help me to write the code for inserting image into database? In my form there is a command button labelled 'save'. When click that button, it gets browse, from that we can select an...
1
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: I have the following code that I used in my application to retive image from database: Dim cnn As New SqlConnection("Data Source=localhost;Initial Catalog=Husam;Integrated...
2
by: keerthisreenu | last post by:
hai to all...!! iam working with Ms Access 2000. iam saving an image in the database. After that i want to retrive the same image from database and displaying it in the picture box. but its not...
2
by: shivapadma | last post by:
i have inserted the image into database using the following code String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/"; String dbName =...
3
by: brijesh1234 | last post by:
dear Sir/madam I am getting a problem while displaying a image from database Database : MSSQL2000 I have a table named “Image” having following fields as ImageId int Image Image
4
selvasoft
by: selvasoft | last post by:
Hi Please help me i want display all images from my database. but here my code that will display only one image from database. But i want all images, any one help me. <html> <head>...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.