Skip to content

Instantly share code, notes, and snippets.

@catlan
Created April 18, 2010 16:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save catlan/370345 to your computer and use it in GitHub Desktop.
Save catlan/370345 to your computer and use it in GitHub Desktop.
revert iphone png optimizations
#!/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="pngcrush";
my $PNGCRUSH = "/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/$PNGCRUSH_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 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", "xml1", $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, "-revert-iphone-optimizations", "-f", "0", $name, $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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment