Attaching an image saved in MySql as a background-image instead of setting <img src


I have a gallery page that displays the large image by attaching it as the background image to a styled <div> when you click on a  thumbnail..

<div id=viewer></div>

<script language="javascript" type="text/javascript">
function ShowImage(image,width,height){
      newimage='url(' + image + ')';
triggered as follows:
<div class="smallpic"><img onMouseover=" = 'hand'" onMouseout=" = 'auto'" onClick="ShowImage('1.jpg','264px','350px')" src="thumbs/1.jpg" /></div>

The situation:
I have to provide a databased solution with the images stored in MySql...
I can retrieve the saved images and display them using this code....

<div class=smallpic>
echo "<a href=get_image.php?id=2&image=5><img src=get_image_tn.php?app_id=2&image=5 border=\"0\"></a>";

which simply pops the image up in a browser window, but the design really calls for the image to display in the styled viewer div. If I could get something like this:

      echo "<script language=\"javascript\" type=\"text/javascript\">\n";
      echo "document.getElementById('viewer').style.backgroundImage=url(get_image.php?id=2&image=5);\n";
      echo "</script>";

(which throws the javascsript error "invalid assignment left-hand side") to work I am hoping I can simply rewrite my ShowImage function, but after a couple of days trying and researching figured I should simply ask! I played around with output buffering, but no luck there either.

Thanks for your help,

get_image.php is:

    require '../sqlconnect.php';

    $id = $_GET['id'];
      $image = $_GET['image'];
    $query = "SELECT image_name, image_type, image_size, image_file FROM application_images WHERE application_id = $id AND image_name = '$image' and flag_archived = 0";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);

//    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo $content;

For what its worth, the images are resized to a standard size and thumbnailed before saving in BLOB's with the following function, where $resizedFile is the saved content:

            function resize_image($tmpName, $fileType, $fileSize, $max_width, $max_height){
            /* Creates resized image file as global var $resizedFile */

                  global $resizedFile;
                  global $iFileWidth;
                  global $iFileHeight;
                  $vars = explode('/',$fileType);
                  $type = $vars[1];
                  $fp = fopen($tmpName, 'r');
                  $content = fread($fp, $fileSize);
                  $oSourceImage = imagecreatefromstring($content);
                  $x = imagesx($oSourceImage); // uploaded image width, pixels
                  $y = imagesy($oSourceImage); // uploaded image height, pixels
                  if($x==0 || $y==0)return $file='';
                  if ($x > $max_width || $y > $max_height){
                        if ($x > $y){
                              // wide..
                              if($x > $max_width){
                                    $newy = resize_height($x, $y, $max_width);
                        }elseif($y > $x){
                        //      tall..
                              if($y > $max_height){
                                    $newx = resize_width($x, $y, $max_height);
                              //      square..use smallest max size...
                              $newx = ($max_width > $max_height)? $max_height : $max_width;
                              $newy = $newx;
                        $newx = $x;
                        $newy = $y;

                  $oDestinationImage = imagecreatetruecolor($newx, $newy);
                  $oResult = imagecopyresampled( $oDestinationImage, $oSourceImage, 0, 0, 0, 0, $newx, $newy, $x, $y); // resize the image
                  ob_start(); // Start capturing stdout.
                              case "jpeg":
                              case "jpg":
                                    imagejpeg($oDestinationImage); // As though output to browser.
                              case "png":
                              case "gif":
                  $resizedFile = addslashes(ob_get_contents()); // the raw image data.
                  ob_end_clean(); //
                  $iFileWidth = $newx;
                  $iFileHeight = $newy;                  
                  return true;

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard DavisSenior Web DeveloperCommented:

This is the source of your problem;
echo "document.getElementById('viewer').style.backgroundImage=url(get_image.php?id=2&image=5);\n";

Using that php file in there will cause major problems, because what is being echo'd back from the get_image.php file is actual image data and not what backgroundImage is expecting. 'backgroundImage' is actually expecting a string of a URL to the location of an image. Hence your javascript error.

As for the way to display that the way you would like, try something more like this;

<div id=viewer>
<img src="get_image.php?id=2&image=5">

I have the feeling that this will suit your needs quite nicely :)

Happy coding

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.