...

OO Gallery based on a directory

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"

oesxyl
04-23-2011, 04:43 PM
few problems:

1. $file is the absolute path to the image, will not allways work when you use it in the src attribute of the img tag( server settings), so you need a relative path to the root document for this:


$path = str_replace($_SERVER['DOCUMENT_ROOT'],'',$file);


2. clearing floats:
move this after you output the image inside the foreach

$a++;
if ($a % $number == 0 && $a > 0){
echo "\n<div style='clear:both' id=\"cf".$a."\"></div>";
}

and add this just after foreach

echo "\n<div style='clear:both' id=\"cf".$a."\"></div>";

id can't start with a number, you need a prefix, i used 'cf' (abbr for clear float).

3. move last block:


</div></div>


from html page inside the method displayGallery since you start divs there.

4. you need to add a offset for left and right margin to this else last img will apear on the next line in some situation. I used a $contmargin of 10px.

$galleryContSize = $maxSize2 * $number + $contmargin;


some personal opinions:
- usualy a class is a model of something which is a noun( when the class doesn't model a relation between objects), and probably the class name should be Gallery not createGallery.
- the suffix Gallery in method name, displayGallery is redundat because the method is of the class Gallery, should be only display.

best regards

tomharto
04-24-2011, 05:07 PM
Thanks for your advice/changes :). As i said its my first attempt at an OOP so any advice i can get now to help me prevent bad habits later on are always helpful :)

EDIT: Also the id inside the float was to check the value of $number i dont need it in at all so ill just take it out :P. Also changed the posted code, hopefully thats better than before :)

britney12
07-22-2011, 12:01 PM
Sorry, my question is a little out of topic, but still.....
I'm having trouble finding something. If I can't find it, I could
bumble my way through writing something, but I thought I'd check around
first.

I am looking for a php script or some other software that will build a
dynamic photo gallery without adding or changing my photos, or creating
any static content.

I use Gallery2 for my favorite photos...but I'd like the rest of the
thousands to be available online, and to be available just becuase
they're in the photos folder. I keep my photos in something like
/home/mdg/Photos/2006/whatever ../2006/whateverelse
.../2006/whateverthethird and I add them all the time.

I want a script that I could keep at
/home/mdg/public_html/somescript.php or whatever...and point it at my
photo root and have them all be there when I hit the script with my
browser.

Any advice?

Inigoesdr
07-22-2011, 07:03 PM
Any advice?

Did you... try the class out? It seems to be almost exactly what you're looking for. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum