views:

1152

answers:

4

I have a javascript slideshow that pre-loads images out of a mySQL database and then displays them one at a time in an image tag in the HTML document. Briefly, it accomplishes this by pre-loading images like many slideshow tutorials show on the web, but instead of using static images (i.e. images/image1.jpg etc.) it uses a dynamic image provided via PHP (i.e. getData.php?n=1) The getData.php script runs on the server and opens a new connection to the database for every image that is preloaded.

Is there anyway to avoid making so many connections to the database and having each connection make only a single query on the image database with LIMIT n,1?

It seems that if i am going to use getData.php as the image.src then getData.php needs to return a single image at a time! I'd really like to fetch them in blocks of 10 and then assign them to Image() objects one by one on the client side. I think this would be faster because when I wrote a client app in python to display the images (outside the browser of course) blocks of 10 or so transferred much faster (and reduces the load on the mySQL server). Can I accomplish this with javascript and PHP? Do I use XML in between? If I could get binary image data into javascript with AJAX could I do anything with it? Is there an imagecreatefromstring() function in javascript that I'm missing?

I can figure out exact code on my own, but I'm new to web programming and need a tip about how to tackle this problem! I think I'm missing some piece of major framework. Do I need Actionscript or something other than javascript for this problem? Thanks for the tip in advance!

EDIT: I like this first suggestion and I think I could get that done. It will also enable me too follow part of alex shishkin's suggestion to avoid LIMIT n,1 queries (i do want to keep the sql BLOB fields though) But how do I stick binary data from XML into an Image() object in javascript?

+1  A: 

I would say:

  • Have the getData.php generate an XML file that contains the next 10 images.
  • Have javascript to get the XML response from an AJAX-Request to getData.php.
  • Traverse the images in the XML response one by one and when the ninth images is displayed query the getData.php for the next set of images.

As for a framework: My advice is to use JQuery for both the AJAX request and the image display and preloading.


You can use a binary-to-text encoding like base64 to pass the binary data through XML. In most browsers you can use the base64 decode by itself and pass it to an image object like so:

<img src="data:image/jpeg;base64,your binary data comes here">

This will work in all but Internet Explorer, you can use a neat trick by Dean Edwards to pass the base64 data back to a PHP module for IE :)

Using JQuery and PHP for a simple image (no slideshows... this was just my test-code) the code looks something like this:

PHP:

<?php
   echo base64_encode(file_get_contents("image.jpg"));
?>

JS / JQuery:

$(document).ready( function() {
   $.get( "image.php", function( data ) {
      $(document).append("<img src=\"data:image/jpeg;base64,"+ data +"\">");
   });
});
Huppie
I like this suggestion and I think I could get that done. It will also enable me too follow part of alex shishkin's suggestion to avoid LIMIT n,1 queries (i do want to keep the sql BLOB fields though)But how do I stick binary data from XML into an Image() object in javascript? Thanks!
agartland
I think you can use binary-to-text encoding but will need to run a test to verify that.
Huppie
Thanks for this solution. I am a bit nervous about using the data: URI for large images (some people say there is a size limit imposed by browsers?) but will let you know what I find. Dean Edward's trick is indeed "neat" but it saves mySQL queries at the expense of a roundtrip for each byte of data!
agartland
You don't need a round-trip for every byte of data, you'll need a round-trip for every image though.
Huppie
A: 

Using binary images and "limit n,1" - very bad solutions.

Why your images didn't stored in database as links to the images? And images must stored on the file system. Queries like "Limit n, 1" really very slow, particularly on big tables. You need use small queries with big offset instead many queries with small offset and pack data in php to json format for the sending into the javascript. (limit 100 for example) Sending of the ajax request for the one images its very prodigally.

Nice slide show you can find here - plugins.jquery.com .

alex shishkin
A: 
  1. Create a web page.
  2. Let the page connect to the server and get XML or JSON with the IDs for the images.
  3. Request the images from the server using the ID from step 2.
svinto
A: 

how can display the images as slide show which are fetch from data base.

Please help me.