Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; use Image::Magick; use File::Find; use Getopt::Long; # Declares defaults my($size, $dir, $exts, $is_percent, $help) = ("foo", './', "bmp gif jpg jpeg mng pcd pcx png tga tiff xpm", 0, +0); # Changes defaults if specified on command line GetOptions ('size=s' => \$size, 'directory=s' => \$dir, 'extensions=s' => \$exts, 'help' => \$help); # Gives help if indicated help() if $help || $size eq "foo"; # Creates a hash of valid image extensions my(@exts) = split(" ", $exts); my(%exts); foreach (@exts) { $_ = lc; $exts{$_} = 1; } # Formats the $size as the max dimension for the shrink() # subroutine later, unless it was given as a percentage. $size = "${size}x$size" unless $size =~ /%$/; # Recurses, starting from within the current directory, # and shrinks every valid pic it can find find (\&get_img, $dir); # Displays help sub help { print <<DONE; Usage: -s <SIZE> [OPTIONS] Scales down image files in a directory and all of its subdirectories. Required parameter is: -s, --size May be given as a percentage or as a number of pixel +s. If a percentage is given, the image will be scaled t +o that percentage. If a number is given, the image wil +l be scaled so that its largest dimension (height or widt +h) is not greater than the given number in pixels. Valid options are: -d, --directory Directory to begin looking for files in. Defaults t +o: ./ -e, --extensions Only shrink files that end with the given extensions +. The list of extensions should be quoted, each extens +ion separated by a space. Valid extensions are all thos +e file types supported by PerlMagick. Defaults to: "bmp gif jpg jpeg mng pcd pcx png tga t +iff xpm" -h, --help Display this help message. Examples: -d ~/pics -s 400 -e "jpg gif" Shrinks all .jpg and .gif files in the ~/pics directory and its subdir +ectories so that no image has a height or width greater than 400 pixels. -s 40% -e "bmp" Shrinks all .bmp files in the current directory and its subdirectories + to 40% of their original size. DONE exit; } # If the current item is a file of a valid extension, shrinks it sub get_img() { if (-f) { my($ext) = $_; $ext =~ s/.*\.(.+)/$1/; shrink($_) if $exts{$ext}; } } # Loads the given image and shrinks it to be within the given size or +percentage sub shrink() { my($name) = shift; my($img) = new Image::Magick; print "I got to $name!\n"; print $size; $img->Read($name); $img->Resize('geometry' => $size); $img->Write($name); }

In reply to - Scales down images by Vortacist

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2018-06-21 18:35 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (118 votes). Check out past polls.