I was looking for a quick way to scan a directory and subdirectories for valid image files without relying on the file extension to identify image type.

This reads small chunk of each file and identifies images by header info.

Looking for code review/suggestions. As mentioned in threads bellow, this is work is for educational purposes.

zzSPECTREz

updates

Please read following threads to be aware of limitations and possible bugs.

use strict; use warnings; sub read_dir { my $dir = shift; if ( opendir( DIR, $dir ) ) { my @tmp = readdir(DIR); my @files = map { "$dir/$_" } grep { !/^\.{1,2}\z/ && -f "$dir +/$_" } @tmp; my @dirs = map { "$dir/$_" } grep { !/^\.{1,2}\z/ && -d "$dir +/$_" } @tmp; closedir( DIR ); return ( \@files, \@dirs ); }else{ return; } } sub glob_files { my ($dir, $recurse) = @_; my @files; if ($dir) { my ($files, $dirs) = read_dir($dir) or return; if (@$files) { push @files, @$files; } recursion: { last unless $recurse; while (@$dirs) { my $d = shift (@$dirs); my @f = glob_files("$d", 1); push @files, @f; } } return @files; }else{ return; } } sub find_images { my @files = @_; my $file; my @images; foreach $file (@files) { open FH, $file or die "Error opening [$file]: $!\n"; my $data; my $type; next unless ( -s $file > 9 ); read(FH, $data, 10) or die "Error reading from [$file]: $!\n"; if ( $data =~ /^BM/ ) { $type = 'BMP'; }elsif ( $data =~ /^GIF8[79]a/) { $type = 'GIF'; }elsif ( $data =~ /^\xFF\xD8/ ) { $type = 'JPG'; }else { $type = undef; } push @images, ( [ $file, $type ] ) if ($type); close FH or die "Error closing file [$file]: $!\n"; } return @images if ( scalar (@images) ); return; } my (@files, @images); my $recurse = 1; my $verbose = 1; my $dir = '.'; @files = glob_files($dir, $recurse); @images = find_images(@files); if (@images){ foreach my $a (@images) { if ($verbose){ print "Found [",$a->[0],"] which appears to be an image of + type ",$a->[1],".\n"; }else{ print $a->[0],"\n"; } } } if ($verbose) { print "\n\tFound ", scalar(@files), " files."; print "\n\tFound ", scalar(@images), " images.\n"; }

In reply to Find images regardless of filetype extension. by zzspectrez

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.