Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you display more than one image to a browser... 1

Status
Not open for further replies.

mThomas

Instructor
May 3, 2001
404
US
I have a couple of sites sharing a database. There will be a small number of images. Currently each site has a seperate image upload process where the image is stored on the server and the path is stored in a MS SQL table. I have been trying to use the image data and not the path so I can read the data from the database and use it on each site.

Here is some of the code I'm trying:

Code:
if(!$link)
{
    die('Something went wrong while connecting to the database.');
} else {

    $sql = "SET TEXTSIZE 2147483647";
	
    @mssql_query($sql);
    
	$sql = "select name,filetype,photodata
            from photos";
    
	$results = @mssql_query($sql, $link);
    
	if (!$results) {
        exit("<strong>Error while loading photo</strong>");
    }

    while ($row = mssql_fetch_assoc($results)) {
	
    header("Content-header: image/".$row['filetype']);
	
	$imageID = $row['photodata'];
	print $imageID;

	}

mssql_free_result($results);

}

This works, except only the first image in the table is displayed. If I use a non-image field the result set contains all the matching data.

to display the image with markup I use this on a seperate page. I haven't tried posting back to the same page, but i suspect that would also work, except of course, it would still display only one image.

Code:
<table border="5" width="900">
   <tr>
     <td><img src="imageprocessing.php?imageID=<?php print $imageID;?>" /></td>
   </tr>
</table>

Can you display more than one image to a browser when using the actual image data and not just a path to the image?

tia...mike

 
You can't display more than one image because image tags can only handle on image at a time.

If you want more images you need more image tags.

Add another image tag, with a different ID and use that in your query to select a different image and display it.
Code:
 $sql = "select name,filetype,photodata
            from photos WHERE imageid=$_GET['imageID']";

you can call the code to generate the image and specify which one you want with the imageID value.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thank you for your reply. Adding a clause to select a specific image will only select that image. I want to display multiple images.

The first code I posted (not the markup) only one image is displayed even though there are multiple images in the return set.

If I can get the code which returns the images to display more than one image, then I can write the code and markup to display the images. The problem is getting code to display/return more than one image.

It isn't a matter of the markup, it is a matter the code behind the markup will not produce/display/return more than one image. In other words, the code which runs the sql will only return the first record containing an image.

If instead of 'photodata' I use 'name' all the image names are returned. It is when I call image data that only one image is returned, so the code is looping through the data ok.

I've tried creating a regular array [non assoc] and create multiple variables while assigning an image to each variable sort of like:

$imageID1 = $images[0];
$imageID2 = $images[1];

print $imageID1;
print $imageID2;

The code runs without error, however only the first image is displayed.

tia...mike

 
You can only push a single image out at a time. Even if your query returns more than one, the way the header works it only displays one Image regardless of how much actual content there is.

Image tags likewise can only display one image.

So the rest of the images acquired from the database are not displayed.

What you can do, is to pass the id of the image and run your query to get the contents of a single image to display.


You can run this multiple times to get the other images one at a time:

Code:
if(!$link)
{
    die('Something went wrong while connecting to the database.');
} else {

    $sql = "SET TEXTSIZE 2147483647";
    
    @mssql_query($sql);
    
    $sql = "select name,filetype,photodata
            from photos [red]WHERE photo_id=$_GET[photo_id][/red] ";
    
    $results = @mssql_query($sql, $link);
    
    if (!$results) {
        exit("<strong>Error while loading photo</strong>");
    }

    $row = mssql_fetch_assoc($results);
    
    header("Content-header: image/".$row['filetype']);
    
    $imageID = $row['photodata'];
    print $imageID;

    

mssql_free_result($results);

}



Code:
if(!$link)
{
    die('Something went wrong while connecting to the database.');
} else {

    $sql = "SET TEXTSIZE 2147483647";
    
    @mssql_query($sql);
    
    $sql = "select name,[red]photo_id[/red]
            from photos";
    
    $results = @mssql_query($sql, $link);
    
    if (!$results) {
        exit("<strong>Error while loading photo</strong>");
    }

    while ($row = mssql_fetch_assoc($results)) {
    
    
    
    $imageID = $row['photo_id'];
   [red]print "<img src='getimage.php?photo_id=" . $imageID . "'>"; [/red]

    }

mssql_free_result($results);

}





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I appreciate your help. I suspect because the actual image data is not being called, no images will be displayed, which explains why the amended code returns broken images.

I'm beginning to think, what I'm trying to do cannot be done, as you explain the header will only parse one image at a time.

tia...mike
 
hi, you may be able to "cheat" by generating iframes w/ loop and different content.. It's not very hard and I havent tested it.

You could have one php file which runs with ?id=x
then the x is the post id in the sql.. and they put the img in the header.

I believe this should work, after 5 minutes of coding.

Olav Alexander Mjelde
 
I really appreciate the assistance of vacunita and DaButcher. The iframe idea does work.

Tek-Tips rules!

Thanks... Mike
 
Thought I'd report back that while I got the process to work. Even with a handful of images, the page using the process is way to slow.

So I guess for now, I'll have two seperate image upload scripts, one for each site, and continue to store the path to the image in the DB. It sure is faster.

It was an interesting exercise.

mike


 
If you have the img path, you can quite easilly loop the table and put the header content.

That loop file can then be used as src, in the img, as you tried.

I usually use htaccess to "cheat" the url (mod rewrite).

Code:
RewriteEngine on
RewriteRule ^thumb/picture/([a-zA-Z0-9_-]+)/([0-9]+)\.jpg$ thumb.php?id=$2

You make several lines like that, eg. one for thumb, one for original size and one for web-size.

Then you can generate neat urls with the db-loop script.

I wish I could share more code, but I dont think it will help posting out of context.

However, here is how I output the img in a fake picture:
Code:
<?php

   $link = mysql_connect('xxx', 'xxx', 'xxx');
 
    if(!is_resource($link)) {
 
       // output server error image (database)

    } else {
       
        // Reverse magic_quotes_gpc/magic_quotes_sybase effects on those vars if ON.
 
        if(get_magic_quotes_gpc()) {
            $pictureID        = stripslashes($_GET['id']);

        } else {
            $pictureID        = $_GET['id'];
        }
 	
        // Make a safe query
        $query = sprintf("SELECT * FROM `xxx`.`pictures` where `picture_id` = '%s'",
                    mysql_real_escape_string($pictureID, $link));
 		
        $result = mysql_query($query, $link);
 
        if (mysql_affected_rows($link) > 0) {
           	while ($row = mysql_fetch_assoc($result)) {
			$im = imagecreatefromjpeg("secretFolderForImages/tn_".$row['picture_realname']);

			header('Content-type: image/jpeg');
			imagepng($im);
			imagedestroy($im);

			}
// here you can also update a table with records (hits and such) for statistics -- that's what I do.
        }
		else {
		// output 404 image
		}
    }



?>


ps. to use this, I then have another php page, which loops and retrieves the ID in the database and parses like so:

Code:
echo "<div style=\"border: 1px solid black; width: 45px; height: 45px; background-attachment:fixed; background: black url([URL unfurl="true"]http://www.yourdomain.com/thumb/picture/web_{$commentshort}/{$_GET[/URL]['id']}.jpg) no-repeat; background-position: 50% 50%;  margin-right: 2px; margin-bottom: 2px; float:left;\"><a style=\"text-align:right; display:block;\" href=\"#\" title=\"{$alttext}\"><img src=\"/images/zoom.gif\" alt=\"{$alttext}\"  /></a></div>";

Hope you can use some of my code, even though it's out of context and it's only a small part :) the $commentshort I use for SEO

Olav Alexander Mjelde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top