#!/usr/bin/php <?php // First, some configuration variables: $thumb_max_width = '150'; $web_ready_max_width = '1000'; $gallery_dir_name = "gallery"; $ThumbPrefix = "thumb_"; $WebReadyPrefix = ""; $GalleryFileName = "index.html"; // We could define the header and the footer in another file, then include them. But whatever $htmlfileheader = "<html><head><title></title></head>\n"; $htmlfileheader .= "<body>\n"; $htmlfilefooter = "</body></html>"; // we should use the current dir for input, a subdir for output, // and a sub/sub dir for the images? // do we want to output html files for each image? // for the first round, one html file, outputs // links to each (large-ish) image, and an overall // html file that we configure to have all the links and thumbs function makeThumb($image, $DestDir, $max_width, $prefix) { // Made with the help of http://sniptools.com/vault/dynamic-thumbnailing-with-imagemagick-in-php $thumbFileName = $prefix.$image; // do something to make a little file // Specify your file details $current_file = $image; // Get the current info on the file $current_size = getimagesize($current_file); $current_img_width = $current_size[0]; $current_img_height = $current_size[1]; $image_base = explode('.', $current_file); // This part gets the new thumbnail name $image_basename = $image_base[0]; $image_ext = $image_base[1]; $thumb_name = $DestDir."/". $prefix.$image_basename.'.'.$image_ext; // Determine if the image actually needs to be resized // and if it does, get the new height for it if ($current_img_width > $max_width) { $too_big_diff_ratio = $current_img_width/$max_width; $new_img_width = $max_width; $new_img_height = round($current_img_height/$too_big_diff_ratio); // Convert the file $make_magick = system("convert -geometry $new_img_width"."x"."$new_img_height $current_file $thumb_name", $retval); // Did it work? if (!($retval)) { echo 'Thumbnail created: <img src="' .$thumb_name .'">'; } else { echo 'Error: Please try again.'; } } else { echo 'No need to resize.'; } return $thumbFileName; } function getimages($CurrentDir) { // return an array of image files //using the opendir function $dir_handle = @opendir($CurrentDir) or die("Unable to open $path"); echo "Directory Listing of $CurrentDir"; $ArrayCounter=0; //running the while loop while ($file = readdir($dir_handle)) { $ext = substr($file, strrpos($file, '.') + 1); echo "$file has extension $ext"; if (($ext=="jpg")||($ext=="JPG")) { echo "match!"; $ImageArray[$ArrayCounter]=$file; $ArrayCounter++; } echo "\n"; } //closing the directory closedir($dir_handle); return $ImageArray; } // Since this is a command line script, we'll include some nice helpful stuff if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) { echo "This is a command line PHP script with one option. Usage: $argv[0] <directory full of images> <directory full of images> contains the images you want to make an image gallery from. With the --help, -help, -h, or -? options, you can get this help. \n"; } else { echo "creating gallery from files located in $argv[1]"; $FromDir = $argv[1]; if ($FromDir==".") { $FromDir = getcwd(); } echo "From Dir is $FromDir"; $destDir = "$FromDir/$gallery_dir_name"; // $ThumbDir = "$CurrentDir/gallery/thumb"; I think I'll put // everything in one dir $workDir = "/var/tmp"; // define this as per local system $GalleryFileName = "$destDir/$GalleryFileName"; // make the dir the thumbs will go in $mode="0750"; //permissions $output=mkdir($destDir); echo "I made a dir is ".$output."\n"; echo "\n$CurrentDir - $destDir - $workDir"; # fetch image details $images = getImages($FromDir); echo "\n----\n"; $GalleryFile = fopen($GalleryFileName, 'w') or die("can't open file"); fwrite($GalleryFile, $htmlfileheader); # display on page foreach($images as $img) { // This is where the magic happens // TODO: create an HTML file for each image, allow facebook share? // make the thumb $ThumbFile = makeThumb($img,$destDir,$thumb_max_width,$ThumbPrefix); // make the web friendly large image $ImageFile = makeThumb($img,$destDir,$web_ready_max_width,$WebReadyPrefix); fwrite($GalleryFile, "<a href=\"$ImageFile\"><img src=\"$ThumbFile\"></a>\n"); } // Clean up - write the footer and close the file fwrite($GalleryFile, $htmlfilefooter); fclose($GalleryFile); } // End of the 'else' statement which begins the actual program ?>