I am wanting to alter the following php code so that it resizes and displays images one at a time, with a ‘next’ and ‘previous’ button, in order to browse through the photos. I don’t want any image gallery or lightbox solutions, rather the photos to just show on the page. I’m new to php so If someone can help out or point me in the right direction all help is appreciated.
$sql = "select * from people";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<div class=\"picture\">";
echo "<p>";
// Note that we are building our src string using the filename from the database
echo "<img src=\"images/" . $row['filename'] . "\" alt=\"\" /><br />";
echo $row['fname'] . " " . $row['lname'] . "<br />";
echo "</p>";
echo "</div>";
You can scale them in the browser using
widthandheightattributes (or just one will maintain aspect ratio) however this is bad for many reasons including bandwidth, page performance and image quality.You can re-size the images using libraries such as
GDorImagickA quick sample with
IMagick:Note
Be sure to have permissions to read/write. You can verify this permissions by using
is_readableandis_writable.Loading
It’s recommended to load the images using
AJAXwhich is quite easy if usingJQueryor a similar library.The PHP would be relatively simple too, a structure similar to this:
** Another note **
As stated by kgb you should resize these on upload, however, they may not be user submitted so you can also check if the thumbs exist on output and generate any as required. Certainly don’t generate them for every view though.