PDA

View Full Version : Simple image gallery


cragllo
03-04-2005, 11:26 PM
ok, i have this code to make thumbnails:
<?php

import_request_variables("gP", "");

$t_width = 100;

$img = $_SERVER['QUERY_STRING'];

// get and display jpeg images
if(stristr($img,".jpg")){
header("Content-type: image/jpeg");
$srcimage = imagecreatefromjpeg($img);
$width = imageSX($srcimage);
$height = imageSY($srcimage);
$newh1= $height / $width;
$newh2= $newh1 * $t_width;
$destimage = imagecreatetruecolor($t_width,$newh2);
imagecopyresized($destimage,$srcimage,0,0,0,0,$t_width,$newh2,$width,$height);
ImageJPEG($destimage);
ImageDestroy($destimage);
}

?>

And use this code to display the images:
<?
$record = $_GET['recid'];
if(empty($record))
$record = 1;

$query = "SELECT * FROM screenshots";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);

// the number of records, record number to start from and number of pages
$numrows = mysql_num_rows($result);
$pagestart = ($record * $pagecount - ($pagecount));
$numpages = $numrows / $pagecount;

if(($numrows % $pagecount) != 0)
$numpages += 1;

$query = mysql_query("SELECT * FROM screenshots ORDER BY id DESC LIMIT $pagestart, $pagecount ");
?>
<font size="2" face="Arial">
<form name="form1" method="post" action="<?=$_SERVER['PHP_SELF'];?>">
Show <select name="items_per_page" value="<?= $pagecount;?>">
<option value="20">20</option>
<option value="40">40</option>
<option value="60">60</option>
<option value="100">100</option>
</select> downloads per page.
<input type="submit" name="Submit" value="GO">
</form>Go to page:
<?

// this creates the pagination links
for($i=1; $i < $numpages; $i++) {
echo '&nbsp;&nbsp;<a href="' . $_SERVER['PHP_SELF'] . '?recid=' . $i . '">' . $i . '</a>&nbsp;&nbsp;';
}
?>
</font><br>
<?
while($var = mysql_fetch_array($query)){
?>

<a href="screenshot.php?id=<?=$var[id]?>"><img src="thumb.php?screenshots/<?=$var[image]?>" border="0"></a>&nbsp;

<?
}
?>

I also have this code to display images in a folder...
<?
$files = Array();
$path = "images/animations";
$dh = opendir($path);
while ($file = readdir($dh)) {
if (!is_dir($path."/".$file)) {
if (is_file($path."/".$file)) {
if (($file!=".") && ($file!="..")) {
$files[] = $file;
}
}
}
}
closedir($dh);

asort($files);
foreach($files as $index=>$file) {
echo "<img src=\"images/animations/".$file."\">\n";
}
?>

What I want to do is, display all image in a folder, as thumbnails. And each image is a link to the full sized image. (not using a database)

Also to display 20 per page and have pagination...

So if someone could explaing how, or give exampleas, or show me how, that would be great.

Thank you,
Craig.

cfc
03-05-2005, 12:03 AM
For page numbers, a system where the lowest item number on any given page is (page number - 1 * # of items) + 1 and the highest number is (page number * # of items) should work, so:

$lowerItem = (($page - 1) * $itemsPerPage) + 1;
$upperItem = $page * $itemsPerPage;

this way you can let the user specify the items per page if you wish, or you can just replace $itemsPerPage with 20 if that's what it will be for all users. It should be noted that I haven't done this, but it works theoretically where page 1 will contain items 1 - 20 and page 2 will contain items 21 - 40.

Are you going to scale the images down EVERY time someone hits the page? Seems kind of inefficient to me. You could use an admin upload page that generates the thumbnails in a seperate folder when you upload them.

Cycling through directories is easy enough. See opendir() (http://www.php.net/opendir), readdir() (http://www.php.net/readdir), and closedir() (http://www.php.net/closedir).

cragllo
03-05-2005, 12:08 AM
I lack the knowledge to make such a system...

Wat i want is a system where users can upload their own images, and then I approve them before they are shown... idealy...
But I do not know where to start.

I have a vuage idea, have a MySQL database and users can add their own, then I approve them, but the server Im on has Gobal vars turned off (php is in safe mode i think)

And the thinks I do dont work because of it...