Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added png and jpeg image compression task

  • Loading branch information...
commit 3e2e2b332d89d4fc2b4089f5e23136c541853270 1 parent 8908a7c
@Alexeyco Alexeyco authored
Showing with 73 additions and 0 deletions.
  1. +73 −0 ImageCompress.php
View
73 ImageCompress.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Minimizes the HTML files,
+ * removing them from the extra spaces and line breaks
+ *
+ * Install optipng and jpegtran libraries first
+ *
+ * @see http://jpegclub.org/jpegtran/
+ * @see http://optipng.sourceforge.net/
+ *
+ * @author Alexey Popov
+ * @copyright (c) 2012 Leemo studio
+ * @site http://leemo-studio.net
+ * @license http://www.opensource.org/licenses/bsd-license.php
+ */
+
+/**
+ */
+
+/**
+ * Uses the Phing Compress abstract Task
+ */
+require_once 'Compress.php';
+
+class IMAGECompress extends Compress {
+
+ /**
+ * Compress an HTML file
+ *
+ * @param string $source_file Source file path
+ * @return void
+ */
+ protected function _compress($source_file)
+ {
+ $extension = strtolower(pathinfo($source_file, PATHINFO_EXTENSION));
+
+ if ($extension == 'png')
+ {
+ $cmd = 'optipng -o7 '.$source_file;
+ }
+ elseif ($extension == 'jpg' OR $extension == 'jpeg')
+ {
+ $new_file = $source_file.'.tmp';
+ $cmd = 'jpegtran -optimize -perfect -progressive -outfile '
+ .$new_file.' '.$source_file;
+ }
+ else
+ {
+ return;
+ }
+
+ $output = array();
+ $return = null;
+
+ exec($cmd, $output, $return);
+
+ if (is_file($new_file))
+ {
+ rename($new_file, $source_file);
+ }
+
+ foreach ($output as $line)
+ {
+ $this->log($line, Project::MSG_VERBOSE);
+ }
+
+ if ($return != 0)
+ {
+ throw new BuildException('Task exited with code '.$return);
+ }
+
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.