Last active
December 14, 2015 20:19
-
-
Save RobinKardell/5143436 to your computer and use it in GitHub Desktop.
This is just a begin code sample that will be a photogallery.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Detta är version 1 av mitt fotogalleri jag programmerar. | |
Vad denna gör i dagsläget | |
- Listar alla bilder som ligger i en mapp som heter 'bilder' | |
- När bilderna listas så skapas det automatisk thumbnails av bilderna (150x150px), | |
dessa thumbnails ligger sig i en mapp som heter 'thumbs' som ligger i mappen 'bilder', finns inte mappen skapas mappen. | |
- Klickar man sig in i bilderna så får man en stor bild att vissas, och man får möjligheter att navigera runt i alla bilder. | |
Struktur | |
- Class (mapp) | |
- Image.php - "huvudklassen som innehåller alla funktioner som index.php kommer använda sig av" | |
- bilder - "innehåller alla bilder" | |
- index.php - "Huvudfilen som gör det lilla magiska" | |
Det som ska bli mer utvecklat är: | |
- Kunna lägga bilder i olika mappar(album), som man sen kan se istället, | |
men möjlighet att kunna se alla bilder i en lång lista om det så önskas. | |
- Man ska kunna skapa egna mappar(albums), sedan kunna lägga in olika bilder i dessa album. | |
- Enklare design ska komma till, byggas på HTML5 | |
- snygga till bilderna som man klickas till sig så det blir lättare att se hela bilden, | |
men med möjligheten att kunna ladda ner bilden om man vill det. | |
- Navigeringen ska bättras på. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Image{ | |
public $_dir; | |
public $_thumbFolder = 'thumbs'; | |
public $_files = array(); | |
private $wthumb = 150; | |
private $hthumb = 150; | |
public $_total; | |
public function __construct($dir){ | |
$this->_dir = $dir; | |
$this->ExistThumbsFolder(); | |
$this->count(); | |
if(is_dir($this->_dir) === true){ | |
$dirHand = opendir($this->_dir); | |
while (false !== ($filename = readdir($dirHand))) { | |
if(!is_dir($filename) === TRUE && $filename != '' && $filename != '.' && $filename != '..' && $filename != $this->_thumbFolder){ | |
$this->_files[] = array( | |
'file_thumb' => $this->_dir.'/'.$this->_thumbFolder.'/thumb_'.$filename, | |
'filenormal' => $this->_dir.'/'.$filename | |
); | |
} | |
} | |
} | |
} | |
private function count(){ | |
$x = 0; | |
if(is_dir($this->_dir) === true){ | |
$dirHand = opendir($this->_dir); | |
while (false !== ($filename = readdir($dirHand))) { | |
if(!is_dir($filename) === TRUE && $filename != '' && $filename != '.' && $filename != '..' && $filename != $this->_thumbFolder){ | |
$x++; | |
} | |
} | |
$this->_total = $x; | |
} | |
} | |
public function readFiles(){ | |
if(is_dir($this->_dir) === true){ | |
$dirHand = opendir($this->_dir); | |
while (false !== ($filename = readdir($dirHand))) { | |
if(!is_dir($filename) === TRUE && $filename != '' && $filename != '.' && $filename != '..' && $filename != $this->_thumbFolder){ | |
if(self::ThumbExists('thumb_'.$filename)===FALSE){ | |
$kaboom = explode(".", $filename); | |
$fileExt = end($kaboom); | |
$target_file = $this->_dir."/".$filename; | |
$thumbnail = $this->_dir."/".$this->_thumbFolder."/thumb_$filename"; | |
self::create_thumb($target_file, $thumbnail, $this->wthumb, $this->hthumb, $fileExt); | |
} | |
$this->_files[] = array( | |
'file_thumb' => $this->_dir.'/'.$this->_thumbFolder.'/thumb_'.$filename, | |
'filenormal' => $this->_dir.'/'.$filename | |
); | |
} | |
} | |
if(!empty($this->_files)===TRUE){ | |
return $this->_files; | |
} | |
} | |
} | |
private function ThumbExists($file){ | |
if(file_exists($this->_dir.'/'.$this->_thumbFolder.'/'.$file)){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
private function ExistThumbsFolder(){ | |
if(is_dir($this->_dir.'/'.$this->_thumbFolder) === false){ | |
mkdir($this->_dir.'/'.$this->_thumbFolder, 0744); | |
} | |
} | |
private function create_thumb($target, $newcopy, $w, $h, $ext) { | |
list($w_orig, $h_orig) = getimagesize($target); | |
$src_aspect = round(($w_orig / $h_orig), 1); | |
$thumb_aspect = round(($w / $h), 1); | |
if($src_aspect < $thumb_aspect){ | |
//higher | |
$new_size = array($w, ($w /$w_orig) * $h_orig); | |
$src_pos = array(0, (($new_size[1] - $h) * ($h_orig / $new_size[1])) / 2); | |
}else if($src_aspect > $thumb_aspect){ | |
//wider | |
$new_size = array(($w / $h_orig) * $w_orig, $h); | |
$src_pos = array((($new_size[0] - $w) * ($w_orig / $new_size[0])) / 2, 0); | |
}else{ | |
// same shape | |
$new_size = array($w, $h); | |
$src_pos = array(0, 0); | |
} | |
if($new_size[0] < 1) $new_size[0] = 1; | |
if($new_size[1] < 1) $new_size[1] = 1; | |
$ext = strtolower($ext); | |
$img = ""; | |
if ($ext == "gif"){ | |
$img = imagecreatefromgif($target); | |
} else if($ext =="png"){ | |
$img = imagecreatefrompng($target); | |
} else { | |
$img = imagecreatefromjpeg($target); | |
} | |
$tci = imagecreatetruecolor($w, $h); | |
imagecopyresampled($tci, $img, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $w_orig, $h_orig); | |
if ($ext == "gif"){ | |
imagegif($tci, $newcopy); | |
} else if($ext =="png"){ | |
imagepng($tci, $newcopy); | |
} else { | |
imagejpeg($tci, $newcopy, 84); | |
} | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'Class/Image.php'; | |
$i = new Image('bilder'); | |
if(isset($_GET['bild']) === TRUE && empty($_GET['bild']) === FALSE){ | |
$next =($_GET['bild'] +1); | |
$prev = ($_GET['bild'] -1); | |
$now = $_GET['bild']; | |
$IAN = ($_GET['bild'] -1); | |
if($IAN < $i->_total){ | |
if($now == 1){ | |
echo "<a href='{$_SERVER['PHP_SELF']}?bild={$i->_total}'>Prev</a>"; | |
}else if($prev < $now){ | |
echo "<a href='{$_SERVER['PHP_SELF']}?bild={$prev}'>Prev</a>"; | |
} | |
echo '--'; | |
if($now == $i->_total){ | |
echo "<a href='{$_SERVER['PHP_SELF']}?bild=1'>Next</a>"; | |
}else if($now < $i->_total){ | |
echo "<a href='{$_SERVER['PHP_SELF']}?bild={$next}'>Next</a>"; | |
} | |
echo '<hr/>'; | |
echo "<img src='".$i->_files[$IAN]['filenormal']."' />"; | |
} | |
}else{ | |
$x = 0; | |
foreach ($i->readFiles() as $key => $val) { | |
$x++; | |
$p = ($key + 1); | |
echo "<a href=\"{$_SERVER['PHP_SELF']}?bild={$p}\"><img src=\"{$val['file_thumb']}\" alt=\"{$val['file_thumb']}\" /></a>"; | |
if($x == 4){ | |
echo "<br />"; | |
$x = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment