Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Image aggregator

This PHP snippet outputs the contents of a table (beginning & ending tags not included) containing all the images from the current directory.
<?PHP
 $columns = 3;
 $im = glob("*.{gif,jpg,png}", GLOB_BRACE);
 $rows = ceil(count($im) / $columns);
 for ($i = 0; $i < $rows; $i++) {
  echo "\n<TR>";
  for ($j = $columns*$i; isset($im[$j]) && $j - $columns*$i < $columns; $j++) {
   echo "<TD>$im[$j]<BR><IMG SRC='$im[$j]'></TD>";
  }
  echo "</TR>";
 }
?>

walk a path

#!/usr/bin/env python

import os

from os.path import isdir, abspath, join
from glob import iglob


def walk(name='.'):
    print abspath(name)
    for item in iglob(join(name, '*')):
        if isdir(item):
            walk(item)
        else:
            print abspath(item)


if __name__ == '__main__':
    walk()

Create Image Thumbnails (Python)

# experiments with the Python Image Library (PIL)

# free from:  http://www.pythonware.com/products/pil/index.htm

# create 128x128 (max size) thumbnails of all JPEG images in the working folder

# Python23 tested    vegaseat    25feb2005


import glob
import Image

# get all the jpg files from the current folder

for infile in glob.glob("*.jpg"):
  im = Image.open(infile)
  # convert to thumbnail image

  im.thumbnail((128, 128), Image.ANTIALIAS)
  # don't save if thumbnail already exists

  if infile[0:2] != "T_":
    # prefix thumbnail file with T_

    im.save("T_" + infile, "JPEG")

php glob_rsort_modtime()

<?php
/**
 FUNCTION: glob_rsort_modtime()
 * Glob reverse sorted by file modification time.
 *
 * Returns an array of files matching the given glob pattern, sorted 
 * by their modification times in descending order.  The array keys
 * hold the filepath and the values hold the mtime.  On error, array
 * will be indexed and contain 'false' and an error message.
 *
 * {@source}
 *
 * @param string $patt Pattern to search, including glob braces.
 * @return array filename => mtime OR false, 'errormsg'.
 */
function glob_rsort_modtime( $patt ) {
    if ( ( $files = @glob($patt, GLOB_BRACE) ) === false ) {
        return array( false, 'Glob error.');
    }
    if ( !count($files) ) {
        return array( false, 'No files found.');
    }
    $rtn = array();
    foreach ( $files as $filename ) {
        $rtn[$filename] = filemtime($filename);
    }
    arsort($rtn);
    reset($rtn);
    return $rtn;
}

$files = glob_rsort_modtime( './*' ) ;
echo'<pre>'.print_r($files, true).'</pre>';
?> 
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS