From 5a55d2cc5e49948394b67768473760d41ec85a17 Mon Sep 17 00:00:00 2001
From: Attila Santo <attisan@2041338.no-reply.drupal.org>
Date: Thu, 24 Sep 2015 21:42:34 +0200
Subject: [PATCH] minor: added settings for pngquant

---
 .../imageapi_optimize/binaries/pngquant.inc        |   81 +++++++++++++++++++-
 1 file changed, 80 insertions(+), 1 deletion(-)

diff --git a/sites/all/modules/contrib_fields/imageapi_optimize/binaries/pngquant.inc b/sites/all/modules/contrib_fields/imageapi_optimize/binaries/pngquant.inc
index 8256668..53fc58f 100644
--- a/sites/all/modules/contrib_fields/imageapi_optimize/binaries/pngquant.inc
+++ b/sites/all/modules/contrib_fields/imageapi_optimize/binaries/pngquant.inc
@@ -12,15 +12,94 @@ function imageapi_optimize_binaries_pngquant_info() {
     'title' => t('pngquant'),
     'url' => 'http://pngquant.org/',
     'type' => 'png',
+    'settings' => array(
+      // 'ncolors' => '128',
+      'speed' => '3',
+      'quality' => '90-99',
+      'ext' => '.png',
+      'ext_auto' => FALSE,
+      'nofs' => FALSE,
+    ),
     'weight' => 0,
   );
 }
 
 /**
+ * Pngquant ImageAPI Optimize form callback.
+ */
+function imageapi_optimize_binaries_pngquant_form($settings) {
+  $form = array();
+
+  // $form['ncolors'] = array(
+    // '#title' => t('Number of colors'),
+    // '#type' => 'textfield',
+    // '#default_value' => $settings['ncolors'],
+    // '#description' => t('File extension (suffix) to use for output files instead of the default ‘-fs8.png’ or ‘-or8.png’.'),
+  // );
+  $form['speed'] = array(
+    '#title' => t('Speed'),
+    '#type' => 'select',
+    '#options' => array_combine(range(1,10), range(1,10)),
+    '#default_value' => $settings['speed'],
+    '#description' => t('1 (brute-force) to 10 (fastest). The default is 3. Speed 10 has 5% lower quality, but is about 8 times faster than the default.'),
+  );
+  $form['quality'] = array(
+    '#title' => t('Quality'),
+    '#type' => 'textfield',
+    '#default_value' => $settings['quality'],
+    '#description' => t('min and max are numbers in range 0 (worst) to 100 (perfect), similar to JPEG.<br/>
+    pngquant will use the least amount of colors required to meet or exceed the max quality.<br/>
+    If conversion results in quality below the min quality the image won‘t be saved<br/>
+    (or if outputting to stdin, 24-bit original will be output) and pngquant will<br/>
+    exit with status code 99.'),
+  );
+  $form['ext'] = array(
+    '#title' => t('File extension'),
+    '#type' => 'textfield',
+    '#default_value' => $settings['ext'],
+    '#description' => t('File extension (suffix) to use for output files instead of the default ‘-fs8.png’ or ‘-or8.png’.'),
+    '#states' => array(
+      'disabled' => array(
+       ':input[name="imageapi_optimize_pngquant[settings][ext_auto]"]' => array('checked' => TRUE),
+      ),
+    ),
+  );
+  $form['ext_auto'] = array(
+    '#title' => t('Automatic file extension'),
+    '#type' => 'checkbox',
+    '#default_value' => $settings['ext_auto'],
+    '#description' => t('Automatically find file extension (suffix). Workaround for drupal image type conversion where extension is not changed.'),
+  );
+  $form['nofs'] = array(
+    '#title' => t('Disable Floyd-Steinberg'),
+    '#type' => 'checkbox',
+    '#default_value' => $settings['nofs'],
+    '#description' => t('Disable Floyd-Steinberg dithering. It’s enabled by default (--floyd).'),
+  );
+
+  return $form;
+}
+
+/**
  * pngquant ImageAPI Optimize binary callback.
  */
 function imageapi_optimize_binaries_pngquant($image, $dst, $cmd, $settings) {
   if ($image->info['mime_type'] == 'image/png') {
-    exec("$cmd --speed=1 --quality=90-99 --force --ext .png " . escapeshellarg($dst));
+    if ($settings['ext_auto']) {
+      $extension = pathinfo($dst, PATHINFO_EXTENSION);
+    } else {
+      $extension = $settings['ext'];
+    }
+    
+    $command = $cmd .
+      ' --speed=' . $settings['speed'].
+      ' --quality=' . $settings['quality'].
+      ' --force --ext ' . escapeshellarg('').
+      ' ' . escapeshellarg($dst);
+
+    exec($command);
+
+
+    // exec("$cmd --speed=1 --quality=90-99 --force --ext .png " . escapeshellarg($dst));
   }
 }
-- 
1.7.10.4

