tomharto
04-23-2011, 01:09 AM
class_lib.php<?php
class createGallery {
var $result;
function __construct($dir) {
$search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");
$path = '{' . implode(',', $search) . '}';
$this->result = glob($path,GLOB_BRACE);
}
function display($maxSize, $containerSize, $marginBoth)
{
$maxSize2 = $maxSize + $marginBoth;
$number = floor($containerSize / $maxSize2);
$galleryContSize = $maxSize2 * $number + $contmargin;
echo "<div style=\"width:".$containerSize."px;margin:0 auto;background-color:#999;\">\n\n<div id='galleryCont' style='width:".$galleryContSize."px;margin:0 auto;'>\n\n";
foreach ($this->result as $file)
{
$file = str_replace($_SERVER['DOCUMENT_ROOT'],'',$file);
$dimensions1 = getimagesize($file);
$xoldsize = $dimensions1['0'];
$yoldsize = $dimensions1['1'];
if ($xoldsize > $yoldsize) {
$biggest = $xoldsize;
$class = "l";
}
if ($xoldsize < $yoldsize) {
$biggest= $yoldsize;
$class = "p";
}
if ($biggest < $maxSize) {
$ratio = "Not Changed";
$xnew = $xoldsize;
$ynew = $yoldsize;
}else {
$ratio = $maxSize / $biggest;
$xnew = $xoldsize * $ratio;
$ynew = $yoldsize * $ratio;
}
if ($class == "l") {
$ynew2 = $maxSize - $ynew;
$margin = $ynew2 / "2";
$margin = "margin:".$margin."px 0px";
}
if ($class == "p") {
$xnew2 = $maxSize - $xnew;
$margin = $xnew2 / "2";
$margin = "margin:0px ".$margin."px";
}
echo "<div class=\"imgCont\" style='width:".$maxSize."px;height:".$maxSize."px;'>\n<img src=\"".$file."\" style=\"".$margin."\" width=\"".$xnew."\" height=\"".$ynew."\" /></div>\n";
$a++;
if ($a % $number == 0 && $a > 0){
echo "\n<div style='clear:both'></div>";
}
//unset($margin);
}
echo "<div style='clear:both;'></div></div></div>";
}
}
?>
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gallery</title>
<?php include("gallery.php"); ?>
<style type="text/css">>
body
{
margin:0;
padding:0;
}
.imgCont {
background-color:#333;
float:left;
margin:5px 5px;
}
</style>
</head>
<body>
<?php
$gallery = new createGallery("photos2/"); //createGallery("path/to/photos");
echo "<center><h1>Tom's OOP Gallery</h1></center>";
$gallery->displayGallery("250", "800", "10"); //displayGallery("max image size", "containerSize", "margin(both sides)");
?>
</body>
</html>
EDIT: Added oesxyl's improvements.
Just something i made, feel free to use/change/post any improvments/questions you want :). This is my first attempt at an OOP. From the max image width and container size it will determine how many columns it can have (including room for the margin). Also it will find the largest side of an image, and proportionally shrink it until the longer side is "max image width"
class createGallery {
var $result;
function __construct($dir) {
$search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");
$path = '{' . implode(',', $search) . '}';
$this->result = glob($path,GLOB_BRACE);
}
function display($maxSize, $containerSize, $marginBoth)
{
$maxSize2 = $maxSize + $marginBoth;
$number = floor($containerSize / $maxSize2);
$galleryContSize = $maxSize2 * $number + $contmargin;
echo "<div style=\"width:".$containerSize."px;margin:0 auto;background-color:#999;\">\n\n<div id='galleryCont' style='width:".$galleryContSize."px;margin:0 auto;'>\n\n";
foreach ($this->result as $file)
{
$file = str_replace($_SERVER['DOCUMENT_ROOT'],'',$file);
$dimensions1 = getimagesize($file);
$xoldsize = $dimensions1['0'];
$yoldsize = $dimensions1['1'];
if ($xoldsize > $yoldsize) {
$biggest = $xoldsize;
$class = "l";
}
if ($xoldsize < $yoldsize) {
$biggest= $yoldsize;
$class = "p";
}
if ($biggest < $maxSize) {
$ratio = "Not Changed";
$xnew = $xoldsize;
$ynew = $yoldsize;
}else {
$ratio = $maxSize / $biggest;
$xnew = $xoldsize * $ratio;
$ynew = $yoldsize * $ratio;
}
if ($class == "l") {
$ynew2 = $maxSize - $ynew;
$margin = $ynew2 / "2";
$margin = "margin:".$margin."px 0px";
}
if ($class == "p") {
$xnew2 = $maxSize - $xnew;
$margin = $xnew2 / "2";
$margin = "margin:0px ".$margin."px";
}
echo "<div class=\"imgCont\" style='width:".$maxSize."px;height:".$maxSize."px;'>\n<img src=\"".$file."\" style=\"".$margin."\" width=\"".$xnew."\" height=\"".$ynew."\" /></div>\n";
$a++;
if ($a % $number == 0 && $a > 0){
echo "\n<div style='clear:both'></div>";
}
//unset($margin);
}
echo "<div style='clear:both;'></div></div></div>";
}
}
?>
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gallery</title>
<?php include("gallery.php"); ?>
<style type="text/css">>
body
{
margin:0;
padding:0;
}
.imgCont {
background-color:#333;
float:left;
margin:5px 5px;
}
</style>
</head>
<body>
<?php
$gallery = new createGallery("photos2/"); //createGallery("path/to/photos");
echo "<center><h1>Tom's OOP Gallery</h1></center>";
$gallery->displayGallery("250", "800", "10"); //displayGallery("max image size", "containerSize", "margin(both sides)");
?>
</body>
</html>
EDIT: Added oesxyl's improvements.
Just something i made, feel free to use/change/post any improvments/questions you want :). This is my first attempt at an OOP. From the max image width and container size it will determine how many columns it can have (including room for the margin). Also it will find the largest side of an image, and proportionally shrink it until the longer side is "max image width"