barcode
what it says on the tin
this project is filed under
teachin.

PHPGallery

Fot those of you who were in this is the completed code for the PHP gallery, you want to add  in the styling and the body of thee HTML section. Comments have been added in the  appropriate format for the code used but NOT the PHP section as you should have been commenting this as you went along.

Remember to check quotation marks if you paste anything in as they can get altered easily without your realising.

<?php
# SETTINGS
$max_width = 100;
$max_height = 100;

function getPictureType($ext) {
if ( preg_match(‘/jpg|jpeg/i’, $ext) ) {
return ‘jpg';
} else if ( preg_match(‘/png/i’, $ext) ) {
return ‘png';
} else if ( preg_match(‘/gif/i’, $ext) ) {
return ‘gif';
} else {
return ”;
}
}

function getPictures() {
global $max_width, $max_height;
if ( $handle = opendir(“.”) ) {
$lightbox = rand();
echo ‘<ul id=”pictures”>';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) ) {
$split = explode(‘.’, $file);
$ext = $split[count($split) – 1];
if ( ($type = getPictureType($ext)) == ” ) {
continue;
}
if ( ! is_dir(‘thumbs’) ) {
mkdir(‘thumbs’);
}
if ( ! file_exists(‘thumbs/’.$file) ) {
if ( $type == ‘jpg’ ) {
$src = imagecreatefromjpeg($file);
} else if ( $type == ‘png’ ) {
$src = imagecreatefrompng($file);
} else if ( $type == ‘gif’ ) {
$src = imagecreatefromgif($file);
}
if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
$newW = $oldW * ($max_width / $oldH);
$newH = $max_height;
} else {
$newW = $max_width;
$newH = $oldH * ($max_height / $oldW);
}
$new = imagecreatetruecolor($newW, $newH);
imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
if ( $type == ‘jpg’ ) {
imagejpeg($new, ‘thumbs/’.$file);
} else if ( $type == ‘png’ ) {
imagepng($new, ‘thumbs/’.$file);
} else if ( $type == ‘gif’ ) {
imagegif($new, ‘thumbs/’.$file);
}
imagedestroy($new);
imagedestroy($src);
}
echo ‘<li><a href=”‘.$file.'” rel=”lightbox[‘.$lightbox.’]”>';
echo ‘<img src=”thumbs/’.$file.'” alt=”” />';
echo ‘</a></li>';
}
}
echo ‘</ul>';
}
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” dir=”ltr” lang=”en-US”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UFT-8″ />
<title>Pictures</title>
<link rel=”stylesheet” href=”css/lightbox.css” type=”text/css” media=”screen” />

<!–this loads the stylesheet for lightbox but NOT the page as a whole–>

<style type=”text/css”>
#pictures li {
float:left;
height:<?php echo ($max_height + 10); ?>px;
list-style:none outside;
width:<?php echo ($max_width + 10); ?>px;
text-align:center;
}
img {
border:0;
outline:none;
}
/*this styling should really be in a stand alone cssfile or even at the end of the lightbox css but to keep things tidy but I’ve put it here to save me having another file*/
</style>
</head>
<body>

<?php getPictures(); ?>
<!–this lone line of code runs the whole of the getPictures function you have created!–>

<script type=”text/javascript” src=”js/prototype.js”></script>
<script type=”text/javascript” src=”js/scriptaculous.js?load=effects,builder”></script>
<script type=”text/javascript” src=”js/lightbox.js”></script>
<!–add the lightbox scriots to the page to make it look shint and nice–>
</body>
</html>

john doe 2009