i made a folder images in d:/wamp/www/images and uploaded photos to it and sent the path to of the images to the database e.g
image_path = d:/wamp/www/images/apples.jpg

now i want to create a gallery showing thumbnails of the images i created the script below ....its supposed to read the image path and resize the image and then display it but tis not doing so ....
its giving me output as

echo $thumbnail , echo " image thumbnail" ;

here's my code

<?

// generating a array with image paths 

$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);

while ($record_images = mysql_fetch_assoc($result_images)) {
          $image_list[] = $record_images['image_path'] ;

?>

<?
// generating a thumbnail 

$thumb_height = 100;

while ($record_images = mysql_fetch_assoc($result_images))
{
$filename = $image_list[];
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

$thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// displaying thumbnail

$thumbnail = "<img src= ".$thumb_image.">";
echo $thumbnail;
 
echo " image thumbnail ";

?>

plz tell what i m doing wrong and which line nos. if i need to change an existing line tell the line nos. and if i need to put in some other code then where i need t insert it......

and if u can alter the code to make it work and post it then even better.

thanx in advance

$thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// displaying thumbnail

$thumbnail = "<img src= ".$thumb_image.">";

Try going to view source of the page and you will find that there is no image location specified. That is because you can't just dump an image to the browser. You need to save the image to a file then dump the location of the image to the browser. So I think your script might just be about to get a lot more complex and will be interesting to see some ideas on this one.

but isn the file already there in the folder cant i load it from there itself?

Well actually the way it is at the moment, there is no file and the contents of the file is contained inside the variable but in a different format. So basically it's all just in the servers ram.

no the file is there in the root or rather the folder d:/wamp/www/images
and the image address
d:/wamp/www/images/apples.jpg is stored in the field image_path

no the file is there in the root or rather the folder d:/wamp/www/images
and the image address
d:/wamp/www/images/apples.jpg is stored in the field image_path

I think that only applies to freshly uploaded pictures via form. It is just the way the gd library was designed and I have heard of people creating tmp files but don't see the point when you can create public files.

Try this:
Change

$thumbnail = "<img src= ".$thumb_image.">";
echo $thumbnail;
 
echo " image thumbnail ";

to

header('Content-Type: image/jpeg');
echo $thumb_image;
exit;

Thats usually what I use to output images, but you have used some different functions which I am not familiar with.

Try this:
Change

$thumbnail = "<img src= ".$thumb_image.">";
echo $thumbnail;
 
echo " image thumbnail ";

to

header('Content-Type: image/jpeg');
echo $thumb_image;
exit;

Thats usually what I use to output images, but you have used some different functions which I am not familiar with.

Forgot about that syntax however isn't it more like the following:

header('Content-Type: image/jpeg');
imagejpeg($thumb_image);
imagedestroy($thumb_image);
exit;

Also just a note on that code, I believe it requires a dedicated file for each picture or at least a dedicated url (example: a variable $_GET parameter to link to).

Forgot about that syntax however isn't it more like the following:

header('Content-Type: image/jpeg');
imagejpeg($thumb_image);
imagedestroy($thumb_image);
exit;

Also just a note on that code, I believe it requires a dedicated file for each picture or at least a dedicated url (example: a variable $_GET parameter to link to).

I thought that too, but he used a different function and I didn't know if it outputted the data. Thanks for the correction.

And yes, that requires a dedicated file. I figured that's what he is doing. ???

hey its displaying nothing now...only a blank page. this is wat i did now.

<?

// generating a array with image paths 

$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);

while ($record_images = mysql_fetch_assoc($result_images)) {
          $image_list[] = $record_images['image_path'] ;

?>

<?
// generating a thumbnail 

$thumb_height = 100;

while ($record_images = mysql_fetch_assoc($result_images))
{
//$filename=$record_images['image_path'];
$filename = $image_list[];
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

$thumb_image = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

  // displaying thumbnail


header('Content-Type: image/jpeg');
$thumbnail =  imagejpeg($thumb_image);

echo $thumbnail;

imagedestroy($thumb_image);

?>

Still there is that fatal problem in your script. The script will need to be spreaded over two php files and so your current file should be as follows:

<? session_start();

// generating a array with image paths 

$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);

for ($row=0;$record_images = mysql_fetch_assoc($result_images);$row++) {
          $image_list[] = $record_images['image_path'] ;


// generating a thumbnail 

$thumb_height = 100;


//$filename=$record_images['image_path'];
$filename = $image_list[];
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

$_SESSION['img'][$row] = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

  // displaying thumbnail

echo '<img src="img.php?id='.$row.'">';
}
?>

Then img.php should be as follows:

<? session_start();
if (is_int($_GET['id'])) {
header('Content-Type: image/jpeg');
imagejpeg($_SESSION['img'][$_GET['id']]);
imagedestroy($_SESSION['img'][$_GET['id']]);
}
?>

now using this code u posted i m getting the output as

'; } ?>

code is looking fine

now using this code u posted i m getting the output as

'; } ?>

Well the large code box in post #11 (my last set of code) was designed to be the entire page. If you have more to the page then post the entire php file and I shall help you embed the code. Or perhaps you forgot to delete the last line when replacing the file. In any case just let me know weather there is more code to the page and if there is please post it to see how you have embeded my sample code.

no there' no more to it? basically later on i want to paginate it too. with links to prev and next showing the images in the gallery.

Well then you didn't replace the file properly and left some random bits left at the end. In the below quote is what the entire files should be.

The script will need to be spreaded over two php files and so your current file should be as follows:

<? session_start();

// generating a array with image paths 

$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);

for ($row=0;$record_images = mysql_fetch_assoc($result_images);$row++) {
          $image_list[] = $record_images['image_path'] ;


// generating a thumbnail 

$thumb_height = 100;


//$filename=$record_images['image_path'];
$filename = $image_list[];
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

$_SESSION['img'][$row] = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

  // displaying thumbnail

echo '<img src="img.php?id='.$row.'">';
}
?>

Then img.php should be as follows:

<? session_start();
if (is_int($_GET['id'])) {
header('Content-Type: image/jpeg');
imagejpeg($_SESSION['img'][$_GET['id']]);
imagedestroy($_SESSION['img'][$_GET['id']]);
}
?>

Edit: Accidental double posted my post as my internet was so slow.

nope i copoied just ur code on a fresh page and getting display on browser as

'; } ?>

in ur code u r using wat is tht ...there is no such field in the database.... wat is tht supposed to indicate

The $_SESSION is just an array that contains all of the picture data to send to the next page where it can be displayed via html <img src= code. But as for the error, I have just tested the code and it appears as if $_SESSION is not binary safe. Also I have updated the code to the following:

<? session_start();

// generating a array with image paths 
mysql_connect('localhost','root','');
mysql_select_db('mydatabase');


$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);

for ($row=0;$record_images = mysql_fetch_assoc($result_images);$row++) {
$filename = $record_images['image_path'] ;

// generating a thumbnail 
$thumb_height = 100;
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);

$_SESSION['img'][$row] = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($_SESSION['img'][$row], $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  // displaying thumbnail
echo '<img src="img.php?id='.$row.'">';
}

?>
<? session_start();
sleep(1);
if (!preg_match('/[^0-9]/',$_GET['id'])) {
header('Content-Type: image/jpeg');
imagejpeg($_SESSION['img'][$_GET['id']]);
imagedestroy($_SESSION['img'][$_GET['id']]);
}
?>

From a number of tests on the code such as displaying what $_SESSION[0] contains, it appears when storing binary data, the data is rejected and just an empty string is stored. So this will make it a little harder to solve. An alternative solution to sessions is longer lasting cookies which is like a variation of cookies and sessions combined.

why are you using sessions?

Just have img.php read the image file and resize it and display it.

On the page where the images are suppose to be displayed call the image using the id in the database. i.e. <img src="img.php?id=image_id_here" /> . In the img.php use $_GET to resize and display the proper image.

ok i made this correction to my code and now its displaying the location of php file instead on the browser

<?php
 
// generating a array with image paths
 
$query_images = "SELECT image_path FROM userfolders" ;
$result_images = mysql_query($query_images);
confirm_query($result_images);
 
while ($record_images = mysql_fetch_assoc($result_images)) {
          $image_list[] = $record_images['image_path'] ;
}         
?>
 
<?php
// generating a thumbnail
 
$thumb_height = 100;
 
for ($i=0;$i<count($image_list);$i++)
{
// Content type
header('Content-type: image/jpeg');
$filename = $image_list[$i];
list($width, $height) = getimagesize($filename);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);
 
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
 
$thumbnail_rezised[$i] = imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
 
  // displaying thumbnail
 
$thumbnail[$i]= imagejpeg($thumb,$filename);
echo '<img src="'.$thumbnail[$i].'">';
}

?>

why are you using sessions?

Just have img.php read the image file and resize it and display it.

On the page where the images are suppose to be displayed call the image using the id in the database. i.e. <img src="img.php?id=image_id_here" /> . In the img.php use $_GET to resize and display the proper image.

Great idea then the server is dual processing since it is processing multiple php files at the same time. As for the code index.php is as follows:

<?
//database connections
mysql_connect('localhost','root','');
mysql_select_db('mydatabase');

$result_images = mysql_query("SELECT image_path FROM userfolders");
for ($row=0;$row<mysql_num_rows($result_images);$row++) {
echo '<img src="img.php?id='.$row.'">';
}
?>

Then img.php is as follows:

<?
if (!preg_match('/[^0-9]/',$_GET['id'])) {
header('Content-Type: image/jpeg');
// generating a array with image paths 
mysql_connect('localhost','root','');
mysql_select_db('mydatabase');

$result_images = mysql_query('SELECT image_path FROM userfolders LIMIT '.$_GET['id'].',1');
if (mysql_num_rows($result_images)>0) { //protect from integer injection bugs
$record_images = mysql_fetch_assoc($result_images);

// generating a thumbnail 
$thumb_height = 100;
list($width, $height) = getimagesize($record_images['image_path']);
$ratio = ($height/$width);
$newheight = $thumb_height;
$newwidth = ($thumb_height/$ratio);

$img = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($record_images['image_path']);

imagecopyresized($img, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// displaying thumbnail (no echo or print)
imagejpeg($img);
}
}
?>

Note that the ONLY thing to change in img.php is the database connections.
Also the code in index.php can be added to your existing script. I have tested it to make sure the code works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.