1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use File::Find;
  5. use File::Spec::Functions qw(rel2abs);
  6. use File::Basename;
  7. use Cwd;
  8. my $dir = dirname(rel2abs($0));
  9. my $SCRIPT_NAME = "iphoneos-optimize";
  10. my $PNGCRUSH_NAME="optipng";
  11. my $PNGCRUSH = "/opt/local/bin/$PNGCRUSH_NAME";
  12. my $JPGCRUSH_NAME="jpegoptim";
  13. my $JPGCRUSH = "/opt/local/bin/$JPGCRUSH_NAME";
  14. if ($#ARGV + 1 < 1 or $#ARGV + 1 > 2) {
  15. print STDERR "usage: $SCRIPT_NAME path [-skip-PNGs]\n";
  16. exit 1;
  17. }
  18. my $dstroot = Cwd::realpath(shift);
  19. my $options = shift;
  20. print "$SCRIPT_NAME: Converting plists to binary in $dstroot\n";
  21. find( { wanted => \&optimizePlists }, $dstroot );
  22. exit(0) if defined $options and $options =~ /-skip-PNGs/;
  23. print "$SCRIPT_NAME: Optimizing jpgs in $dstroot\n";
  24. find( { wanted => \&optimizeJPGs }, $dstroot );
  25. print "$SCRIPT_NAME: Optimizing PNGs in $dstroot\n";
  26. find( { wanted => \&optimizePNGs }, $dstroot );
  27. sub optimizePlists {
  28. my $name = $File::Find::name;
  29. if ( -f $name && ($name =~ /\.plist$/i || ($name =~ /\.strings$/i && -s $name > 2))) {
  30. my @args = ( "plutil", "-convert", "binary1", $name );
  31. if (system(@args) != 0) {
  32. print STDERR "$SCRIPT_NAME: Unable to convert $name to a binary plist!\n";
  33. return;
  34. }
  35. print "$SCRIPT_NAME: Converted to binary plist: $name\n";
  36. }
  37. }
  38. sub optimizePNGs {
  39. my $name = $File::Find::name;
  40. if ( -f $name && $name =~ /^(.*)\.png$/i) {
  41. my $crushedname = "$1-pngcrush.png";
  42. my @args = ( $PNGCRUSH, "-o2", "-f0", $name, "-out", $crushedname );
  43. if (system(@args) != 0) {
  44. print STDERR "$SCRIPT_NAME: Unable to convert $name to an optimized png!\n";
  45. return;
  46. }
  47. unlink $name or die "Unable to delete original file: $name";
  48. rename($crushedname, $name) or die "Unable to rename $crushedname to $name";
  49. print "$SCRIPT_NAME: Optimized PNG: $name\n";
  50. }
  51. }
  52. sub optimizeJPGs {
  53. my $name = $File::Find::name;
  54. if ( -f $name && $name =~ /^(.*)\.jpg$/i) {
  55. my @args = ( $JPGCRUSH, "--strip-all", $name );
  56. if (system(@args) != 0) {
  57. print STDERR "$SCRIPT_NAME: Unable to convert $name to an optimized jpg!\n";
  58. return;
  59. }
  60. print "$SCRIPT_NAME: Optimized JPG: $name\n";
  61. }
  62. }

iphoneos-optimize script with JPEG compression