/sorting-images/sorting.php
https://bitbucket.org/dbogicevic/daki-projects · PHP · 107 lines · 72 code · 25 blank · 10 comment · 19 complexity · 80a86898b21f5953af65c0f3643885af MD5 · raw file
- <?php
- $dirFiles = array();
- // opens images folder
- if ($handle = opendir('Images')) {
- while (false !== ($file = readdir($handle))) {
- // strips files extensions
- $crap = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");
- $newstring = str_replace($crap, " ", $file );
- // hides folders, writes out ul of images and thumbnails from two folders
- if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
- $dirFiles[] = $file;
- }
- }
- closedir($handle);
- }
- sort($dirFiles);
- foreach($dirFiles as $file)
- {
- echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\" </a></li>\n";
- }
- ?>
- <?php
- /* function: generates thumbnail */
- function make_thumb($src,$dest,$desired_width) {
- /* read the source image */
- $source_image = imagecreatefromjpeg($src);
- $width = imagesx($source_image);
- $height = imagesy($source_image);
- /* find the "desired height" of this thumbnail, relative to the desired width */
- $desired_height = floor($height*($desired_width/$width));
- /* create a new, "virtual" image */
- $virtual_image = imagecreatetruecolor($desired_width,$desired_height);
- /* copy source image at a resized size */
- imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
- /* create the physical thumbnail image to its destination */
- imagejpeg($virtual_image,$dest);
- }
- function get_files($images_dir,$exts = array('jpg')) {
- $files = array();
- if($handle = opendir($images_dir)) {
- while(false !== ($file = readdir($handle))) {
- $extension = strtolower(get_file_extension($file));
- if($extension && in_array($extension,$exts)) {
- $files[] = $file;
- }
- }
- closedir($handle);
- }
- return $files;
- }
- function get_file_extension($file_name) {
- return substr(strrchr($file_name,'.'),1);
- }
- ?>
- <?php
- /** settings **/
- $images_dir = '/img/hersteller/';
- $thumbs_dir = '/img/hersteller/thumb/';
- $thumbs_width = 200;
- $images_per_row = 3;
- $image_files = get_files($images_dir);
- if(count($image_files)) {
- $index = 0;
- sort($thumbnail_image);
- foreach($image_files as $index=>$file) {
- $index++;
- $thumbnail_image = $thumbs_dir.$file;
- if(!file_exists($thumbnail_image)) {
- $extension = get_file_extension($thumbnail_image);
- if($extension) {
- make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
- }
- }
- echo '<a href="',$images_dir.$file,'" class="unterseitenbild"><img src="',$thumbnail_image,'" /></a>';
- if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
- }
- echo '<div class="clear"></div>';
- }
- else {
- echo '<p>Keine Bilder vorhanden!</p>';
- }
- ?>