473,327 Members | 2,025 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,327 software developers and data experts.

Displaying pictures

Hello

I am working on an applicaion that shows several pictures on a webpage.
These pictures are saved in a MySQL DB as BLOB. I noticed, that the web
server suffers in its performance by printing the pictures. Let's say
there are 20 pictures to show, there also are 20 queries to do. This is
the way I am doing it up to now:

index.php
<?php
foreach ($icons as $value) {
echo '<img src="./show_icon.php?icon_id=' . $value . '">';
}
?>

show_icon.php
<?php
$query = "SELECT icon FROM pictures WHERE id='$_GET['icon_id'] ";
$result = @mysql_query ($query) or die (mysql_error());
$icon = @mysql_result ($result, 0, "icon");

header("Content-type: image/png");
echo $icon;
?>

Actually this works quite well, but the performance is an issue. Is
there a more simple or more elegant way than the code above? Is there
actually a solution to do it with one query instead of 20 queries?

Thanks for your help
Stefan

Jan 14 '06 #1
13 2163
gooze wrote:
Actually this works quite well, but the performance is an issue. Is
there a more simple or more elegant way than the code above? Is there
actually a solution to do it with one query instead of 20 queries?


The improvement to gain here, is not to store the binary data in the
database, but only the reference to an image file on the server.
JW
Jan 14 '06 #2
Thank you for your answer. Actually I store the pictures in the DB
because they belong to the records fields. In this case it would be
meaningless to store the pictures on the filesystem, since there are
thousands of them and they are very small.

Just wondering if there is a way with only one query.

Jan 14 '06 #3
gooze wrote:
Thank you for your answer. Actually I store the pictures in the DB
because they belong to the records fields. In this case it would be
meaningless to store the pictures on the filesystem, since there are
thousands of them and they are very small.

Just wondering if there is a way with only one query.


Well, you could use something like:

SELECT icon FROM pictures WHERE id=$id and id=$id2 and $id=$id3...

Constructing the query from an array with ids would go as follows:

$ids = array(1,2,3,4,5,6);
$query = 'SELECT icon FROM pictures WHERE id=' . implode(' AND id=', $ids);

PS: Don't forget to escape user input which gets parsed into a query, e.g.
with the following function:

http://www.php.net/manual/en/functio...ape-string.php
JW
Jan 15 '06 #4
Actually the problem is not the selection from the DB, the real problem
is, how do I display the pictures in the browser after I have them in
the array? There are binary data in the queries result, and I have to
feed the browser with it...

Jan 15 '06 #5
gooze wrote:
Hello

I am working on an applicaion that shows several pictures on a webpage.
These pictures are saved in a MySQL DB as BLOB. I noticed, that the web
server suffers in its performance by printing the pictures. Let's say
there are 20 pictures to show, there also are 20 queries to do. This is
the way I am doing it up to now:

index.php
<?php
foreach ($icons as $value) {
echo '<img src="./show_icon.php?icon_id=' . $value . '">';
}
?>

show_icon.php
<?php
$query = "SELECT icon FROM pictures WHERE id='$_GET['icon_id'] ";
$result = @mysql_query ($query) or die (mysql_error());
$icon = @mysql_result ($result, 0, "icon");

header("Content-type: image/png");
echo $icon;
?>

Actually this works quite well, but the performance is an issue. Is
there a more simple or more elegant way than the code above? Is there
actually a solution to do it with one query instead of 20 queries?

Thanks for your help
Stefan


The performance issue you're experiencing is unlikely to be caused by
the overhead of storing images in the database. The bottleneck is
usually the Internet. Even a slow server can retrieve data from the
database faster than can be transferred across the network.

There are a couple things that could slow things down. First, make sure
the images are cached on the client-side by sending the appropriate
HTTP headers. Second, check to see if session-auto-start is turn on. If
it is, then call session_write_close() before sending the image data.

Jan 15 '06 #6
gooze wrote:
Actually the problem is not the selection from the DB, the real
problem is, how do I display the pictures in the browser after I have
them in the array? There are binary data in the queries result, and I
have to feed the browser with it...


You could try to use someting based on the following:

<?php

session_start();

if (isset($_SESSION['img']) && isset($_GET['show'])) {
header("Content-Type: image/jpeg");
print base64_decode($_SESSION['img']);
exit;
}

// Prepare some test data
$binary = file_get_contents('103.jpg');
$array = array_fill(0, 3, $binary);

foreach ($array as $e) {
$_SESSION['img'] = base64_encode($e);
// Timestamp appended to prevent caching
echo '<img src="', $_SERVER['PHP_SELF'], '?', time(), '&show=1" />';
}

?>
JW
Jan 15 '06 #7
This might work a little better:

<?php

session_start();

if (isset($_SESSION['img']) && isset($_GET['id'])) {
if (isset($_SESSION['img'][$_GET['id']])) {
header("Content-Type: image/jpeg");
print base64_decode($_SESSION['img'][$_GET['id']]);
exit;
}
}

// Prepare some test data
$binary = file_get_contents('103.jpg');
$binary2 = file_get_contents('283.jpg');
$array = array($binary, $binary2);

$_SESSION['img'] = array();
foreach ($array as $e) {
$_SESSION['img'][] = base64_encode($e);
}

for ($i = 0, $m = count($_SESSION['img']); $i < $m; $i++) {
print "<img width=100 src='{$_SERVER['PHP_SELF']}?id=$i' />";
}

?>

But also read Chung's advice...
JW
Jan 15 '06 #8
actually, you don't need to do a base64_decode() on a blob if it wasn't
encoded that way. if it's real binary data, all you need is stripslashes().
that's faster.

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:43***********************@news.euronet.nl...
This might work a little better:

<?php

session_start();

if (isset($_SESSION['img']) && isset($_GET['id'])) {
if (isset($_SESSION['img'][$_GET['id']])) {
header("Content-Type: image/jpeg");
print base64_decode($_SESSION['img'][$_GET['id']]);
exit;
}
}

// Prepare some test data
$binary = file_get_contents('103.jpg');
$binary2 = file_get_contents('283.jpg');
$array = array($binary, $binary2);

$_SESSION['img'] = array();
foreach ($array as $e) {
$_SESSION['img'][] = base64_encode($e);
}

for ($i = 0, $m = count($_SESSION['img']); $i < $m; $i++) {
print "<img width=100 src='{$_SERVER['PHP_SELF']}?id=$i' />";
}

?>

But also read Chung's advice...
JW

Jan 15 '06 #9
where do I go to find out how to manipulate the cache with headers?

"Chung Leong" <ch***********@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
gooze wrote:
Hello

I am working on an applicaion that shows several pictures on a webpage.
These pictures are saved in a MySQL DB as BLOB. I noticed, that the web
server suffers in its performance by printing the pictures. Let's say
there are 20 pictures to show, there also are 20 queries to do. This is
the way I am doing it up to now:

index.php
<?php
foreach ($icons as $value) {
echo '<img src="./show_icon.php?icon_id=' . $value . '">';
}
?>

show_icon.php
<?php
$query = "SELECT icon FROM pictures WHERE id='$_GET['icon_id'] ";
$result = @mysql_query ($query) or die (mysql_error());
$icon = @mysql_result ($result, 0, "icon");

header("Content-type: image/png");
echo $icon;
?>

Actually this works quite well, but the performance is an issue. Is
there a more simple or more elegant way than the code above? Is there
actually a solution to do it with one query instead of 20 queries?

Thanks for your help
Stefan


The performance issue you're experiencing is unlikely to be caused by
the overhead of storing images in the database. The bottleneck is
usually the Internet. Even a slow server can retrieve data from the
database faster than can be transferred across the network.

There are a couple things that could slow things down. First, make sure
the images are cached on the client-side by sending the appropriate
HTTP headers. Second, check to see if session-auto-start is turn on. If
it is, then call session_write_close() before sending the image data.

Jan 15 '06 #10
Jim Michaels wrote:
actually, you don't need to do a base64_decode() on a blob if it
wasn't encoded that way. if it's real binary data, all you need is
stripslashes(). that's faster.


When you look closely to my example, you will notice that the binary data is
base64 encoded before it's stored in the session.
JW
Jan 15 '06 #11
Jim Michaels wrote:

Don't top-post
where do I go to find out how to manipulate the cache with headers?


http://www.google.nl/search?q=cache+headers
JW
Jan 15 '06 #12
d
"gooze" <g0****@gmx.net> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Thank you for your answer. Actually I store the pictures in the DB
because they belong to the records fields. In this case it would be
meaningless to store the pictures on the filesystem, since there are
thousands of them and they are very small.

Just wondering if there is a way with only one query.


Just keep the filenames with the records to which they belong. As to
storing thousands of small files, that's exactly what a filesystem is for
and exactly what a database is not for :)
Jan 17 '06 #13
d
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
gooze wrote:
Hello

I am working on an applicaion that shows several pictures on a webpage.
These pictures are saved in a MySQL DB as BLOB. I noticed, that the web
server suffers in its performance by printing the pictures. Let's say
there are 20 pictures to show, there also are 20 queries to do. This is
the way I am doing it up to now:

index.php
<?php
foreach ($icons as $value) {
echo '<img src="./show_icon.php?icon_id=' . $value . '">';
}
?>

show_icon.php
<?php
$query = "SELECT icon FROM pictures WHERE id='$_GET['icon_id'] ";
$result = @mysql_query ($query) or die (mysql_error());
$icon = @mysql_result ($result, 0, "icon");

header("Content-type: image/png");
echo $icon;
?>

Actually this works quite well, but the performance is an issue. Is
there a more simple or more elegant way than the code above? Is there
actually a solution to do it with one query instead of 20 queries?

Thanks for your help
Stefan


The performance issue you're experiencing is unlikely to be caused by
the overhead of storing images in the database. The bottleneck is
usually the Internet. Even a slow server can retrieve data from the
database faster than can be transferred across the network.

There are a couple things that could slow things down. First, make sure
the images are cached on the client-side by sending the appropriate
HTTP headers. Second, check to see if session-auto-start is turn on. If
it is, then call session_write_close() before sending the image data.


Remember that reading images from a database requires the whole image to be
passed around from DB server to php, then php out to the web server, to the
client. That is a significant hit on a site with only a reasonable amount
of traffic.

That session_write_close() tip is a good one, though - more than two scripts
using the same session will cause the third to time out, which is quite
unexpected to most people :)
Jan 17 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: qqqq | last post by:
Hi I have a web page that displays properties for sale. The web page links to a database. The page shows information in the database and the path to the picture. The code is
8
by: Jason Steeves | last post by:
Can someone please point me in the right direction for displaying word documents onto a webpage? The word documents will contain text/tables/pictures. I am new to this and can't quite find what I...
6
by: Nutshell | last post by:
Hi, I created a web page which contains table. I use table cells to display a picture using <img src>. The problem is that some pictures are not being fully displayed, only a quarter of the left...
3
by: David. E. Goble | last post by:
Hi all; I have this list of pictures; <script type="text/javascript"> var picssigs = new...
1
by: wschlichtman | last post by:
I'm attempting to retrieve a bitmap from an image field in SQL Server 2005 using Visual Studio 2005 C#. I then want to load the bitmap into a picturebox. When I run the following code, I get the...
2
by: tperri | last post by:
I'm familiar with the Data Controls, however what I have is a website where users can upload multiple pictures. I want to display these pictures in a table, that has 3 columns in each row, and...
8
by: Jon Weston | last post by:
I'm setting up an Access2003 database with pictures. I put a bound ole picture ctrl on a form that's source is the table that contains the pictures and follow ALL the directions for embedding a...
1
by: greatjuli | last post by:
Please can anyone help with a javascript code to display bigger pictures me.jpg, me2.jpg.... on the same page by clicking on their thumnails on the same page. I have done what i believe is the...
2
by: Tyler | last post by:
I need to have a form that displays a group of 20 pictures in 4 rows of 5 pictures. The pictures are of people. Each Subject record has a picture field. I have a query written that selets the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.