- #!/usr/bin/perl
- use warnings;
- use strict;
- use File::Find;
- use File::Spec::Functions qw(rel2abs);
- use File::Basename;
- use Cwd;
- my $dir = dirname(rel2abs($0));
- my $SCRIPT_NAME = "iphoneos-optimize";
- my $PNGCRUSH_NAME="optipng";
- my $PNGCRUSH = "/opt/local/bin/$PNGCRUSH_NAME";
- my $JPGCRUSH_NAME="jpegoptim";
- my $JPGCRUSH = "/opt/local/bin/$JPGCRUSH_NAME";
- if ($#ARGV + 1 < 1 or $#ARGV + 1 > 2) {
- print STDERR "usage: $SCRIPT_NAME path [-skip-PNGs]\n";
- exit 1;
- }
- my $dstroot = Cwd::realpath(shift);
- my $options = shift;
- print "$SCRIPT_NAME: Converting plists to binary in $dstroot\n";
- find( { wanted => \&optimizePlists }, $dstroot );
- exit(0) if defined $options and $options =~ /-skip-PNGs/;
- print "$SCRIPT_NAME: Optimizing jpgs in $dstroot\n";
- find( { wanted => \&optimizeJPGs }, $dstroot );
- print "$SCRIPT_NAME: Optimizing PNGs in $dstroot\n";
- find( { wanted => \&optimizePNGs }, $dstroot );
- sub optimizePlists {
- my $name = $File::Find::name;
- if ( -f $name && ($name =~ /\.plist$/i || ($name =~ /\.strings$/i && -s $name > 2))) {
- my @args = ( "plutil", "-convert", "binary1", $name );
- if (system(@args) != 0) {
- print STDERR "$SCRIPT_NAME: Unable to convert $name to a binary plist!\n";
- return;
- }
- print "$SCRIPT_NAME: Converted to binary plist: $name\n";
- }
- }
- sub optimizePNGs {
- my $name = $File::Find::name;
- if ( -f $name && $name =~ /^(.*)\.png$/i) {
- my $crushedname = "$1-pngcrush.png";
- my @args = ( $PNGCRUSH, "-o2", "-f0", $name, "-out", $crushedname );
- if (system(@args) != 0) {
- print STDERR "$SCRIPT_NAME: Unable to convert $name to an optimized png!\n";
- return;
- }
- unlink $name or die "Unable to delete original file: $name";
- rename($crushedname, $name) or die "Unable to rename $crushedname to $name";
- print "$SCRIPT_NAME: Optimized PNG: $name\n";
- }
- }
- sub optimizeJPGs {
- my $name = $File::Find::name;
- if ( -f $name && $name =~ /^(.*)\.jpg$/i) {
- my @args = ( $JPGCRUSH, "--strip-all", $name );
- if (system(@args) != 0) {
- print STDERR "$SCRIPT_NAME: Unable to convert $name to an optimized jpg!\n";
- return;
- }
- print "$SCRIPT_NAME: Optimized JPG: $name\n";
- }
- }
iphoneos-optimize script with JPEG compression