Ant build script to optimize images

ant, development, phpunit Add comments

My PHP-projects are built with help of Apache Ant tool. It just runs a set of predefined applications for many different reasons you define: to make a smoke test of your code, to prepare files for commit, grab code stats, etc.

So, Ant without params runs a default ruleset, which can be a simple smoke test: PHP syntax check of modified files + PHPUnit tests.

One more reason I use it is the recommendation from Google PageSpeed Insights to optimize the images — exclude unnecessary technical information (EXIF) from all your JPGs and PNGs pictures.

Here is how I did it.

First, they recommend to install optipng to optimize the PNG files and the jpegoptim for JPEG (look in “Tools and parameter tuning” section).

So, here is my Ant script:

<?xml version="1.0" encoding="UTF-8"?>
<!-- http://ant.apache.org/manual/using.html -->

<project name="yasno.tv" default="build" basedir=".">
  <target name="build" depends="..., optimg" />
  ...

  <target name="optimg" description="Optimizes images in public folder">
    <apply executable="optipng" failonerror="true" description="Optimizes PNG files">
      <arg value="-o7" />
      <fileset dir="${basedir}/public/img/">
        <include name="**/*.png" />
        <modified>
          <param name="cache.cachefile" value="${basedir}/etc/build/cache.properties"/>
        </modified>
      </fileset>  
    </apply>

    <apply executable="jpegoptim" failonerror="true" description="Optimizes JPG files">
      <arg value="--strip-all" />
      <fileset dir="${basedir}/public/img/">
        <include name="**/*.jpg" />
        <modified>
          <param name="cache.cachefile" value="${basedir}/etc/build/cache.properties"/>
        </modified>
      </fileset>  
    </apply>
  </target>
    
</project>

It optimizes all image files from public/img folder.

The etc/build/cache.properties is the file that contains a file name and the last modification time, so the script optimizes only the changed files.

As result, the image files usually loose 10-20% of their size.

The command to do is:

ant optimg

or just

ant

if you want to run the whole ruleset.

Comments are closed.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in