Hi.

I am building a very small and simple photo gallery, that will fetch an images url and a comment assigned to this image, from a MySQL database.

Here is a code snippet that causes some errors:
(yes, i know the coding might not be the best, and i appreciate any suggestions on how to make it better..)

if($_GET['sesjon'] == "")
{
$q = mysql_query("select * from directories");
$n = mysql_num_rows($q);
for($i=0; $i < $n; $i++)
{
$r = mysql_fetch_array($q);
echo "<a href = \"?side=bilder&sesjon=".$r["path"]."\">".stripslashes($r["title"])."</a><br>";

}
}
else{					
$per_row = 2; // Number of images per row
$x = 1; // Counter variable

echo '<table width="534" border="0" cellspacing="1" cellpadding="2"><tr>';
$q = mysql_query("select * from photos where category = '$sesjon'");
$n = mysql_num_rows($q);
$r = mysql_fetch_array($q);
while ($r = mysql_fetch_array($q))
  {
    $filecomment = stripslashes($r["comments"]);
    $bildeuri = $r['category'].$r['photo'];
    $source = @imagecreatefromjpeg($bildeuri);
		$y = (imageSY($source))/2;
		$x = (imageSX($source))/2;
  @imagedestroy($source);

echo "<td class=\"bodytext\"><img src = ".$bildeuri." height = ".$y." width =".$x."><br>".$filecomment."</td>";
if ( $x == (int)$per_row )
{
    echo "</tr>";
    $x = 1;
}
else
{
    $x++;
}

  }
echo '</table>'; } ?>

And these are the errors that i get:
Warning: imagesy(): supplied argument is not a valid Image resource in xxxxxxxx/bilder.php on line 45

Warning: imagesx(): supplied argument is not a valid Image resource in xxxxxxxx/bilder.php on line 46

AND also:
All the images are printed one one row.. It should break on every second photo..

    Your row counting problem is due to $x being used for the image size as well as position counting.

    Take the @ out of
    @imagecreatefromjpeg and it will probably give you an error message that will solve the other problem.

    You should really store the size in the DB , because what you are doing is pretty wasteful of resources, loading the pic just to find its size.
    You could also use

    array getimagesize ( string filename [, array imageinfo])

    HalfaBee

      Write a Reply...